-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
243 lines (224 loc) · 5.63 KB
/
config.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
package main
import (
"bytes"
"errors"
"fmt"
"github.com/eighty4/maestro/git"
"github.com/eighty4/maestro/util"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
)
type Config struct {
Dir string
FileExists bool
Filename string
Packages []*Package
Repositories []*git.Repository
}
func (c *Config) AddPackages(packages []*Package) {
c.Packages = append(c.Packages, packages...)
}
func (c *Config) SaveConfig() error {
cy, err := convertConfigToYamlModel(c)
if err != nil {
return err
}
var b bytes.Buffer
encoder := yaml.NewEncoder(&b)
encoder.SetIndent(2)
err = encoder.Encode(cy)
if err != nil {
return err
}
filename := "maestro.yaml"
err = util.WriteFile(filepath.Join(c.Dir, filename), b.Bytes())
if err != nil {
return err
}
c.FileExists = true
c.Filename = filename
return nil
}
type config struct {
Project *configProject `yaml:"project,omitempty"`
Workspace *configWorkspace `yaml:"workspace,omitempty"`
}
type configProject struct {
Packages []*configPackage
}
type configPackage struct {
Name string `yaml:"name,omitempty"`
Path string `yaml:"path,omitempty"`
Commands []*configCommand `yaml:"commands,omitempty"`
}
type configCommand struct {
Desc string `yaml:"desc,omitempty"`
Exec string `yaml:"exec,omitempty"`
Id string `yaml:"id,omitempty"`
Name string `yaml:"name,omitempty"`
}
type configWorkspace struct {
Repositories []*configRepository
}
type configRepository struct {
Name string `yaml:"name,omitempty"`
Path string `yaml:"path,omitempty"`
Git *git.RemoteDetails
}
func convertConfigToYamlModel(c *Config) (*config, error) {
py, err := convertPackagesToYamlModel(c.Dir, c.Packages)
if err != nil {
return nil, err
}
wy, err := convertRepositoriesToYamlModel(c.Dir, c.Repositories)
if err != nil {
return nil, err
}
cy := &config{Project: py, Workspace: wy}
return cy, nil
}
func convertPackagesToYamlModel(rootDir string, packages []*Package) (*configProject, error) {
if len(packages) == 0 {
return nil, nil
}
result := &configProject{}
for _, pkg := range packages {
path, err := filepath.Rel(rootDir, pkg.dir)
if err != nil {
return nil, err
}
var cmds []*configCommand
for _, cmd := range pkg.commands {
cmds = append(cmds, &configCommand{
Desc: cmd.Desc,
Id: cmd.Id,
Name: cmd.Name,
})
}
name := pkg.name
if name == path {
name = ""
}
result.Packages = append(result.Packages, &configPackage{
Commands: cmds,
Name: name,
Path: path,
})
}
return result, nil
}
func convertRepositoriesToYamlModel(rootDir string, repositories []*git.Repository) (*configWorkspace, error) {
if len(repositories) == 0 {
return nil, nil
}
result := &configWorkspace{}
for _, repo := range repositories {
path, err := filepath.Rel(rootDir, repo.Dir)
if err != nil {
return nil, err
}
result.Repositories = append(result.Repositories, &configRepository{
Name: repo.Name,
Path: path,
Git: repo.Git,
})
}
return result, nil
}
func (r *configRepository) mapToExternalType(parentDir string) (*git.Repository, error) {
if r.Git == nil || r.Git.Url == "" {
return nil, errors.New("missing git.url")
}
repoName := r.Name
repoPath := r.Path
if repoPath == "" {
repoPath = git.RepoNameFromUrl(r.Git.Url)
}
if repoName == "" {
repoName = util.TrimRelativePathPrefix(repoPath)
}
repoDir := filepath.Join(parentDir, repoPath)
et := &git.Repository{
Name: repoName,
Dir: repoDir,
Git: r.Git,
}
return et, nil
}
func parseConfigFile(dir string) (*Config, error) {
for _, filename := range []string{"maestro.yaml", "maestro.yml"} {
if content, err := os.ReadFile(filepath.Join(dir, filename)); err != nil {
if os.IsNotExist(err) {
continue
} else {
return nil, err
}
} else {
return parseConfigBytes(dir, filename, content)
}
}
return &Config{Dir: dir, FileExists: false}, nil
}
func parseConfigBytes(dir string, filename string, bytes []byte) (*Config, error) {
var c config
if err := yaml.Unmarshal(bytes, &c); err != nil {
return nil, err
}
var packages []*Package
if c.Project != nil {
for cfgPkgI, cfgPkg := range c.Project.Packages {
if len(cfgPkg.Path) == 0 {
return nil, fmt.Errorf("$.project.packages[%d] missing path", cfgPkgI)
}
pkgDir := filepath.Join(dir, cfgPkg.Path)
if !util.IsDir(pkgDir) {
return nil, fmt.Errorf("$.project.packages[%d] uses non-existing path %s", cfgPkgI, cfgPkg.Path)
}
if len(cfgPkg.Commands) == 0 {
return nil, fmt.Errorf("$.project.packages[%d] missing configured commands", cfgPkgI)
}
var commands []*Command
for cfgCmdI, cfgCmd := range cfgPkg.Commands {
command, err := NewCommand(&CommandOptions{
Desc: cfgCmd.Desc,
Dir: pkgDir,
Exec: cfgCmd.Exec,
Id: cfgCmd.Id,
Name: cfgCmd.Name,
})
if err != nil {
return nil, fmt.Errorf("$.project.packages[%d].commands[%d] %s", cfgPkgI, cfgCmdI, err.Error())
} else {
commands = append(commands, command)
}
}
name := cfgPkg.Name
if len(name) == 0 {
name = util.TrimRelativePathPrefix(cfgPkg.Path)
}
packages = append(packages, &Package{
dir: pkgDir,
name: name,
commands: commands,
})
}
}
var repositories []*git.Repository
if c.Workspace != nil {
for i, r := range c.Workspace.Repositories {
if repo, err := r.mapToExternalType(dir); err != nil {
return nil, fmt.Errorf("$.workspace.repositories[%d] %s", i, err.Error())
} else {
repositories = append(repositories, repo)
}
}
}
return &Config{
Dir: dir,
FileExists: true,
Filename: filename,
Packages: packages,
Repositories: repositories,
}, nil
}