-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
62 lines (53 loc) · 1.42 KB
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"os"
"path"
"strconv"
)
const (
projectDirEnvVar = "ProjectDirectory"
reqDotGitEnvVar = "RequireDotGit"
maxProjDepthEnvVar = "MaxProjectDepth"
maxResults = 5
)
type Params struct {
ProjectsDir string
RequireDotGit bool
MaxProjectDepth uint
MaxResults uint
}
func NewParamsFromEnv() (*Params, error) {
projDir := os.Getenv(projectDirEnvVar)
if len(projDir) == 0 {
return nil, fmt.Errorf("Please set project directory before using the workflow")
}
reqDotGit, err := strconv.ParseBool(os.Getenv(reqDotGitEnvVar))
if err != nil {
return nil, fmt.Errorf("could not parse %s: %w", reqDotGitEnvVar, err)
}
maxProjDepth, err := strconv.Atoi(os.Getenv(maxProjDepthEnvVar))
if err != nil {
return nil, fmt.Errorf("could not parse %s: %w", maxProjDepthEnvVar, err)
}
return &Params{
ProjectsDir: projDir,
RequireDotGit: reqDotGit,
MaxProjectDepth: uint(maxProjDepth),
MaxResults: maxResults,
}, nil
}
func (p *Params) Equal(p2 Params) bool {
return p.RequireDotGit == p2.RequireDotGit &&
p.MaxProjectDepth == p2.MaxProjectDepth &&
p.MaxResults == p2.MaxResults &&
p.ProjectsDir == p2.ProjectsDir
}
func (p *Params) ProjectsPath() string {
// absolute paths with be honored
if path.IsAbs(p.ProjectsDir) {
return p.ProjectsDir
}
// Relative paths are in relation to the user's home directory
return path.Join(os.Getenv("HOME"), p.ProjectsDir)
}