@@ -17,7 +17,6 @@ package cli
17
17
18
18
import (
19
19
"fmt"
20
- "io/ioutil"
21
20
"os"
22
21
"strings"
23
22
@@ -37,7 +36,6 @@ import (
37
36
"github.com/arduino/arduino-cli/cli/lib"
38
37
"github.com/arduino/arduino-cli/cli/monitor"
39
38
"github.com/arduino/arduino-cli/cli/outdated"
40
- "github.com/arduino/arduino-cli/cli/output"
41
39
"github.com/arduino/arduino-cli/cli/sketch"
42
40
"github.com/arduino/arduino-cli/cli/update"
43
41
"github.com/arduino/arduino-cli/cli/updater"
@@ -47,17 +45,14 @@ import (
47
45
"github.com/arduino/arduino-cli/configuration"
48
46
"github.com/arduino/arduino-cli/i18n"
49
47
"github.com/arduino/arduino-cli/inventory"
50
- "github.com/fatih/color"
51
- "github.com/mattn/go-colorable"
52
- "github.com/rifflock/lfshook"
48
+ "github.com/arduino/arduino-cli/logging"
49
+ "github.com/arduino/arduino-cli/output"
53
50
"github.com/sirupsen/logrus"
54
51
"github.com/spf13/cobra"
55
52
semver "go.bug.st/relaxed-semver"
56
53
)
57
54
58
55
var (
59
- verbose bool
60
- outputFormat string
61
56
configFile string
62
57
updaterMessageChan chan * semver.Version = make (chan * semver.Version )
63
58
)
@@ -104,57 +99,34 @@ func createCliCommandTree(cmd *cobra.Command) {
104
99
cmd .AddCommand (burnbootloader .NewCommand ())
105
100
cmd .AddCommand (version .NewCommand ())
106
101
107
- cmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , tr ("Print the logs on the standard output." ))
108
102
validLogLevels := []string {"trace" , "debug" , "info" , "warn" , "error" , "fatal" , "panic" }
109
- cmd .PersistentFlags ().String ("log-level" , "" , tr ("Messages with this level and above will be logged. Valid levels are: %s" , strings .Join (validLogLevels , ", " )))
103
+ validLogFormats := []string {"text" , "json" }
104
+ cmd .PersistentFlags ().String ("log-level" , "info" , tr ("Messages with this level and above will be logged. Valid levels are: %s" , strings .Join (validLogLevels , ", " )))
105
+ cmd .PersistentFlags ().String ("log-file" , "" , tr ("Path to the file where logs will be written." ))
106
+ cmd .PersistentFlags ().String ("log-format" , "text" , tr ("The output format for the logs, can be: %s" , strings .Join (validLogFormats , ", " )))
110
107
cmd .RegisterFlagCompletionFunc ("log-level" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
111
108
return validLogLevels , cobra .ShellCompDirectiveDefault
112
109
})
113
- cmd .PersistentFlags ().String ("log-file" , "" , tr ("Path to the file where logs will be written." ))
114
- validLogFormats := []string {"text" , "json" }
115
- cmd .PersistentFlags ().String ("log-format" , "" , tr ("The output format for the logs, can be: %s" , strings .Join (validLogFormats , ", " )))
116
110
cmd .RegisterFlagCompletionFunc ("log-format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
117
111
return validLogFormats , cobra .ShellCompDirectiveDefault
118
112
})
119
113
validOutputFormats := []string {"text" , "json" , "jsonmini" , "yaml" }
120
- cmd .PersistentFlags ().StringVar (& outputFormat , "format" , "text" , tr ("The output format for the logs, can be: %s" , strings .Join (validOutputFormats , ", " )))
114
+ cmd .PersistentFlags ().BoolP ("verbose" , "v" , false , tr ("Print the logs on the standard output." ))
115
+ cmd .PersistentFlags ().String ("format" , "text" , tr ("The output format for the logs, can be: %s" , strings .Join (validOutputFormats , ", " )))
116
+ cmd .PersistentFlags ().Bool ("no-color" , false , "Disable colored output." )
121
117
cmd .RegisterFlagCompletionFunc ("format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
122
118
return validOutputFormats , cobra .ShellCompDirectiveDefault
123
119
})
124
120
cmd .PersistentFlags ().StringVar (& configFile , "config-file" , "" , tr ("The custom config file (if not specified the default will be used)." ))
125
121
cmd .PersistentFlags ().StringSlice ("additional-urls" , []string {}, tr ("Comma-separated list of additional URLs for the Boards Manager." ))
126
- cmd .PersistentFlags ().Bool ("no-color" , false , "Disable colored output." )
127
122
configuration .BindFlags (cmd , configuration .Settings )
128
123
}
129
124
130
- // convert the string passed to the `--log-level` option to the corresponding
131
- // logrus formal level.
132
- func toLogLevel (s string ) (t logrus.Level , found bool ) {
133
- t , found = map [string ]logrus.Level {
134
- "trace" : logrus .TraceLevel ,
135
- "debug" : logrus .DebugLevel ,
136
- "info" : logrus .InfoLevel ,
137
- "warn" : logrus .WarnLevel ,
138
- "error" : logrus .ErrorLevel ,
139
- "fatal" : logrus .FatalLevel ,
140
- "panic" : logrus .PanicLevel ,
141
- }[s ]
142
-
143
- return
144
- }
145
-
146
- func parseFormatString (arg string ) (feedback.OutputFormat , bool ) {
147
- f , found := map [string ]feedback.OutputFormat {
148
- "json" : feedback .JSON ,
149
- "jsonmini" : feedback .JSONMini ,
150
- "text" : feedback .Text ,
151
- "yaml" : feedback .YAML ,
152
- }[strings .ToLower (arg )]
153
-
154
- return f , found
155
- }
156
-
157
125
func preRun (cmd * cobra.Command , args []string ) {
126
+ if cmd .Name () == "daemon" {
127
+ return
128
+ }
129
+
158
130
configFile := configuration .Settings .ConfigFileUsed ()
159
131
160
132
// initialize inventory
@@ -164,12 +136,13 @@ func preRun(cmd *cobra.Command, args []string) {
164
136
os .Exit (errorcodes .ErrBadArgument )
165
137
}
166
138
167
- // https://no-color.org/
168
- color .NoColor = configuration .Settings .GetBool ("output.no_color" ) || os .Getenv ("NO_COLOR" ) != ""
169
-
170
- // Set default feedback output to colorable
171
- feedback .SetOut (colorable .NewColorableStdout ())
172
- feedback .SetErr (colorable .NewColorableStderr ())
139
+ outputFormat , err := cmd .Flags ().GetString ("format" )
140
+ if err != nil {
141
+ feedback .Errorf (tr ("Error getting flag value: %s" , err ))
142
+ os .Exit (errorcodes .ErrBadCall )
143
+ }
144
+ noColor := configuration .Settings .GetBool ("output.no_color" ) || os .Getenv ("NO_COLOR" ) != ""
145
+ output .Setup (outputFormat , noColor )
173
146
174
147
updaterMessageChan = make (chan * semver.Version )
175
148
go func () {
@@ -185,70 +158,19 @@ func preRun(cmd *cobra.Command, args []string) {
185
158
updaterMessageChan <- updater .CheckForUpdate (currentVersion )
186
159
}()
187
160
188
- //
189
- // Prepare logging
190
- //
191
-
192
- // decide whether we should log to stdout
193
- if verbose {
194
- // if we print on stdout, do it in full colors
195
- logrus .SetOutput (colorable .NewColorableStdout ())
196
- logrus .SetFormatter (& logrus.TextFormatter {
197
- ForceColors : true ,
198
- DisableColors : color .NoColor ,
199
- })
200
- } else {
201
- logrus .SetOutput (ioutil .Discard )
202
- }
203
-
204
- // set the Logger format
205
- logFormat := strings .ToLower (configuration .Settings .GetString ("logging.format" ))
206
- if logFormat == "json" {
207
- logrus .SetFormatter (& logrus.JSONFormatter {})
208
- }
209
-
210
- // should we log to file?
211
- logFile := configuration .Settings .GetString ("logging.file" )
212
- if logFile != "" {
213
- file , err := os .OpenFile (logFile , os .O_CREATE | os .O_WRONLY | os .O_APPEND , 0666 )
214
- if err != nil {
215
- fmt .Println (tr ("Unable to open file for logging: %s" , logFile ))
216
- os .Exit (errorcodes .ErrBadCall )
217
- }
218
-
219
- // we use a hook so we don't get color codes in the log file
220
- if logFormat == "json" {
221
- logrus .AddHook (lfshook .NewHook (file , & logrus.JSONFormatter {}))
222
- } else {
223
- logrus .AddHook (lfshook .NewHook (file , & logrus.TextFormatter {}))
224
- }
225
- }
226
-
227
- // configure logging filter
228
- if lvl , found := toLogLevel (configuration .Settings .GetString ("logging.level" )); ! found {
229
- feedback .Errorf (tr ("Invalid option for --log-level: %s" ), configuration .Settings .GetString ("logging.level" ))
230
- os .Exit (errorcodes .ErrBadArgument )
231
- } else {
232
- logrus .SetLevel (lvl )
233
- }
234
-
235
- //
236
- // Prepare the Feedback system
237
- //
238
-
239
- // normalize the format strings
240
- outputFormat = strings .ToLower (outputFormat )
241
- // configure the output package
242
- output .OutputFormat = outputFormat
243
- // check the right output format was passed
244
- format , found := parseFormatString (outputFormat )
245
- if ! found {
246
- feedback .Errorf (tr ("Invalid output format: %s" ), outputFormat )
161
+ // Setups logging if necessary
162
+ verbose , err := cmd .Flags ().GetBool ("verbose" )
163
+ if err != nil {
164
+ feedback .Errorf (tr ("Error getting flag value: %s" , err ))
247
165
os .Exit (errorcodes .ErrBadCall )
248
166
}
249
-
250
- // use the output format to configure the Feedback
251
- feedback .SetFormat (format )
167
+ logging .Setup (
168
+ verbose ,
169
+ noColor ,
170
+ configuration .Settings .GetString ("logging.level" ),
171
+ configuration .Settings .GetString ("logging.file" ),
172
+ configuration .Settings .GetString ("logging.format" ),
173
+ )
252
174
253
175
//
254
176
// Print some status info and check command is consistent
0 commit comments