Skip to content

Commit 80115b7

Browse files
committed
Allow setting official check mode configuration via environment variable
Since the official mode should only be used when checking official Arduino projects, I thought it was not a good idea to add a flag for this setting, since it could cause confusion to the those using the tool to check 3rd party projects, who have no use for this setting.
1 parent 6a15949 commit 80115b7

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
## Usage
1010

1111
After installing `arduino-check`, run the command `arduino-check --help` for usage documentation.
12+
13+
Set the `ARDUINO_CHECK_OFFICIAL` environment variable to "true" to run the checks that only apply to official Arduino
14+
projects.

configuration/checkmode/checkmode.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ func LibraryManagerModeFromString(libraryManagerModeString string) (bool, bool,
5151
}
5252
}
5353

54+
// OfficialModeFromString parses the ARDUINO_CHECK_OFFICIAL environment variable value and returns the corresponding check mode setting.
55+
func OfficialModeFromString(officialModeString string) (bool, error) {
56+
switch strings.ToLower(officialModeString) {
57+
case "true":
58+
return true, nil
59+
case "false":
60+
return false, nil
61+
default:
62+
return false, fmt.Errorf("No matching official check mode for string %s", officialModeString)
63+
}
64+
}
65+
5466
// Modes merges the default check mode values for the given superproject type with any user-specified check mode settings.
5567
func Modes(defaultCheckModes map[projecttype.Type]map[Type]bool, customCheckModes map[Type]bool, superprojectType projecttype.Type) map[Type]bool {
5668
checkModes := make(map[Type]bool)

configuration/configuration.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
7878
// TODO support multiple paths
7979
targetPath = paths.New(projectPaths[0])
8080

81-
// TODO: set via environment variable
82-
// customCheckModes[checkmode.Official] = false
81+
officialModeString, officialModeEnvironmentVariableDefined := os.LookupEnv("ARDUINO_CHECK_OFFICIAL")
82+
if officialModeEnvironmentVariableDefined {
83+
customCheckModes[checkmode.Official], err = checkmode.OfficialModeFromString(officialModeString)
84+
if err != nil {
85+
return fmt.Errorf("ARDUINO_CHECK_OFFICIAL environment variable value %s not valid", officialModeString)
86+
}
87+
}
8388

8489
logrus.WithFields(logrus.Fields{
8590
"output format": OutputFormat(),

configuration/configuration_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package configuration
1717

1818
import (
19+
"os"
1920
"testing"
2021

2122
"github.com/arduino/arduino-check/configuration/checkmode"
@@ -129,4 +130,15 @@ func TestInitialize(t *testing.T) {
129130
projectPaths = []string{reportFilePath.String()}
130131
assert.Nil(t, Initialize(flags, projectPaths))
131132
assert.Equal(t, reportFilePath, TargetPath())
133+
134+
os.Setenv("ARDUINO_CHECK_OFFICIAL", "true")
135+
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths))
136+
assert.True(t, customCheckModes[checkmode.Official])
137+
138+
os.Setenv("ARDUINO_CHECK_OFFICIAL", "false")
139+
assert.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths))
140+
assert.False(t, customCheckModes[checkmode.Official])
141+
142+
os.Setenv("ARDUINO_CHECK_OFFICIAL", "invalid value")
143+
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths))
132144
}

0 commit comments

Comments
 (0)