/
command.go
257 lines (218 loc) · 6.3 KB
/
command.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package commands
import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"github.com/jlentink/aem/internal/aem"
"github.com/jlentink/aem/internal/aem/objects"
"github.com/jlentink/aem/internal/cli/cachedir"
"github.com/jlentink/aem/internal/cli/project"
"github.com/jlentink/aem/internal/output"
"github.com/jlentink/aem/internal/sliceutil"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"strings"
)
// Exit codes for AEM
const (
ExitNormal = 0
ExitError = 1
HomeDirFile = ".aem"
)
// Command internal interface for commands
type Command interface {
setup() *cobra.Command
preRun(cmd *cobra.Command, args []string)
run(cmd *cobra.Command, args []string)
}
func getConfig() (*objects.Config, error) {
return aem.GetConfig()
}
func getConfigAndInstance(i string) (*objects.Config, *objects.Instance, string, error) {
cnf, err := aem.GetConfig()
if err != nil {
return nil, nil, "Could not load config file. (%s)", err
}
currentInstance, err := aem.GetByName(i, cnf.Instances)
if err != nil {
return cnf, nil, "Could not find instance. (%s)", err
}
aem.Cnf = cnf
return cnf, currentInstance, ``, nil
}
//nolint
func getConfigAndInstanceOrGroup(i, g string) (*objects.Config, []objects.Instance, string, error) {
if len(g) > 0 {
return getConfigAndGroup(g)
}
c, in, s, e := getConfigAndInstance(i)
return c, []objects.Instance{*in}, s, e
}
func getConfigAndInstanceOrGroupWithRoles(i, g string, r []string) (*objects.Config, []objects.Instance, string, error) {
if len(g) > 0 {
return getConfigAndGroupWithRoles(g, r)
}
c, in, s, e := getConfigAndInstance(i)
if e != nil {
return c, nil, s, e
}
if !sliceutil.InSliceString(r, in.Type) {
return c, []objects.Instance{*in}, s, fmt.Errorf("instance is not of type %s", in.Type)
}
return c, []objects.Instance{*in}, s, e
}
func getCdnConfig(name string) (*objects.CDN, error){
cnf, err := aem.GetConfig()
if err != nil {
return nil, fmt.Errorf("Could not load config file. (%s)", err)
}
for _, cdn := range cnf.CDNs {
if strings.EqualFold(cdn.Name, name) {
return &cdn, nil
}
}
return nil, fmt.Errorf("could not find cdn: %s", name)
}
func getConfigAndGroup(i string) (*objects.Config, []objects.Instance, string, error) {
cnf, err := aem.GetConfig()
if err != nil {
return nil, nil, "Could not load config file. (%s)", err
}
currentInstance, err := aem.GetByGroup(i, cnf.Instances)
if err != nil {
return cnf, nil, "Could not find instance. (%s)", err
}
aem.Cnf = cnf
return cnf, currentInstance, ``, nil
}
func getConfigAndGroupWithRole(i, r string) (*objects.Config, []objects.Instance, string, error) {
return getConfigAndGroupWithRoles(i, []string{r})
}
func getConfigAndGroupWithRoles(i string, r []string) (*objects.Config, []objects.Instance, string, error) {
cnf, err := aem.GetConfig()
if err != nil {
return nil, nil, "Could not load config file. (%s)", err
}
currentInstance, err := aem.GetByGroupAndRoles(i, cnf.Instances, r)
if err != nil {
return cnf, nil, "Could not find instance. (%s)", err
}
aem.Cnf = cnf
return cnf, currentInstance, ``, nil
}
// CheckConfigExists is the configuration file available
func CheckConfigExists() (bool, error) {
p, err := project.GetConfigFileLocation()
if err != nil {
return false, fmt.Errorf("could not get config file location: %s", err)
}
if !project.Exists(p) {
return false, nil
}
return true, nil
}
// ReadRegisteredProjects reads aem file to find registered projects
func ReadRegisteredProjects(homedir string) objects.Projects {
projects := objects.Projects{}
if project.Exists(homedir + "/" + HomeDirFile) {
toml.DecodeFile(homedir+"/"+HomeDirFile, &projects) // nolint: errcheck
}
return projects
}
func changeProjectDir(projectName string) {
projects := cachedir.RegisteredProjects()
for _, cProject := range projects {
if strings.EqualFold(cProject.Name, projectName) {
err := os.Chdir(cProject.Path)
if err != nil {
output.Printf(output.NORMAL, "Could not change to project folder: %s.\n", err.Error())
os.Exit(ExitError)
}
return
}
}
output.Printf(output.NORMAL, "Could not find project: %s.\n", projectName)
os.Exit(ExitError)
}
func lookupByPath() error {
cwd, err := project.GetWorkDir()
projects := cachedir.RegisteredProjects()
if err != nil {
return err
}
for _, p := range projects {
if strings.HasPrefix(cwd, p.Path) {
err := os.Chdir(p.Path)
return err
}
}
return fmt.Errorf("not found")
}
// ConfigCheckListProjects Check for config and list projects if needed
func ConfigCheckListProjects() {
if Project != "" {
changeProjectDir(Project)
}
b, err := CheckConfigExists()
if err != nil {
output.Print(output.NORMAL, "Error while searching for config file.\n")
output.Printf(output.VERBOSE, "error: %s", err.Error())
os.Exit(ExitError)
}
if !b {
err := lookupByPath()
if err == nil {
return
}
output.Print(output.NORMAL, "No config file in the current directory.\n")
p, err := project.HomeDir()
if err != nil {
output.Print(output.NORMAL, "Could not get the home dir.\n")
os.Exit(ExitError)
}
projects := ReadRegisteredProjects(p)
if len(p) > 0 {
output.Print(output.NORMAL, "You have the following projects on registered.\n")
output.Print(output.NORMAL, "Switch to the project location to start using the tool\n\n")
for _, project := range projects.Project {
fmt.Printf(" * %s - %s\n", project.Name, project.Path)
}
output.Print(output.NORMAL, "\n")
}
os.Exit(ExitError)
}
}
// RegisterProject in homedir
func RegisterProject() {
cnf, err := getConfig()
if err != nil {
return
}
cwd, err := project.GetWorkDir()
if err != nil {
return
}
cachedir.RegisterProject(cnf.ProjectName, cwd)
}
// WriteRegisterFile writes project in project registry file
func WriteRegisterFile(projects objects.Projects, homedir string) {
buf := new(bytes.Buffer)
err := toml.NewEncoder(buf).Encode(projects)
if err != nil {
return
}
err = ioutil.WriteFile(homedir+"/"+HomeDirFile, buf.Bytes(), 0644)
if err != nil {
return
}
}
// GetInstancesAndConfig gets config and configuration for instance or group
func GetInstancesAndConfig(i, g string) (*objects.Config, []objects.Instance, string, error) {
if len(g) > 0 {
return getConfigAndGroup(g)
}
cnf, instance, errString, err := getConfigAndInstance(i)
aem.Cnf = cnf
return cnf, []objects.Instance{*instance}, errString, err
}