Skip to content

Commit d8e15dd

Browse files
author
Giuseppe Lumia
committed
Improve search of config file
Some pieces were taken from `github.com/arduino/arduino-cli/configuration`. It would be nice in the future to export a method from arduino-cli to get its configuration directory, in order to not duplicate this logic here.
1 parent 0bae4ec commit d8e15dd

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/antihax/optional v1.0.0
77
github.com/arduino/arduino-cli v0.0.0-20210607095659-16f41352eac3
88
github.com/arduino/go-paths-helper v1.6.1
9+
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b
910
github.com/arduino/iot-client-go v1.3.4-0.20211103115604-d4d372164262
1011
github.com/gofrs/uuid v4.0.0+incompatible
1112
github.com/google/go-cmp v0.5.6

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ github.com/arduino/go-properties-orderedmap v1.3.0/go.mod h1:DKjD2VXY/NZmlingh4l
6060
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b/go.mod h1:uwGy5PpN4lqW97FiLnbcx+xx8jly5YuPMJWfVwwjJiQ=
6161
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b h1:3PjgYG5gVPA7cipp7vIR2lF96KkEJIFBJ+ANnuv6J20=
6262
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b/go.mod h1:iIPnclBMYm1g32Q5kXoqng4jLhMStReIP7ZxaoUC2y8=
63-
github.com/arduino/iot-client-go v1.3.4-0.20210930122852-04551f4cb061 h1:uQeaIHzj0tOlqnHaRskSy6UwMgQ6LIOECySpaYBCt5M=
64-
github.com/arduino/iot-client-go v1.3.4-0.20210930122852-04551f4cb061/go.mod h1:gYvpMt7Qw+OSScTLyIlCnpbvy9y96ey/2zhB4w6FoK0=
6563
github.com/arduino/iot-client-go v1.3.4-0.20211103115604-d4d372164262 h1:qVq8cdkaRPaLc9DAjY/6rH3ocs6ZvnEJtD26f5++/RU=
6664
github.com/arduino/iot-client-go v1.3.4-0.20211103115604-d4d372164262/go.mod h1:gYvpMt7Qw+OSScTLyIlCnpbvy9y96ey/2zhB4w6FoK0=
6765
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
@@ -499,17 +497,13 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
499497
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
500498
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
501499
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
502-
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c=
503-
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
504500
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3 h1:VrJZAjbekhoRn7n5FBujY31gboH+iB3pdLxn3gE9FjU=
505501
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
506502
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
507503
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
508504
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
509505
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
510506
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
511-
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f h1:Qmd2pbz05z7z6lm0DrgQVVPuBm92jqujBKMHMOlOQEw=
512-
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
513507
golang.org/x/oauth2 v0.0.0-20211028175245-ba495a64dcb5 h1:v79phzBz03tsVCUTbvTBmmC3CUXF5mKYt7DA4ZVldpM=
514508
golang.org/x/oauth2 v0.0.0-20211028175245-ba495a64dcb5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
515509
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

internal/config/config.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ package config
1919

2020
import (
2121
"fmt"
22+
"os"
23+
"path/filepath"
24+
"runtime"
2225

26+
"github.com/arduino/go-paths-helper"
27+
"github.com/arduino/go-win32-utils"
2328
"github.com/spf13/viper"
2429
)
2530

@@ -33,16 +38,61 @@ type Config struct {
3338
// Retrieve returns the actual parameters contained in the
3439
// configuration file, if any. Returns error if no config file is found.
3540
func Retrieve() (*Config, error) {
36-
conf := &Config{}
41+
configDir, err := searchConfigDir()
42+
if err != nil {
43+
return nil, fmt.Errorf("can't get config directory: %w", err)
44+
}
45+
3746
v := viper.New()
3847
v.SetConfigName(Filename)
39-
v.AddConfigPath(".")
40-
err := v.ReadInConfig()
48+
v.AddConfigPath(configDir)
49+
err = v.ReadInConfig()
4150
if err != nil {
4251
err = fmt.Errorf("%s: %w", "retrieving config file", err)
4352
return nil, err
4453
}
4554

55+
conf := &Config{}
4656
v.Unmarshal(conf)
4757
return conf, nil
4858
}
59+
60+
func searchConfigDir() (string, error) {
61+
// Search in current directory and its parents.
62+
if cwd, err := paths.Getwd(); err == nil {
63+
for _, path := range cwd.Parents() {
64+
if path.Join(Filename+".yaml").Exist() || path.Join(Filename+".json").Exist() {
65+
return path.String(), nil
66+
}
67+
}
68+
}
69+
70+
// Search in arduino-cli's default config directory.
71+
userHomeDir, err := os.UserHomeDir()
72+
if err != nil {
73+
return "", fmt.Errorf("unable to get user home dir: %w", err)
74+
}
75+
76+
var path *paths.Path
77+
switch runtime.GOOS {
78+
case "darwin":
79+
path = paths.New(filepath.Join(userHomeDir, "Library", "Arduino15"))
80+
case "windows":
81+
localAppDataPath, err := win32.GetLocalAppDataFolder()
82+
if err != nil {
83+
return "", fmt.Errorf("unable to get local app data folder: %w", err)
84+
}
85+
path = paths.New(filepath.Join(localAppDataPath, "Arduino15"))
86+
default: // linux, android, *bsd, plan9 and other Unix-like systems
87+
path = paths.New(filepath.Join(userHomeDir, ".arduino15"))
88+
}
89+
90+
if path.Join(Filename+".yaml").Exist() || path.Join(Filename+".json").Exist() {
91+
return path.String(), nil
92+
}
93+
94+
return "", fmt.Errorf(
95+
"didn't find config file in the current directory, its parents or in %s.",
96+
path.String(),
97+
)
98+
}

0 commit comments

Comments
 (0)