@@ -19,6 +19,7 @@ import (
19
19
"os"
20
20
"path/filepath"
21
21
"runtime"
22
+ "strings"
22
23
23
24
"github.com/arduino/arduino-cli/cli/feedback"
24
25
"github.com/arduino/go-win32-utils"
@@ -27,17 +28,21 @@ import (
27
28
)
28
29
29
30
// Init initialize defaults and read the configuration file
30
- func Init () {
31
+ func Init (configPath string ) {
31
32
// Config file metadata
32
33
viper .SetConfigName ("arduino-cli" )
33
34
35
+ // Get default data path if none was provided
36
+ if configPath == "" {
37
+ configPath = getDefaultArduinoDataDir ()
38
+ }
39
+
34
40
// Add paths where to search for a config file
35
- configPath := GetDefaultArduinoDataDir ()
36
41
logrus .Infof ("Checking for config file in: %s" , configPath )
37
42
viper .AddConfigPath (configPath )
38
43
39
44
// Set configuration defaults
40
- setDefaults ()
45
+ setDefaults (configPath , getDefaultSketchbookDir () )
41
46
42
47
// Attempt to read config file
43
48
if err := viper .ReadInConfig (); err != nil {
@@ -50,11 +55,17 @@ func Init() {
50
55
51
56
// Bind env vars
52
57
viper .SetEnvPrefix ("ARDUINO" )
58
+ viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
53
59
viper .AutomaticEnv ()
60
+
61
+ // Bind env aliases to keep backward compatibility
62
+ viper .BindEnv ("directories.Sketchbook" , "ARDUINO_SKETCHBOOK_DIR" )
63
+ viper .BindEnv ("directories.Downloads" , "ARDUINO_DOWNLOADS_DIR" )
64
+ viper .BindEnv ("directories.Data" , "ARDUINO_DATA_DIR" )
54
65
}
55
66
56
- // GetDefaultArduinoDataDir returns the full path to the default arduino folder
57
- func GetDefaultArduinoDataDir () string {
67
+ // getDefaultArduinoDataDir returns the full path to the default arduino folder
68
+ func getDefaultArduinoDataDir () string {
58
69
userHomeDir , err := os .UserHomeDir ()
59
70
if err != nil {
60
71
logrus .Errorf ("Unable to get user home dir: %v" , err )
@@ -77,3 +88,78 @@ func GetDefaultArduinoDataDir() string {
77
88
return "."
78
89
}
79
90
}
91
+
92
+ // getDefaultSketchbookDir returns the full path to the default sketchbook folder
93
+ func getDefaultSketchbookDir () string {
94
+ userHomeDir , err := os .UserHomeDir ()
95
+ if err != nil {
96
+ logrus .Errorf ("Unable to get user home dir: %v" , err )
97
+ return "."
98
+ }
99
+
100
+ switch runtime .GOOS {
101
+ case "linux" :
102
+ return filepath .Join (userHomeDir , "Arduino" )
103
+ case "darwin" :
104
+ return filepath .Join (userHomeDir , "Documents" , "Arduino" )
105
+ case "windows" :
106
+ documentsPath , err := win32 .GetDocumentsFolder ()
107
+ if err != nil {
108
+ logrus .Errorf ("Unable to get Documents Folder: %v" , err )
109
+ return "."
110
+ }
111
+ return filepath .Join (documentsPath , "Arduino" )
112
+ default :
113
+ return "."
114
+ }
115
+ }
116
+
117
+ // IsBundledInDesktopIDE returns true if the CLI is bundled with the Arduino IDE.
118
+ func IsBundledInDesktopIDE () bool {
119
+ // value is cached the first time we run the check
120
+ if viper .IsSet ("IDE.Bundled" ) {
121
+ return viper .GetBool ("IDE.Bundled" )
122
+ }
123
+
124
+ viper .Set ("IDE.Bundled" , false )
125
+ viper .Set ("IDE.Portable" , false )
126
+
127
+ logrus .Info ("Checking if CLI is Bundled into the IDE" )
128
+ executable , err := os .Executable ()
129
+ if err != nil {
130
+ logrus .WithError (err ).Warn ("Cannot get executable path" )
131
+ return viper .GetBool ("IDE.Bundled" )
132
+ }
133
+
134
+ executablePath , err := filepath .EvalSymlinks (executable )
135
+ if err != nil {
136
+ logrus .WithError (err ).Warn ("Cannot get executable path" )
137
+ return viper .GetBool ("IDE.Bundled" )
138
+ }
139
+
140
+ ideDir := filepath .Dir (executablePath )
141
+ logrus .Info ("Candidate IDE Directory: " , ideDir )
142
+
143
+ // We check an arbitrary number of folders that are part of the IDE
144
+ // install tree
145
+ tests := []string {
146
+ "tools-builder" ,
147
+ "examples/01.Basics/Blink" ,
148
+ "portable" ,
149
+ }
150
+
151
+ for _ , test := range tests {
152
+ if _ , err := os .Stat (filepath .Join (ideDir , test )); err != nil {
153
+ // the test folder doesn't exist or is not accessible
154
+ return viper .GetBool ("IDE.Bundled" )
155
+ }
156
+
157
+ if test == "portable" {
158
+ logrus .Info ("IDE is portable" )
159
+ viper .Set ("IDE.Portable" , true )
160
+ }
161
+ }
162
+
163
+ viper .Set ("IDE.Directory" , ideDir )
164
+ return true
165
+ }
0 commit comments