Skip to content

Commit f842715

Browse files
author
Massimiliano Pippi
committed
do not use logging before it's ready
1 parent 6a9e480 commit f842715

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

Diff for: cli/cli.go

+39-22
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,22 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
118118
}
119119

120120
func preRun(cmd *cobra.Command, args []string) {
121-
// before doing anything, decide whether we should log to stdout
121+
//
122+
// Prepare the configuration system
123+
//
124+
configPath := ""
125+
if configFile != "" {
126+
// override the config path if --config-file was passed
127+
configPath = filepath.Dir(configFile)
128+
}
129+
configuration.Init(configPath)
130+
configFile := viper.ConfigFileUsed()
131+
132+
//
133+
// Prepare logging
134+
//
135+
136+
// decide whether we should log to stdout
122137
if verbose {
123138
// if we print on stdout, do it in full colors
124139
logrus.SetOutput(colorable.NewColorableStdout())
@@ -129,23 +144,14 @@ func preRun(cmd *cobra.Command, args []string) {
129144
logrus.SetOutput(ioutil.Discard)
130145
}
131146

132-
// setup the configuration system
133-
configPath := ""
134-
if configFile != "" {
135-
// override the config path if --config-file was passed
136-
configPath = filepath.Dir(configFile)
137-
}
138-
configuration.Init(configPath)
139-
140-
// normalize the format strings
141-
outputFormat = strings.ToLower(outputFormat)
142-
// configure the output package
143-
output.OutputFormat = outputFormat
144-
// configure log format
147+
// set the Logger format
145148
logFormat := strings.ToLower(viper.GetString("logging.format"))
149+
if logFormat == "json" {
150+
logrus.SetFormatter(&logrus.JSONFormatter{})
151+
}
146152

147153
// should we log to file?
148-
logFile := viper.GetString("log.file")
154+
logFile := viper.GetString("logging.file")
149155
if logFile != "" {
150156
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
151157
if err != nil {
@@ -169,11 +175,14 @@ func preRun(cmd *cobra.Command, args []string) {
169175
logrus.SetLevel(lvl)
170176
}
171177

172-
// set the Logger format
173-
if logFormat == "json" {
174-
logrus.SetFormatter(&logrus.JSONFormatter{})
175-
}
178+
//
179+
// Prepare the Feedback system
180+
//
176181

182+
// normalize the format strings
183+
outputFormat = strings.ToLower(outputFormat)
184+
// configure the output package
185+
output.OutputFormat = outputFormat
177186
// check the right output format was passed
178187
format, found := parseFormatString(outputFormat)
179188
if !found {
@@ -184,10 +193,18 @@ func preRun(cmd *cobra.Command, args []string) {
184193
// use the output format to configure the Feedback
185194
feedback.SetFormat(format)
186195

187-
logrus.Info(globals.VersionInfo.Application + "-" + globals.VersionInfo.VersionString)
188-
logrus.Info("Starting root command preparation (`arduino`)")
196+
//
197+
// Print some status info and check command is consistent
198+
//
199+
200+
if configFile != "" {
201+
logrus.Infof("Using config file: %s", configFile)
202+
} else {
203+
logrus.Info("Config file not found, using default values")
204+
}
205+
206+
logrus.Info(globals.VersionInfo.Application + " version " + globals.VersionInfo.VersionString)
189207

190-
logrus.Info("Formatter set")
191208
if outputFormat != "text" {
192209
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
193210
logrus.Warn("Calling help on JSON format")

Diff for: configuration/configuration.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
"github.com/spf13/viper"
2828
)
2929

30-
// Init initialize defaults and read the configuration file
30+
// Init initialize defaults and read the configuration file.
31+
// Please note the logging system hasn't been configured yet,
32+
// so logging shouldn't be used here.
3133
func Init(configPath string) {
3234
// Config file metadata
3335
viper.SetConfigName("arduino-cli")
@@ -38,7 +40,6 @@ func Init(configPath string) {
3840
}
3941

4042
// Add paths where to search for a config file
41-
logrus.Infof("Checking for config file in: %s", configPath)
4243
viper.AddConfigPath(configPath)
4344

4445
// Bind env vars
@@ -67,9 +68,9 @@ func Init(configPath string) {
6768

6869
// Attempt to read config file
6970
if err := viper.ReadInConfig(); err != nil {
70-
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
71-
logrus.Info("Config file not found, using default values")
72-
} else {
71+
// ConfigFileNotFoundError is acceptable, anything else
72+
// should be reported to the user
73+
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
7374
feedback.Errorf("Error reading config file: %v", err)
7475
}
7576
}
@@ -79,7 +80,7 @@ func Init(configPath string) {
7980
func getDefaultArduinoDataDir() string {
8081
userHomeDir, err := os.UserHomeDir()
8182
if err != nil {
82-
logrus.Errorf("Unable to get user home dir: %v", err)
83+
feedback.Errorf("Unable to get user home dir: %v", err)
8384
return "."
8485
}
8586

@@ -91,7 +92,7 @@ func getDefaultArduinoDataDir() string {
9192
case "windows":
9293
localAppDataPath, err := win32.GetLocalAppDataFolder()
9394
if err != nil {
94-
logrus.Errorf("Unable to get Local App Data Folder: %v", err)
95+
feedback.Errorf("Unable to get Local App Data Folder: %v", err)
9596
return "."
9697
}
9798
return filepath.Join(localAppDataPath, "Arduino15")
@@ -104,7 +105,7 @@ func getDefaultArduinoDataDir() string {
104105
func getDefaultSketchbookDir() string {
105106
userHomeDir, err := os.UserHomeDir()
106107
if err != nil {
107-
logrus.Errorf("Unable to get user home dir: %v", err)
108+
feedback.Errorf("Unable to get user home dir: %v", err)
108109
return "."
109110
}
110111

@@ -116,7 +117,7 @@ func getDefaultSketchbookDir() string {
116117
case "windows":
117118
documentsPath, err := win32.GetDocumentsFolder()
118119
if err != nil {
119-
logrus.Errorf("Unable to get Documents Folder: %v", err)
120+
feedback.Errorf("Unable to get Documents Folder: %v", err)
120121
return "."
121122
}
122123
return filepath.Join(documentsPath, "Arduino")
@@ -138,18 +139,17 @@ func IsBundledInDesktopIDE() bool {
138139
logrus.Info("Checking if CLI is Bundled into the IDE")
139140
executable, err := os.Executable()
140141
if err != nil {
141-
logrus.WithError(err).Warn("Cannot get executable path")
142+
feedback.Errorf("Cannot get executable path: %v", err)
142143
return viper.GetBool("IDE.Bundled")
143144
}
144145

145146
executablePath, err := filepath.EvalSymlinks(executable)
146147
if err != nil {
147-
logrus.WithError(err).Warn("Cannot get executable path")
148+
feedback.Errorf("Cannot get executable path: %v", err)
148149
return viper.GetBool("IDE.Bundled")
149150
}
150151

151152
ideDir := filepath.Dir(executablePath)
152-
logrus.Info("Candidate IDE Directory: ", ideDir)
153153

154154
// We check an arbitrary number of folders that are part of the IDE
155155
// install tree
@@ -166,7 +166,6 @@ func IsBundledInDesktopIDE() bool {
166166
}
167167

168168
if test == "portable" {
169-
logrus.Info("IDE is portable")
170169
viper.Set("IDE.Portable", true)
171170
}
172171
}

0 commit comments

Comments
 (0)