Skip to content

Commit ae52737

Browse files
author
Massimiliano Pippi
committed
look for project wide config file
1 parent da3cd7f commit ae52737

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Diff for: cli/cli.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24+
"path"
2425
"path/filepath"
2526
"strings"
2627

@@ -117,15 +118,43 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
117118
return f, found
118119
}
119120

121+
// This function is here to replicate the old logic looking for a config
122+
// file in the parent tree of the CWD, aka "project config".
123+
// Please
124+
func searchConfigTree(cwd string) string {
125+
// go back up to root and search for the config file
126+
for {
127+
if _, err := os.Stat(path.Join(cwd, "arduino-cli.yaml")); os.IsNotExist(err) {
128+
// no config file found
129+
next := path.Join(cwd, "..")
130+
if filepath.Clean(next) == filepath.Clean(cwd) {
131+
return ""
132+
}
133+
cwd = next
134+
} else {
135+
return cwd
136+
}
137+
}
138+
}
139+
120140
func preRun(cmd *cobra.Command, args []string) {
121141
//
122142
// Prepare the configuration system
123143
//
124144
configPath := ""
145+
146+
// get cwd, if something is wrong don't do anything and let
147+
// configuration init proceed
148+
if cwd, err := os.Getwd(); err == nil {
149+
configPath = searchConfigTree(cwd)
150+
}
151+
152+
// override the config path if --config-file was passed
125153
if configFile != "" {
126-
// override the config path if --config-file was passed
127154
configPath = filepath.Dir(configFile)
128155
}
156+
157+
// initialize the config system
129158
configuration.Init(configPath)
130159
configFile := viper.ConfigFileUsed()
131160

Diff for: cli/cli_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,25 @@ func TestCoreCommandsIntegration(t *testing.T) {
535535
require.Zero(t, exitCode)
536536
require.Contains(t, string(d), AVR+" uninstalled")
537537
}
538+
539+
func TestSearchConfigTreeNotFound(t *testing.T) {
540+
tmp := tmpDirOrDie()
541+
require.Empty(t, searchConfigTree(tmp))
542+
}
543+
544+
func TestSearchConfigTreeSameFolder(t *testing.T) {
545+
tmp := tmpDirOrDie()
546+
_, err := os.Create(filepath.Join(tmp, "arduino-cli.yaml"))
547+
require.Nil(t, err)
548+
require.Equal(t, searchConfigTree(tmp), tmp)
549+
}
550+
551+
func TestSearchConfigTreeInParent(t *testing.T) {
552+
tmp := tmpDirOrDie()
553+
target := filepath.Join(tmp, "foo", "bar")
554+
err := os.MkdirAll(target, os.ModePerm)
555+
require.Nil(t, err)
556+
_, err = os.Create(filepath.Join(tmp, "arduino-cli.yaml"))
557+
require.Nil(t, err)
558+
require.Equal(t, searchConfigTree(target), tmp)
559+
}

0 commit comments

Comments
 (0)