Skip to content

Commit f785e1c

Browse files
committed
Inherit configuration from parent folders
It starts from the root and for every folder until $PWD it parses the yaml. Files closer to $PWD take precedence
1 parent 74f9f6a commit f785e1c

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

configs/navigate.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package configs
22

33
import (
4-
"fmt"
4+
"path/filepath"
5+
"strings"
56

67
paths "github.com/arduino/go-paths-helper"
78
homedir "github.com/mitchellh/go-homedir"
89
)
910

1011
func Navigate(root, pwd string) Configuration {
11-
fmt.Println("Navigate", root, pwd)
12+
relativePath, err := filepath.Rel(root, pwd)
13+
if err != nil {
14+
panic(err)
15+
}
16+
1217
home, err := homedir.Dir()
1318
if err != nil {
1419
panic(err) // Should never happen
@@ -20,8 +25,14 @@ func Navigate(root, pwd string) Configuration {
2025
DataDir: paths.New(home, ".arduino15"),
2126
}
2227

23-
// Search for arduino-cli.yaml in current folder
24-
_ = config.LoadFromYAML(paths.New(pwd, "arduino-cli.yaml"))
28+
// From the root to the current folder, search for arduino-cli.yaml files
29+
parts := strings.Split(relativePath, string(filepath.Separator))
30+
for i := range parts {
31+
path := paths.New(root)
32+
path = path.Join(parts[:i+1]...)
33+
path = path.Join("arduino-cli.yaml")
34+
_ = config.LoadFromYAML(path)
35+
}
2536

2637
return config
2738
}

configs/navigate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestNavigate(t *testing.T) {
1515
tests := []string{
1616
"noconfig",
1717
"local",
18+
"inheritance",
1819
}
1920
for _, tt := range tests {
2021
t.Run(tt, func(t *testing.T) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sketchbook_path: /tmp
2+
board_manager:
3+
additional_urls:
4+
- https://downloads.arduino.cc/package_index_mraa.json
5+

configs/testdata/navigate/inheritance/first/second/.gitkeep

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
board_manager:
2+
additional_urls:
3+
- https://downloads.arduino.cc/package_index_mraa.json
4+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
proxy_type: ""
2+
sketchbook_path: /tmp
3+
arduino_data: $HOME/.arduino15
4+
board_manager:
5+
additional_urls:
6+
- https://downloads.arduino.cc/package_index_mraa.json

configs/yaml_serializer.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (config *Configuration) SerializeToYAML() ([]byte, error) {
112112
if len(config.BoardManagerAdditionalUrls) > 0 {
113113
c.BoardsManager = &yamlBoardsManagerConfig{AdditionalURLS: []string{}}
114114
for _, URL := range config.BoardManagerAdditionalUrls {
115-
c.BoardsManager.AdditionalURLS = append(c.BoardsManager.AdditionalURLS, URL.String())
115+
c.BoardsManager.AdditionalURLS = appendIfMissing(c.BoardsManager.AdditionalURLS, URL.String())
116116
}
117117
}
118118
return yaml.Marshal(c)
@@ -130,3 +130,12 @@ func (config *Configuration) SaveToYAML(path string) error {
130130
}
131131
return nil
132132
}
133+
134+
func appendIfMissing(slice []string, i string) []string {
135+
for _, ele := range slice {
136+
if ele == i {
137+
return slice
138+
}
139+
}
140+
return append(slice, i)
141+
}

0 commit comments

Comments
 (0)