Skip to content

Commit dab9db8

Browse files
authored
Merge pull request #102 from arduino/per1234/improve-output2
Improve error output
2 parents 9092c41 + ab81928 commit dab9db8

File tree

8 files changed

+28
-15
lines changed

8 files changed

+28
-15
lines changed

Diff for: check/checkdata/library.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
6262
url := "http://downloads.arduino.cc/libraries/library_index.json"
6363
httpResponse, err := http.Get(url)
6464
if err != nil {
65-
feedback.Errorf("%s Unable to download Library Manager index from %s", err, url)
65+
feedback.Errorf("Unable to download Library Manager index from %s: %s", err, url)
6666
os.Exit(1)
6767
}
6868
defer httpResponse.Body.Close()

Diff for: command/command.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// ArduinoCheck is the root command function.
3434
func ArduinoCheck(rootCommand *cobra.Command, cliArguments []string) {
3535
if err := configuration.Initialize(rootCommand.Flags(), cliArguments); err != nil {
36-
feedback.Errorf("Configuration error: %v", err)
36+
feedback.Errorf("Invalid configuration: %v", err)
3737
os.Exit(1)
3838
}
3939

Diff for: configuration/configuration.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package configuration
1818

1919
import (
2020
"fmt"
21+
"io/ioutil"
2122
"os"
2223
"strconv"
2324
"strings"
@@ -56,15 +57,13 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
5657
}
5758
}
5859

59-
logrus.SetOutput(defaultLogOutput)
60-
6160
if logFormatString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_FORMAT"); ok {
6261
logFormat, err := logFormatFromString(logFormatString)
6362
if err != nil {
6463
return fmt.Errorf("--log-format flag value %s not valid", logFormatString)
6564
}
6665
logrus.SetFormatter(logFormat)
67-
logrus.SetOutput(os.Stderr) // Enable log output.
66+
EnableLogging(true)
6867
}
6968

7069
if logLevelString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_LEVEL"); ok {
@@ -73,7 +72,7 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
7372
return fmt.Errorf("--log-level flag value %s not valid", logLevelString)
7473
}
7574
logrus.SetLevel(logLevel)
76-
logrus.SetOutput(os.Stderr) // Enable log output.
75+
EnableLogging(true)
7776
}
7877

7978
superprojectTypeFilterString, _ := flags.GetString("project-type")
@@ -100,15 +99,15 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
10099
// Default to using current working directory.
101100
workingDirectoryPath, err := os.Getwd()
102101
if err != nil {
103-
return fmt.Errorf("Error when setting default PROJECT_PATH argument: %s", err)
102+
panic(err)
104103
}
105104
targetPaths.Add(paths.New(workingDirectoryPath))
106105
} else {
107106
for _, projectPath := range projectPaths {
108107
targetPath := paths.New(projectPath)
109108
targetPathExists, err := targetPath.ExistCheck()
110109
if err != nil {
111-
return fmt.Errorf("Problem processing PROJECT_PATH argument value %v: %v", targetPath, err)
110+
return fmt.Errorf("Unable to process PROJECT_PATH argument value %v: %v", targetPath, err)
112111
}
113112
if !targetPathExists {
114113
return fmt.Errorf("PROJECT_PATH argument %v does not exist", targetPath)
@@ -232,3 +231,11 @@ func SchemasPath() *paths.Path {
232231
}
233232
return paths.New(executablePath).Parent().Join("etc", "schemas")
234233
}
234+
235+
func EnableLogging(enable bool) {
236+
if enable {
237+
logrus.SetOutput(defaultLogOutput) // Enable log output.
238+
} else {
239+
logrus.SetOutput(ioutil.Discard)
240+
}
241+
}

Diff for: configuration/defaults.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package configuration
1818
// The default configuration settings.
1919

2020
import (
21-
"io/ioutil"
21+
"os"
2222

2323
"github.com/arduino/arduino-check/configuration/checkmode"
2424
"github.com/arduino/arduino-check/project/projecttype"
@@ -61,4 +61,4 @@ var defaultCheckModes = map[projecttype.Type]map[checkmode.Type]bool{
6161
},
6262
}
6363

64-
var defaultLogOutput = ioutil.Discard // Default to no log output.
64+
var defaultLogOutput = os.Stderr

Diff for: main.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ import (
1919
"os"
2020

2121
"github.com/arduino/arduino-check/cli"
22+
"github.com/arduino/arduino-check/configuration"
2223
"github.com/arduino/arduino-check/result/feedback"
2324
)
2425

26+
func init() {
27+
configuration.EnableLogging(false)
28+
}
29+
2530
func main() {
2631
rootCommand := cli.Root()
2732
if err := rootCommand.Execute(); err != nil {

Diff for: project/project.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
8888
return foundProjects, nil
8989
}
9090

91-
return nil, fmt.Errorf("specified path %s is not an Arduino project", targetPath)
91+
return nil, fmt.Errorf("Specified path %s is not an Arduino project", targetPath)
9292
}
9393

9494
if configuration.SuperprojectTypeFilter() == projecttype.All || configuration.Recursive() {
@@ -110,7 +110,7 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
110110
}
111111

112112
if foundProjects == nil {
113-
return nil, fmt.Errorf("no projects found under %s", targetPath)
113+
return nil, fmt.Errorf("No projects found under %s", targetPath)
114114
}
115115

116116
return foundProjects, nil

Diff for: result/feedback/feedback.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ func Errorf(format string, v ...interface{}) {
6666
Error(fmt.Sprintf(format, v...))
6767
}
6868

69-
// Error behaves like fmt.Print but adds a newline and also logs the error.
69+
// Error behaves like fmt.Print but adds a prefix, newline and also logs the error.
7070
func Error(v ...interface{}) {
71+
fmt.Fprint(os.Stderr, "error: ")
7172
fmt.Fprintln(os.Stderr, v...)
7273
logrus.Error(fmt.Sprint(v...))
7374
}

Diff for: result/result.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ func (results Type) WriteReport() error {
236236
if !reportFilePathParentExists {
237237
err = reportFilePath.Parent().MkdirAll()
238238
if err != nil {
239-
return fmt.Errorf("Error while creating report file path (%v): %v", reportFilePath.Parent(), err)
239+
return fmt.Errorf("Unable to create report file path (%v): %v", reportFilePath.Parent(), err)
240240
}
241241
}
242242

243243
err = reportFilePath.WriteFile(results.jsonReportRaw())
244244
if err != nil {
245-
return fmt.Errorf("Error while writing report: %v", err)
245+
return fmt.Errorf("While writing report: %v", err)
246246
}
247247

248248
return nil

0 commit comments

Comments
 (0)