forked from bridgecrewio/yor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (212 loc) · 6.49 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
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: "support@bridgecrew.io"}},
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(c *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"
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),
}
options.Validate()
return tag(&options)
},
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)",
},
},
}
}
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)
tagsByGroup[group] = tagGroup.GetTags()
}
reports.ReportServiceInst.PrintTagGroupTags(tagsByGroup)
return nil
}
func tag(options *clioptions.TagOptions) error {
yorRunner := new(runner.Runner)
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)
return nil
}
func printReport(reportService *reports.ReportService, options *clioptions.TagOptions) {
reportService.CreateReport()
if options.OutputJSONFile != "" {
reportService.PrintJSONToFile(options.OutputJSONFile)
}
switch strings.ToLower(options.Output) {
case "cli":
reportService.PrintToStdout()
case "json":
reportService.PrintJSONToStdout()
default:
return
}
}