/
gflows_context.go
162 lines (137 loc) · 3.65 KB
/
gflows_context.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jbrunton/gflows/io"
"github.com/jbrunton/gflows/io/pkg"
"github.com/thoas/go-funk"
"github.com/davecgh/go-spew/spew"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
// GFlowsContext - current command context
type GFlowsContext struct {
Dir string
ConfigPath string
GitHubDir string
Config *GFlowsConfig
EnableColors bool
}
type ContextOpts struct {
ConfigPath string
EnableColors bool
Debug bool
Engine string
AllowNoContext bool
}
func NewContext(fs *afero.Afero, logger *io.Logger, opts ContextOpts) (*GFlowsContext, error) {
contextDir := filepath.Dir(opts.ConfigPath)
config, err := LoadConfig(fs, logger, opts)
if err != nil {
return nil, err
}
githubDir := config.GithubDir
if githubDir == "" {
githubDir = ".github/"
}
if !filepath.IsAbs(githubDir) {
githubDir = filepath.Join(filepath.Dir(contextDir), githubDir)
}
context := &GFlowsContext{
Config: config,
ConfigPath: opts.ConfigPath,
GitHubDir: githubDir,
Dir: contextDir,
EnableColors: opts.EnableColors,
}
logger.Debugf("Creating context: %s\n", spew.Sdump(context))
return context, nil
}
// CreateContextOpts - creates ContextOpts from flags and environment variables
func CreateContextOpts(cmd *cobra.Command) ContextOpts {
configPath, err := cmd.Flags().GetString("config")
if err != nil {
panic(err)
}
if configPath == "" {
configPath = os.Getenv("GFLOWS_CONFIG")
}
if configPath == "" {
configPath = ".gflows/config.yml"
}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
panic(err)
}
disableColors, err := cmd.Flags().GetBool("disable-colors")
if err != nil {
panic(err)
}
if os.Getenv("GFLOWS_DISABLE_COLORS") == "true" {
disableColors = true
}
var engine string
if cmd.Flags().Lookup("engine") != nil {
engine, err = cmd.Flags().GetString("engine")
if err != nil {
panic(err)
}
}
allowNoContext := funk.ContainsString([]string{"init", "version"}, cmd.Name())
return ContextOpts{
ConfigPath: configPath,
EnableColors: !disableColors,
Debug: debug,
Engine: engine,
AllowNoContext: allowNoContext,
}
}
func (context *GFlowsContext) WorkflowsDir() string {
return filepath.Join(context.Dir, "/workflows")
}
func (context *GFlowsContext) LibsDir() string {
return filepath.Join(context.Dir, "/libs")
}
func (context *GFlowsContext) GetPathInfo(localPath string) (*pkg.PathInfo, error) {
relPath, err := filepath.Rel(context.Dir, localPath)
if err != nil {
return nil, err
}
if strings.HasPrefix(relPath, "..") {
return nil, fmt.Errorf("Expected %s to be a subdirectory of %s", localPath, context.Dir)
}
return &pkg.PathInfo{
LocalPath: localPath,
SourcePath: localPath,
Description: localPath,
}, err
}
// ResolvePath - returns paths relative to the working directory (since paths in configs may be written relative to the
// context directory instead)
func (context *GFlowsContext) ResolvePath(path string) string {
if filepath.IsAbs(path) {
return path
}
if pkg.IsRemotePath(path) {
return path
}
if pkg.IsGitPath(path) {
return path
}
// check if the path given is relative to the context dir (e.g. already includes ".gflows/")
relPath, err := filepath.Rel(context.Dir, path)
if err != nil {
return path
}
if strings.HasPrefix(relPath, "..") {
// if it's not, join it to the context dir
return filepath.Join(context.Dir, path)
}
return path
}
// ResolvePaths - returns an array of resolved paths
func (context *GFlowsContext) ResolvePaths(paths []string) []string {
return funk.Map(paths, context.ResolvePath).([]string)
}