@@ -19,20 +19,32 @@ package configs
1919
2020import (
2121 "fmt"
22+ "os"
2223 "os/user"
2324 "runtime"
2425
2526 "github.com/arduino/go-paths-helper"
2627 "github.com/arduino/go-win32-utils"
2728)
2829
30+ // getUserHomeDir returns user's home directory from $HOME or then from os/user
31+ func getUserHomeDir () string {
32+ home := os .Getenv ("HOME" )
33+ if home == "" {
34+ usr , err := user .Current ()
35+ if err != nil {
36+ panic (fmt .Errorf ("retrieving user home dir: %s" , err ))
37+ }
38+ home = usr .HomeDir
39+ }
40+ return home
41+ }
42+
2943// getDefaultConfigFilePath returns the default path for arduino-cli.yaml
3044func getDefaultConfigFilePath () * paths.Path {
31- usr , err := user .Current ()
32- if err != nil {
33- panic (fmt .Errorf ("retrieving user home dir: %s" , err ))
34- }
35- arduinoDataDir := paths .New (usr .HomeDir )
45+ userHomeDir := getUserHomeDir ()
46+
47+ arduinoDataDir := paths .New (userHomeDir )
3648
3749 switch runtime .GOOS {
3850 case "linux" :
@@ -53,11 +65,9 @@ func getDefaultConfigFilePath() *paths.Path {
5365}
5466
5567func getDefaultArduinoDataDir () (* paths.Path , error ) {
56- usr , err := user .Current ()
57- if err != nil {
58- return nil , fmt .Errorf ("retrieving user home dir: %s" , err )
59- }
60- arduinoDataDir := paths .New (usr .HomeDir )
68+ userHomeDir := getUserHomeDir ()
69+
70+ arduinoDataDir := paths .New (userHomeDir )
6171
6272 switch runtime .GOOS {
6373 case "linux" :
@@ -77,16 +87,13 @@ func getDefaultArduinoDataDir() (*paths.Path, error) {
7787}
7888
7989func getDefaultSketchbookDir () (* paths.Path , error ) {
80- usr , err := user .Current ()
81- if err != nil {
82- return nil , fmt .Errorf ("retrieving home dir: %s" , err )
83- }
90+ userHomeDir := getUserHomeDir ()
8491
8592 switch runtime .GOOS {
8693 case "linux" :
87- return paths .New (usr . HomeDir ).Join ("Arduino" ), nil
94+ return paths .New (userHomeDir ).Join ("Arduino" ), nil
8895 case "darwin" :
89- return paths .New (usr . HomeDir ).Join ("Documents" , "Arduino" ), nil
96+ return paths .New (userHomeDir ).Join ("Documents" , "Arduino" ), nil
9097 case "windows" :
9198 documentsPath , err := win32 .GetDocumentsFolder ()
9299 if err != nil {
0 commit comments