@@ -24,14 +24,15 @@ type CLI struct {
24
24
// outStream and errStream are the stdout and stderr
25
25
// to write message from the CLI.
26
26
outStream , errStream io.Writer
27
+ loader loader.LoaderIF
28
+ detector detector.DetectorIF
29
+ printer printer.PrinterIF
27
30
testMode bool
28
31
TestCLIOptions TestCLIOptions
29
32
}
30
33
31
34
type TestCLIOptions struct {
32
35
Config * config.Config
33
- Format string
34
- LoadFile string
35
36
ConfigFile string
36
37
}
37
38
@@ -116,45 +117,52 @@ func (cli *CLI) Run(args []string) int {
116
117
if ignoreRule != "" {
117
118
c .SetIgnoreRule (ignoreRule )
118
119
}
119
-
120
+ // If enabled test mode, set config infomation
120
121
if cli .testMode {
121
122
cli .TestCLIOptions = TestCLIOptions {
122
123
Config : c ,
123
- Format : format ,
124
- LoadFile : flags .Arg (0 ),
125
124
ConfigFile : configFile ,
126
125
}
126
+ }
127
+
128
+ // Main function
129
+ var err error
130
+ // If disabled test mode, generates real loader
131
+ if ! cli .testMode {
132
+ cli .loader = loader .NewLoader (c .Debug )
133
+ }
134
+ if flags .NArg () > 0 {
135
+ err = cli .loader .LoadFile (flags .Arg (0 ))
127
136
} else {
128
- // Main function
129
- var err error
130
- l := loader .NewLoader (c .Debug )
131
- if flags .NArg () > 0 {
132
- err = l .LoadFile (flags .Arg (0 ))
133
- } else {
134
- err = l .LoadAllFile ("." )
135
- }
136
- if err != nil {
137
- fmt .Fprintln (cli .errStream , err )
138
- return ExitCodeError
139
- }
137
+ err = cli .loader .LoadAllFile ("." )
138
+ }
139
+ if err != nil {
140
+ fmt .Fprintln (cli .errStream , err )
141
+ return ExitCodeError
142
+ }
140
143
141
- d , err := detector .NewDetector (l .ListMap , c )
142
- if err != nil {
143
- fmt .Fprintln (cli .errStream , err )
144
- return ExitCodeError
145
- }
146
- issues := d .Detect ()
147
- if d .Error {
148
- fmt .Fprintln (cli .errStream , "ERROR: error occurred in detecting. Please run with --debug options for details." )
149
- return ExitCodeError
150
- }
144
+ // If disabled test mode, generates real detector
145
+ if ! cli .testMode {
146
+ cli .detector , err = detector .NewDetector (cli .loader .DumpFiles (), c )
147
+ }
148
+ if err != nil {
149
+ fmt .Fprintln (cli .errStream , err )
150
+ return ExitCodeError
151
+ }
152
+ issues := cli .detector .Detect ()
153
+ if cli .detector .HasError () {
154
+ fmt .Fprintln (cli .errStream , "ERROR: error occurred in detecting. Please run with --debug options for details." )
155
+ return ExitCodeError
156
+ }
151
157
152
- p := printer .NewPrinter (cli .outStream , cli .errStream )
153
- p .Print (issues , format )
158
+ // If disabled test mode, generates real printer
159
+ if ! cli .testMode {
160
+ cli .printer = printer .NewPrinter (cli .outStream , cli .errStream )
161
+ }
162
+ cli .printer .Print (issues , format )
154
163
155
- if errorWithIssues && len (issues ) > 0 {
156
- return ExitCodeIssuesFound
157
- }
164
+ if errorWithIssues && len (issues ) > 0 {
165
+ return ExitCodeIssuesFound
158
166
}
159
167
160
168
return ExitCodeOK
0 commit comments