Skip to content

Commit 80e41c8

Browse files
author
Massimiliano Pippi
committed
init config file using viper
introduce viper
1 parent 3329daf commit 80e41c8

File tree

7 files changed

+129
-107
lines changed

7 files changed

+129
-107
lines changed

cli/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/arduino/arduino-cli/cli/sketch"
3838
"github.com/arduino/arduino-cli/cli/upload"
3939
"github.com/arduino/arduino-cli/cli/version"
40+
"github.com/arduino/arduino-cli/configuration"
4041
"github.com/mattn/go-colorable"
4142
"github.com/rifflock/lfshook"
4243
"github.com/sirupsen/logrus"
@@ -172,7 +173,7 @@ func preRun(cmd *cobra.Command, args []string) {
172173
// use the output format to configure the Feedback
173174
feedback.SetFormat(format)
174175

175-
globals.InitConfigs()
176+
configuration.Init()
176177

177178
logrus.Info(globals.VersionInfo.Application + "-" + globals.VersionInfo.VersionString)
178179
logrus.Info("Starting root command preparation (`arduino`)")

cli/config/init.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ package config
1717

1818
import (
1919
"os"
20+
"path/filepath"
21+
22+
"github.com/arduino/arduino-cli/configuration"
2023

2124
"github.com/arduino/arduino-cli/cli/errorcodes"
2225
"github.com/arduino/arduino-cli/cli/feedback"
23-
"github.com/arduino/arduino-cli/cli/globals"
2426
"github.com/sirupsen/logrus"
2527
"github.com/spf13/cobra"
28+
"github.com/spf13/viper"
2629
)
2730

2831
func initInitCommand() *cobra.Command {
@@ -48,20 +51,13 @@ var initFlags struct {
4851
func runInitCommand(cmd *cobra.Command, args []string) {
4952
logrus.Info("Executing `arduino config init`")
5053

51-
filepath := initFlags.location
52-
if filepath == "" {
53-
filepath = globals.Config.ConfigFile.String()
54-
}
55-
56-
if err := globals.Config.ConfigFile.Parent().MkdirAll(); err != nil {
54+
configFile := filepath.Join(configuration.GetDefaultArduinoDataDir(), "arduino-cli.yaml")
55+
err := viper.WriteConfigAs(configFile)
56+
if err != nil {
5757
feedback.Errorf("Cannot create config file: %v", err)
5858
os.Exit(errorcodes.ErrGeneric)
5959
}
6060

61-
if err := globals.Config.SaveToYAML(filepath); err != nil {
62-
feedback.Errorf("Cannot create config file: %v", err)
63-
os.Exit(errorcodes.ErrGeneric)
64-
}
65-
feedback.Print("Config file PATH: " + filepath)
61+
feedback.Print("Config file written: " + configFile)
6662
logrus.Info("Done")
6763
}

cli/globals/configs.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

configuration/configuration.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package configuration
17+
18+
import (
19+
"os"
20+
"path/filepath"
21+
"runtime"
22+
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/go-win32-utils"
25+
"github.com/sirupsen/logrus"
26+
"github.com/spf13/viper"
27+
)
28+
29+
// Init initialize defaults and read the configuration file
30+
func Init() {
31+
// Config file metadata
32+
viper.SetConfigName("arduino-cli")
33+
34+
// Add paths where to search for a config file
35+
configPath := GetDefaultArduinoDataDir()
36+
logrus.Infof("Checking for config file in: %s", configPath)
37+
viper.AddConfigPath(configPath)
38+
39+
// Set configuration defaults
40+
setDefaults()
41+
42+
// Attempt to read config file
43+
if err := viper.ReadInConfig(); err != nil {
44+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
45+
logrus.Info("Config file not found, using default values")
46+
} else {
47+
feedback.Errorf("Error reading config file: %v", err)
48+
}
49+
}
50+
51+
// Bind env vars
52+
viper.SetEnvPrefix("ARDUINO")
53+
viper.AutomaticEnv()
54+
}
55+
56+
// GetDefaultArduinoDataDir returns the full path to the default arduino folder
57+
func GetDefaultArduinoDataDir() string {
58+
userHomeDir, err := os.UserHomeDir()
59+
if err != nil {
60+
logrus.Errorf("Unable to get user home dir: %v", err)
61+
return "."
62+
}
63+
64+
switch runtime.GOOS {
65+
case "linux":
66+
return filepath.Join(userHomeDir, ".arduino15")
67+
case "darwin":
68+
return filepath.Join(userHomeDir, "Library", "Arduino15")
69+
case "windows":
70+
localAppDataPath, err := win32.GetLocalAppDataFolder()
71+
if err != nil {
72+
logrus.Errorf("Unable to get Local App Data Folder: %v", err)
73+
return "."
74+
}
75+
return filepath.Join(localAppDataPath, "Arduino15")
76+
default:
77+
return "."
78+
}
79+
}

configuration/defaults.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package configuration
17+
18+
import (
19+
"path/filepath"
20+
21+
"github.com/spf13/viper"
22+
)
23+
24+
func setDefaults() {
25+
urls := []string{"https://downloads.arduino.cc/packages/package_index.json"}
26+
viper.SetDefault("BoardManagerAdditionalUrls", urls)
27+
28+
dataDir := GetDefaultArduinoDataDir()
29+
viper.SetDefault("DataDir", dataDir)
30+
viper.SetDefault("DownloadsDir", filepath.Join(dataDir, "staging"))
31+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/sirupsen/logrus v1.4.2
3737
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
3838
github.com/spf13/cobra v0.0.5
39+
github.com/spf13/viper v1.3.2
3940
github.com/stretchr/testify v1.3.0
4041
go.bug.st/cleanup v1.0.0
4142
go.bug.st/downloader v1.1.0

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
5151
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
5252
github.com/h2non/filetype v1.0.8 h1:le8gpf+FQA0/DlDABbtisA1KiTS0Xi+YSC/E8yY3Y14=
5353
github.com/h2non/filetype v1.0.8/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
54+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
5455
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
5556
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
5657
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
@@ -64,6 +65,7 @@ github.com/juju/testing v0.0.0-20190429233213-dfc56b8c09fc h1:5xUWujf6ES9tEpFHFz
6465
github.com/juju/testing v0.0.0-20190429233213-dfc56b8c09fc/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
6566
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
6667
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
68+
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
6769
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
6870
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
6971
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
@@ -75,9 +77,11 @@ github.com/miekg/dns v1.0.5 h1:MQBGf2JEJDu0rg9WOpQZzeO+zW8UKwgkvP3R1dUU1Yw=
7577
github.com/miekg/dns v1.0.5/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
7678
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
7779
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
80+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
7881
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
7982
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 h1:Cvfd2dOlXIPTeEkOT/h8PyK4phBngOM4at9/jlgy7d4=
8083
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228/go.mod h1:MGuVJ1+5TX1SCoO2Sx0eAnjpdRytYla2uC1YIZfkC9c=
84+
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
8185
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
8286
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
8387
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -99,13 +103,17 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
99103
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
100104
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
101105
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
106+
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
102107
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
108+
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
103109
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
104110
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
105111
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
112+
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
106113
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
107114
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
108115
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
116+
github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
109117
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
110118
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
111119
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

0 commit comments

Comments
 (0)