@@ -44,23 +44,9 @@ var internalOS string
44
44
var engine workflow.Engine
45
45
var globalConfiguration configuration.Configuration
46
46
var helpProvided bool
47
- var debugLogger = zerolog .New (zerolog.ConsoleWriter {
48
- Out : os .Stderr ,
49
- TimeFormat : time .RFC3339 ,
50
- NoColor : true ,
51
- PartsOrder : []string {
52
- zerolog .TimestampFieldName ,
53
- "ext" ,
54
- "separator" ,
55
- zerolog .CallerFieldName ,
56
- zerolog .MessageFieldName ,
57
- },
58
- FieldsExclude : []string {"ext" , "separator" },
59
- FormatTimestamp : func (i interface {}) string {
60
- t , _ := time .Parse (time .RFC3339 , i .(string ))
61
- return strings .ToUpper (fmt .Sprintf ("%s" , t .UTC ().Format (time .RFC3339 )))
62
- },
63
- }).With ().Str ("ext" , "main" ).Str ("separator" , "-" ).Timestamp ().Logger ()
47
+
48
+ var noopLogger zerolog.Logger = zerolog .New (io .Discard )
49
+ var globalLogger * zerolog.Logger = & noopLogger
64
50
65
51
const (
66
52
unknownCommandMessage string = "unknown command"
@@ -81,32 +67,6 @@ const (
81
67
handleErrorUnhandled HandleError = iota
82
68
)
83
69
84
- func getDebugLevel (config configuration.Configuration ) zerolog.Level {
85
- loglevel := zerolog .DebugLevel
86
- if loglevelString := config .GetString ("snyk_log_level" ); loglevelString != "" {
87
- var err error
88
- loglevel , err = zerolog .ParseLevel (loglevelString )
89
- if err == nil {
90
- debugLogger .Log ().Msgf ("Setting log level to %s" , loglevelString )
91
- } else {
92
- debugLogger .Log ().Msgf ("%v" , err )
93
- loglevel = zerolog .DebugLevel
94
- }
95
- }
96
- return loglevel
97
- }
98
-
99
- func initDebugLogger (config configuration.Configuration ) * zerolog.Logger {
100
- debug := config .GetBool (configuration .DEBUG )
101
- if ! debug {
102
- debugLogger = debugLogger .Output (io .Discard )
103
- } else {
104
- loglevel := getDebugLevel (config )
105
- debugLogger = debugLogger .Level (loglevel )
106
- }
107
- return & debugLogger
108
- }
109
-
110
70
func main () {
111
71
errorCode := MainWithErrorCode ()
112
72
os .Exit (errorCode )
@@ -132,7 +92,7 @@ func initApplicationConfiguration(config configuration.Configuration) {
132
92
formattedKey := strings .ToUpper (key )
133
93
_ , ok := os .LookupEnv (formattedKey )
134
94
if ok {
135
- debugLogger .Printf ("Found environment variable %s, disabling OAuth flow" , formattedKey )
95
+ globalLogger .Printf ("Found environment variable %s, disabling OAuth flow" , formattedKey )
136
96
config .Set (configuration .FF_OAUTH_AUTH_FLOW_ENABLED , false )
137
97
break
138
98
}
@@ -187,21 +147,21 @@ func runMainWorkflow(config configuration.Configuration, cmd *cobra.Command, arg
187
147
188
148
err := config .AddFlagSet (cmd .Flags ())
189
149
if err != nil {
190
- debugLogger .Print ("Failed to add flags" , err )
150
+ globalLogger .Print ("Failed to add flags" , err )
191
151
return err
192
152
}
193
153
194
154
updateConfigFromParameter (config , args , rawArgs )
195
155
196
156
name := getFullCommandString (cmd )
197
- debugLogger .Print ("Running " , name )
157
+ globalLogger .Print ("Running " , name )
198
158
engine .GetAnalytics ().SetCommand (name )
199
159
200
160
data , err := engine .Invoke (workflow .NewWorkflowIdentifier (name ))
201
161
if err == nil {
202
162
_ , err = engine .InvokeWithInput (localworkflows .WORKFLOWID_OUTPUT_WORKFLOW , data )
203
163
} else {
204
- debugLogger .Print ("Failed to execute the command!" , err )
164
+ globalLogger .Print ("Failed to execute the command!" , err )
205
165
}
206
166
207
167
return err
@@ -391,14 +351,16 @@ func MainWithErrorCode() int {
391
351
globalConfiguration = configuration .New ()
392
352
err = globalConfiguration .AddFlagSet (rootCommand .LocalFlags ())
393
353
if err != nil {
394
- debugLogger . Print ( "Failed to add flags to root command" , err )
354
+ fmt . Fprintln ( os . Stderr , "Failed to add flags to root command" , err )
395
355
}
396
356
357
+ // ensure to init configuration before using it
358
+ initApplicationConfiguration (globalConfiguration )
359
+
397
360
debugEnabled := globalConfiguration .GetBool (configuration .DEBUG )
398
- debugLogger : = initDebugLogger (globalConfiguration )
361
+ globalLogger = initDebugLogger (globalConfiguration )
399
362
400
- initApplicationConfiguration (globalConfiguration )
401
- engine = app .CreateAppEngineWithOptions (app .WithZeroLogger (debugLogger ), app .WithConfiguration (globalConfiguration ), app .WithRuntimeInfo (rInfo ))
363
+ engine = app .CreateAppEngineWithOptions (app .WithZeroLogger (globalLogger ), app .WithConfiguration (globalConfiguration ), app .WithRuntimeInfo (rInfo ))
402
364
403
365
if noProxyAuth := globalConfiguration .GetBool (basic_workflows .PROXY_NOAUTH ); noProxyAuth {
404
366
globalConfiguration .Set (configuration .PROXY_AUTHENTICATION_MECHANISM , httpauth .StringFromAuthenticationMechanism (httpauth .NoAuth ))
@@ -416,7 +378,7 @@ func MainWithErrorCode() int {
416
378
// init engine
417
379
err = engine .Init ()
418
380
if err != nil {
419
- debugLogger .Print ("Failed to init Workflow Engine!" , err )
381
+ globalLogger .Print ("Failed to init Workflow Engine!" , err )
420
382
return constants .SNYK_EXIT_CODE_ERROR
421
383
}
422
384
@@ -449,7 +411,7 @@ func MainWithErrorCode() int {
449
411
cliAnalytics .SetCmdArguments (os .Args [1 :])
450
412
cliAnalytics .SetOperatingSystem (internalOS )
451
413
if globalConfiguration .GetBool (configuration .ANALYTICS_DISABLED ) == false {
452
- defer sendAnalytics (cliAnalytics , debugLogger )
414
+ defer sendAnalytics (cliAnalytics , globalLogger )
453
415
}
454
416
455
417
setTimeout (globalConfiguration , func () {
@@ -462,7 +424,7 @@ func MainWithErrorCode() int {
462
424
// fallback to the legacy cli or show help
463
425
handleErrorResult := handleError (err )
464
426
if handleErrorResult == handleErrorFallbackToLegacyCLI {
465
- debugLogger .Printf ("Using Legacy CLI to serve the command. (reason: %v)" , err )
427
+ globalLogger .Printf ("Using Legacy CLI to serve the command. (reason: %v)" , err )
466
428
err = defaultCmd (os .Args [1 :])
467
429
} else if handleErrorResult == handleErrorShowHelp {
468
430
err = help (nil , []string {})
@@ -475,7 +437,7 @@ func MainWithErrorCode() int {
475
437
displayError (err )
476
438
477
439
exitCode := cliv2 .DeriveExitCode (err )
478
- debugLogger .Printf ("Exiting with %d" , exitCode )
440
+ globalLogger .Printf ("Exiting with %d" , exitCode )
479
441
480
442
return exitCode
481
443
}
@@ -485,7 +447,7 @@ func setTimeout(config configuration.Configuration, onTimeout func()) {
485
447
if timeout == 0 {
486
448
return
487
449
}
488
- debugLogger .Printf ("Command timeout set for %d seconds" , timeout )
450
+ globalLogger .Printf ("Command timeout set for %d seconds" , timeout )
489
451
go func () {
490
452
const gracePeriodForSubProcesses = 3
491
453
<- time .After (time .Duration (timeout + gracePeriodForSubProcesses ) * time .Second )
0 commit comments