Skip to content

Commit c0d1dc6

Browse files
committed
Control logging via environment variables instead of flags
The logging functionality is only needed when debugging the code. This is not something the user will have a use for, so having command line flags adds unnecessary complexity to the UI.
1 parent e48c2be commit c0d1dc6

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@
1010

1111
After installing `arduino-check`, run the command `arduino-check --help` for usage documentation.
1212

13-
Set the `ARDUINO_CHECK_OFFICIAL` environment variable to "true" to run the checks that only apply to official Arduino
14-
projects.
13+
A few additional configuration options only of use for internal/development use of the tool can be set via environment
14+
variables:
15+
16+
- `ARDUINO_CHECK_OFFICIAL` - Set to `"true"` to run the checks that only apply to official Arduino projects.
17+
- `ARDUINO_CHECK_LOG_LEVEL` - Messages with this level and above will be logged.
18+
- Supported values: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `panic`
19+
- `ARDUINO_CHECK_LOG_FORMAT` - The output format for the logs.
20+
- Supported values: `text`, `json`

cli/cli.go

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ func Root() *cobra.Command {
3434
rootCommand.PersistentFlags().String("compliance", "specification", "Configure how strict the tool is. Can be {strict|specification|permissive}")
3535
rootCommand.PersistentFlags().String("format", "text", "The output format can be {text|json}.")
3636
rootCommand.PersistentFlags().String("library-manager", "", "Configure the checks for libraries in the Arduino Library Manager index. Can be {submit|update|false}.\nsubmit: Also run additional checks required to pass before a library is accepted for inclusion in the index.\nupdate: Also run additional checks required to pass before new releases of a library already in the index are accepted.\nfalse: Don't run any Library Manager-specific checks.")
37-
rootCommand.PersistentFlags().String("log-format", "text", "The output format for the logs, can be {text|json}.")
38-
rootCommand.PersistentFlags().String("log-level", "panic", "Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic")
3937
rootCommand.PersistentFlags().String("project-type", "all", "Only check projects of the specified type and their subprojects. Can be {sketch|library|all}.")
4038
rootCommand.PersistentFlags().Bool("recursive", true, "Search path recursively for Arduino projects to check. Can be {true|false}.")
4139
rootCommand.PersistentFlags().String("report-file", "", "Save a report on the checks to this file.")

configuration/configuration.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,23 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
5656
}
5757
}
5858

59-
logFormatString, _ := flags.GetString("log-format")
60-
logFormat, err := logFormatFromString(logFormatString)
61-
if err != nil {
62-
return fmt.Errorf("--log-format flag value %s not valid", logFormatString)
59+
if logFormatString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_FORMAT"); ok {
60+
logFormat, err := logFormatFromString(logFormatString)
61+
if err != nil {
62+
return fmt.Errorf("--log-format flag value %s not valid", logFormatString)
63+
}
64+
logrus.SetFormatter(logFormat)
6365
}
64-
logrus.SetFormatter(logFormat)
6566

66-
logLevelString, _ := flags.GetString("log-level")
67-
logLevel, err := logrus.ParseLevel(logLevelString)
68-
if err != nil {
69-
return fmt.Errorf("--log-level flag value %s not valid", logLevelString)
67+
if logLevelString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_LEVEL"); ok {
68+
logLevel, err := logrus.ParseLevel(logLevelString)
69+
if err != nil {
70+
return fmt.Errorf("--log-level flag value %s not valid", logLevelString)
71+
}
72+
logrus.SetLevel(logLevel)
73+
} else {
74+
logrus.SetLevel(defaultLogLevel)
7075
}
71-
logrus.SetLevel(logLevel)
7276

7377
superprojectTypeFilterString, _ := flags.GetString("project-type")
7478
superprojectTypeFilter, err = projecttype.FromString(superprojectTypeFilterString)
@@ -117,7 +121,6 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
117121
"output format": OutputFormat(),
118122
"Library Manager submission mode": customCheckModes[checkmode.LibraryManagerSubmission],
119123
"Library Manager update mode": customCheckModes[checkmode.LibraryManagerIndexed],
120-
"log format": logFormatString,
121124
"log level": logrus.GetLevel().String(),
122125
"superproject type filter": SuperprojectTypeFilter(),
123126
"recursive": Recursive(),

configuration/configuration_test.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/arduino/arduino-check/result/outputformat"
2525
"github.com/arduino/arduino-check/util/test"
2626
"github.com/arduino/go-paths-helper"
27+
"github.com/sirupsen/logrus"
2728
"github.com/stretchr/testify/assert"
2829
"github.com/stretchr/testify/require"
2930
)
@@ -107,16 +108,26 @@ func TestInitializeLibraryManager(t *testing.T) {
107108
}
108109

109110
func TestInitializeLogFormat(t *testing.T) {
110-
flags := test.ConfigurationFlags()
111+
os.Setenv("ARDUINO_CHECK_LOG_FORMAT", "foo")
112+
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths), "Invalid format")
111113

112-
flags.Set("log-format", "foo")
113-
assert.Error(t, Initialize(flags, projectPaths))
114+
os.Setenv("ARDUINO_CHECK_LOG_FORMAT", "text")
115+
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths), "text format")
114116

115-
flags.Set("log-format", "text")
116-
assert.Nil(t, Initialize(flags, projectPaths))
117+
os.Setenv("ARDUINO_CHECK_LOG_FORMAT", "json")
118+
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths), "json format")
119+
}
117120

118-
flags.Set("log-format", "json")
119-
assert.Nil(t, Initialize(flags, projectPaths))
121+
func TestInitializeLogLevel(t *testing.T) {
122+
require.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths))
123+
assert.Equal(t, defaultLogLevel, logrus.GetLevel(), "Default level")
124+
125+
os.Setenv("ARDUINO_CHECK_LOG_LEVEL", "foo")
126+
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths), "Invalid level")
127+
128+
os.Setenv("ARDUINO_CHECK_LOG_LEVEL", "info")
129+
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths), "Valid level")
130+
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
120131
}
121132

122133
func TestInitializeProjectType(t *testing.T) {

configuration/defaults.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package configuration
2020
import (
2121
"github.com/arduino/arduino-check/configuration/checkmode"
2222
"github.com/arduino/arduino-check/project/projecttype"
23+
"github.com/sirupsen/logrus"
2324
)
2425

2526
// Default check modes for each superproject type.
@@ -58,3 +59,5 @@ var defaultCheckModes = map[projecttype.Type]map[checkmode.Type]bool{
5859
checkmode.Official: false,
5960
},
6061
}
62+
63+
var defaultLogLevel = logrus.FatalLevel

0 commit comments

Comments
 (0)