-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (174 loc) · 4.76 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/davidae/mono-meta/cmd"
"github.com/davidae/mono-meta/mono"
)
// Flags
var (
file string
local string
url string
buildCMD string
clonePath string
servicePath string
compare string
base string
ref string
)
var (
monoConfig mono.Config
repoConfig RepoConfig
root *cobra.Command
diff *cobra.Command
services *cobra.Command
)
// RepoConfig holds all configuration required for retrieve a repo remotely and locally
type RepoConfig struct {
Path string `json:"local,omitempty"`
URL string `json:"url,omitempty"`
}
func init() {
root = &cobra.Command{Use: "mono-meta", TraverseChildren: true, Args: cobra.ExactValidArgs(1)}
assignFlags := func(f *pflag.FlagSet) {
f.StringVarP(&file, "file", "f", "", "load configuration file - it will override any existing values set by a flag with same \"key\"")
f.StringVarP(&local, "local", "l", "", "local path to a git repository")
f.StringVarP(&url, "url", "u", "", "remote URL to a git repository")
f.StringVarP(&buildCMD, "build-cmd", "e", "go build -o $1", "go build command for all services, a '$1' variable of the outputted binary is required")
f.StringVarP(&servicePath, "services", "s", "", "path (pattern) where the microservices resides in the monorepo")
}
diff = cmd.Diff()
assignFlags(diff.Flags())
diff.Flags().StringVarP(&base, "base", "b", "master", "branch to be used, as the base, in the comparison of the diff in the monorepo")
diff.Flags().StringVarP(&compare, "compare", "c", "", "branch to be used to compare with the base in the monorepo")
diff.MarkFlagRequired("compare")
diff.MarkFlagRequired("services")
services = cmd.Services()
assignFlags(services.Flags())
services.Flags().StringVarP(&ref, "branch", "b", "master", "branch to be used for the summary of all services in the monorepo")
services.MarkFlagRequired("services")
}
func main() {
diff.Run = func(cmd *cobra.Command, args []string) {
err := loadConfig(file, &monoConfig, &repoConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return
}
d, err := getDiff(monoConfig, repoConfig, base, compare)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return
}
out, err := toJSON(true, d)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return
}
fmt.Fprint(os.Stdout, out)
}
services.Run = func(cmd *cobra.Command, args []string) {
err := loadConfig(file, &monoConfig, &repoConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return
}
s, err := getServices(monoConfig, repoConfig, ref)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return
}
out, err := toJSON(true, s)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return
}
fmt.Fprint(os.Stdout, out)
}
root.AddCommand(services, diff)
root.Execute()
}
func toJSON(pretty bool, resp interface{}) (string, error) {
if pretty {
data, err := json.MarshalIndent(resp, " ", " ")
if err != nil {
return "", err
}
return string(data), nil
}
data, err := json.Marshal(resp)
if err != nil {
return "", err
}
return string(data), nil
}
func loadConfig(config string, m *mono.Config, r *RepoConfig) error {
monoConfig.ServicePath = servicePath
monoConfig.BuildCMD = buildCMD
repoConfig.URL = url
repoConfig.Path = local
if config != "" {
d, err := ioutil.ReadFile(config)
if err != nil {
return err
}
if err := json.Unmarshal(d, m); err != nil {
return err
}
if err := json.Unmarshal(d, r); err != nil {
return err
}
}
if repoConfig.URL == "" && repoConfig.Path == "" {
return errors.New("--url or --local flag must be set")
}
return monoConfig.Validate()
}
func getDiff(mCfg mono.Config, rCfg RepoConfig, base, compare string) ([]*mono.ServiceDiff, error) {
m, r, err := getMeta(mCfg, rCfg)
if err != nil {
return []*mono.ServiceDiff{}, err
}
defer r.Close()
diffs, err := m.Diff(base, compare)
if err != nil {
return []*mono.ServiceDiff{}, err
}
return diffs, nil
}
func getServices(mCfg mono.Config, rCfg RepoConfig, ref string) ([]*mono.Service, error) {
m, r, err := getMeta(mCfg, rCfg)
if err != nil {
return []*mono.Service{}, err
}
defer r.Close()
services, err := m.Services(ref)
if err != nil {
return []*mono.Service{}, err
}
return services, nil
}
func getMeta(mCfg mono.Config, rCfg RepoConfig) (*mono.Meta, mono.Repository, error) {
var (
r mono.Repository
err error
)
if rCfg.URL == "" {
r, err = mono.NewLocal(rCfg.Path)
if err != nil {
return nil, nil, err
}
} else {
r, err = mono.NewRemote(rCfg.URL)
if err != nil {
return nil, nil, err
}
}
return mono.NewMeta(r, mCfg), r, nil
}