-
Notifications
You must be signed in to change notification settings - Fork 349
/
cli.go
188 lines (169 loc) · 5.47 KB
/
cli.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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"github.com/wata727/tflint/config"
"github.com/wata727/tflint/detector"
"github.com/wata727/tflint/loader"
"github.com/wata727/tflint/printer"
)
// Exit codes are int values that represent an exit code for a particular error.
const (
ExitCodeOK int = 0
ExitCodeError int = 1 + iota
ExitCodeIssuesFound
)
// CLI is the command line object
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
loader loader.LoaderIF
detector detector.DetectorIF
printer printer.PrinterIF
testMode bool
TestCLIOptions TestCLIOptions
}
type TestCLIOptions struct {
Config *config.Config
ConfigFile string
}
type ConfigurableArgs struct {
Debug bool
DeepCheck bool
Fast bool
AwsAccessKey string
AwsSecretKey string
AwsRegion string
IgnoreModule string
IgnoreRule string
Varfile string
ConfigFile string
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
var (
version bool
help bool
format string
errorWithIssues bool
)
// Define option flag parse
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
// Do not print default usage message
flags.SetOutput(new(bytes.Buffer))
configArgs := ConfigurableArgs{}
flags.BoolVar(&version, "version", false, "print version information.")
flags.BoolVar(&version, "v", false, "alias for -version")
flags.BoolVar(&help, "help", false, "show usage of TFLint. This page.")
flags.BoolVar(&help, "h", false, "alias for --help")
flags.BoolVar(&configArgs.Debug, "debug", false, "enable debug mode.")
flags.BoolVar(&configArgs.Debug, "d", false, "alias for --debug")
flags.StringVar(&format, "format", "default", "choose output format from \"default\" or \"json\"")
flags.StringVar(&format, "f", "default", "alias for --format")
flags.StringVar(&configArgs.IgnoreModule, "ignore-module", "", "ignore module by specified source.")
flags.StringVar(&configArgs.IgnoreRule, "ignore-rule", "", "ignore rules.")
flags.StringVar(&configArgs.ConfigFile, "config", ".tflint.hcl", "specify config file. default is \".tflint.hcl\"")
flags.StringVar(&configArgs.ConfigFile, "c", ".tflint.hcl", "alias for --config")
flags.BoolVar(&configArgs.DeepCheck, "deep", false, "enable deep check mode.")
flags.StringVar(&configArgs.AwsAccessKey, "aws-access-key", "", "AWS access key used in deep check mode.")
flags.StringVar(&configArgs.AwsSecretKey, "aws-secret-key", "", "AWS secret key used in deep check mode.")
flags.StringVar(&configArgs.AwsRegion, "aws-region", "", "AWS region used in deep check mode.")
flags.BoolVar(&errorWithIssues, "error-with-issues", false, "return error code when issue exists.")
flags.BoolVar(&configArgs.Fast, "fast", false, "ignore slow rules.")
flags.StringVar(&configArgs.Varfile, "var-file", "", "terraform variable files")
// Parse commandline flag
if err := flags.Parse(args[1:]); err != nil {
fmt.Fprintf(cli.errStream, "ERROR: `%s` is unknown options. Please run `tflint --help`\n", args[1])
return ExitCodeError
}
if !printer.ValidateFormat(format) {
fmt.Fprintf(cli.errStream, "ERROR: `%s` is unknown format. Please run `tflint --help`\n", format)
return ExitCodeError
}
// Show version
if version {
fmt.Fprintf(cli.outStream, "%s version %s\n", Name, Version)
return ExitCodeOK
}
// Show help
if help {
fmt.Fprintln(cli.outStream, Help)
return ExitCodeOK
}
// Setup config
c, err := cli.setupConfig(configArgs)
if err != nil {
fmt.Fprintln(cli.errStream, err)
return ExitCodeError
}
// Main function
// If disabled test mode, generates real loader
if !cli.testMode {
cli.loader = loader.NewLoader(c.Debug)
}
cli.loader.LoadState()
cli.loader.LoadTFVars(c.Varfile)
if flags.NArg() > 0 {
err = cli.loader.LoadTemplate(flags.Arg(0))
} else {
err = cli.loader.LoadAllTemplate(".")
}
if err != nil {
fmt.Fprintln(cli.errStream, err)
return ExitCodeError
}
// If disabled test mode, generates real detector
if !cli.testMode {
templates, state, tfvars := cli.loader.Dump()
cli.detector, err = detector.NewDetector(templates, state, tfvars, c)
}
if err != nil {
fmt.Fprintln(cli.errStream, err)
return ExitCodeError
}
issues := cli.detector.Detect()
if cli.detector.HasError() {
fmt.Fprintln(cli.errStream, "ERROR: error occurred in detecting. Please run with --debug options for details.")
return ExitCodeError
}
// If disabled test mode, generates real printer
if !cli.testMode {
cli.printer = printer.NewPrinter(cli.outStream, cli.errStream)
}
cli.printer.Print(issues, format)
if errorWithIssues && len(issues) > 0 {
return ExitCodeIssuesFound
}
return ExitCodeOK
}
func (cli *CLI) setupConfig(args ConfigurableArgs) (*config.Config, error) {
c := config.Init()
if args.Debug {
c.Debug = true
}
if err := c.LoadConfig(args.ConfigFile); err != nil {
return nil, err
}
if args.DeepCheck || c.DeepCheck {
c.DeepCheck = true
c.SetAwsCredentials(args.AwsAccessKey, args.AwsSecretKey, args.AwsRegion)
}
// `aws_instance_invalid_ami` is very slow...
if args.Fast {
c.SetIgnoreRule("aws_instance_invalid_ami")
}
c.SetIgnoreModule(args.IgnoreModule)
c.SetIgnoreRule(args.IgnoreRule)
c.SetVarfile(args.Varfile)
// If enabled test mode, set config information
if cli.testMode {
cli.TestCLIOptions = TestCLIOptions{
Config: c,
ConfigFile: args.ConfigFile,
}
}
return c, nil
}