-
Notifications
You must be signed in to change notification settings - Fork 123
/
main.go
313 lines (299 loc) · 9.32 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/bridgecrewio/yor/src/common"
"github.com/bridgecrewio/yor/src/common/clioptions"
"github.com/bridgecrewio/yor/src/common/logger"
"github.com/bridgecrewio/yor/src/common/reports"
"github.com/bridgecrewio/yor/src/common/runner"
"github.com/bridgecrewio/yor/src/common/tagging"
"github.com/bridgecrewio/yor/src/common/tagging/tags"
"github.com/bridgecrewio/yor/src/common/tagging/utils"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "yor",
HelpName: "",
Usage: "enrich IaC files with tags automatically",
Version: common.Version,
Description: "Yor, the IaC auto-tagger",
Compiled: time.Time{},
Authors: []*cli.Author{{Name: "Bridgecrew", Email: "[email protected]"}},
UseShortOptionHandling: true,
Commands: []*cli.Command{
listTagsCommand(),
listTagGroupsCommand(),
tagCommand(),
},
}
err := app.Run(os.Args)
if err != nil {
logger.Error(err.Error())
}
}
func listTagGroupsCommand() *cli.Command {
return &cli.Command{
Name: "list-tag-groups",
Usage: "List the tag groups that will be applied by yor",
Action: func(_ *cli.Context) error {
return listTagGroups()
},
}
}
func listTagsCommand() *cli.Command {
tagGroupsArg := "tag-groups"
return &cli.Command{
Name: "list-tags",
Usage: "List the tags yor will create if possible",
Action: func(c *cli.Context) error {
listTagsOptions := clioptions.ListTagsOptions{
// cli package doesn't split comma separated values
TagGroups: c.StringSlice(tagGroupsArg),
}
listTagsOptions.Validate()
return listTags(&listTagsOptions)
},
Flags: []cli.Flag{ // When adding flags, make sure they are supported in the GitHub action as well via entrypoint.sh
&cli.StringSliceFlag{
Name: tagGroupsArg,
Aliases: []string{"g"},
Usage: "Filter results by specific tag group(s), comma delimited",
Value: cli.NewStringSlice(utils.GetAllTagGroupsNames()...),
DefaultText: strings.Join(utils.GetAllTagGroupsNames(), ","),
},
},
HideHelpCommand: true,
UseShortOptionHandling: true,
}
}
func tagCommand() *cli.Command {
directoryArg := "directory"
tagArg := "tags"
skipTagsArg := "skip-tags"
customTaggingArg := "custom-tagging"
skipDirsArg := "skip-dirs"
outputArg := "output"
tagGroupArg := "tag-groups"
outputJSONFileArg := "output-json-file"
externalConfPath := "config-file"
skipResourceTypesArg := "skip-resource-types"
skipResourcesArg := "skip-resources"
parsersArgs := "parsers"
dryRunArgs := "dry-run"
validateModeArgs := "validate"
tagLocalModules := "tag-local-modules"
tagPrefix := "tag-prefix"
noColor := "no-color"
useCodeowners := "use-code-owners"
nonRecursiveArgs := "non-recursive"
return &cli.Command{
Name: "tag",
Usage: "apply tagging across your directory",
HideHelpCommand: true,
UseShortOptionHandling: true,
Action: func(c *cli.Context) error {
options := clioptions.TagOptions{
Directory: c.String(directoryArg),
Tag: c.StringSlice(tagArg),
SkipTags: c.StringSlice(skipTagsArg),
CustomTagging: c.StringSlice(customTaggingArg),
SkipDirs: c.StringSlice(skipDirsArg),
Output: c.String(outputArg),
OutputJSONFile: c.String(outputJSONFileArg),
TagGroups: c.StringSlice(tagGroupArg),
ConfigFile: c.String(externalConfPath),
SkipResourceTypes: c.StringSlice(skipResourceTypesArg),
SkipResources: c.StringSlice(skipResourcesArg),
Parsers: c.StringSlice(parsersArgs),
DryRun: c.Bool(dryRunArgs),
ValidateMode: c.Bool(validateModeArgs),
TagLocalModules: c.Bool(tagLocalModules),
TagPrefix: c.String(tagPrefix),
NoColor: c.Bool(noColor),
UseCodeOwners: c.Bool(useCodeowners),
NonRecursive: c.Bool(nonRecursiveArgs),
}
options.Validate()
colors := common.NoColorCheck(options.NoColor)
return tag(&options, colors)
},
Flags: []cli.Flag{ // When adding flags, make sure they are supported in the GitHub action as well via entrypoint.sh
&cli.StringFlag{
Name: directoryArg,
Aliases: []string{"d"},
Usage: "directory to tag",
Required: true,
DefaultText: "path/to/iac/root",
},
&cli.StringSliceFlag{
Name: tagArg,
Aliases: []string{"t"},
Usage: "run yor only with the specified tags",
DefaultText: "yor_trace,git_repository",
},
&cli.StringSliceFlag{
Name: skipTagsArg,
Aliases: []string{"s"},
Usage: "run yor skipping the specified tags",
Value: cli.NewStringSlice(),
DefaultText: "yor_trace",
},
&cli.StringFlag{
Name: outputArg,
Aliases: []string{"o"},
Usage: "set output format",
Value: "cli",
DefaultText: "json",
},
&cli.StringFlag{
Name: outputJSONFileArg,
Usage: "json file path for output",
DefaultText: "result.json",
},
&cli.StringSliceFlag{
Name: customTaggingArg,
Aliases: []string{"c"},
Usage: "paths to custom tag groups and tags plugins",
Value: cli.NewStringSlice(),
DefaultText: "path/to/custom/yor/tagging",
},
&cli.StringSliceFlag{
Name: skipDirsArg,
Aliases: nil,
Usage: "configuration paths to skip",
Value: cli.NewStringSlice(),
DefaultText: "path/to/skip,another/path/to/skip",
},
&cli.StringSliceFlag{
Name: tagGroupArg,
Aliases: []string{"g"},
Usage: "Narrow down results to the matching tag groups",
Value: cli.NewStringSlice(utils.GetAllTagGroupsNames()...),
DefaultText: "git,code2cloud",
},
&cli.StringFlag{
Name: externalConfPath,
Usage: "external tag group configuration file path",
DefaultText: "/path/to/conf/file/ (.yml/.yaml extension)",
},
&cli.StringSliceFlag{
Name: skipResourceTypesArg,
Usage: "skip resource types for tagging",
Value: cli.NewStringSlice(),
DefaultText: "aws_rds_instance,AWS::S3::Bucket",
},
&cli.StringSliceFlag{
Name: skipResourcesArg,
Usage: "skip resources for tagging",
Value: cli.NewStringSlice(),
DefaultText: "aws_s3_bucket.test-bucket,EC2InstanceResource0",
},
&cli.StringSliceFlag{
Name: parsersArgs,
Aliases: []string{"i"},
Usage: "IAC types to tag",
Value: cli.NewStringSlice("Terraform", "CloudFormation", "Serverless"),
DefaultText: "Terraform,CloudFormation,Serverless",
},
&cli.BoolFlag{
Name: dryRunArgs,
Usage: "skip resource tagging",
Value: false,
DefaultText: "false",
},
&cli.BoolFlag{
Name: validateModeArgs,
Usage: "dry-run and exit with error if changes made/needed",
Value: false,
DefaultText: "false",
},
&cli.BoolFlag{
Name: tagLocalModules,
Usage: "Always tag local modules",
Value: false,
DefaultText: "false",
},
&cli.StringFlag{
Name: tagPrefix,
Usage: "Add prefix to all the tags",
DefaultText: "",
},
&cli.BoolFlag{
Name: noColor,
Usage: "remove colorized output",
Value: false,
DefaultText: "false",
},
&cli.BoolFlag{
Name: useCodeowners,
Usage: "use code owners file to tag team",
Value: false,
DefaultText: "false",
},
&cli.BoolFlag{
Name: nonRecursiveArgs,
Usage: "non recursive tagging",
Value: false,
DefaultText: "false",
},
},
}
}
func listTagGroups() error {
for _, tagGroup := range utils.GetAllTagGroupsNames() {
fmt.Println(tagGroup)
}
return nil
}
func listTags(options *clioptions.ListTagsOptions) error {
var tagGroup tagging.ITagGroup
tagsByGroup := make(map[string][]tags.ITag)
for _, group := range options.TagGroups {
tagGroup = utils.TagGroupsByName(utils.TagGroupName(group))
if tagGroup == nil {
return fmt.Errorf("tag group %v is not supported", group)
}
tagGroup.InitTagGroup("", nil, nil)
tagsByGroup[group] = tagGroup.GetTags()
}
reports.ReportServiceInst.PrintTagGroupTags(tagsByGroup)
return nil
}
func tag(options *clioptions.TagOptions, colors *common.ColorStruct) error {
yorRunner := new(runner.Runner)
if options.ValidateMode {
options.DryRun = true
}
logger.Info(fmt.Sprintf("Setting up to tag the directory %v\n", options.Directory))
err := yorRunner.Init(options)
if err != nil {
logger.Error(err.Error())
}
reportService, err := yorRunner.TagDirectory()
if err != nil {
logger.Error(err.Error())
}
printReport(reportService, options, colors)
if options.ValidateMode && reportService.Changed() {
logger.Error("Changes needed and ValidateMode is true.")
}
return nil
}
func printReport(reportService *reports.ReportService, options *clioptions.TagOptions, colors *common.ColorStruct) {
reportService.CreateReport()
if options.OutputJSONFile != "" {
reportService.PrintJSONToFile(options.OutputJSONFile)
}
switch strings.ToLower(options.Output) {
case "cli":
reportService.PrintToStdout(colors)
case "json":
reportService.PrintJSONToStdout()
default:
return
}
}