@@ -19,20 +19,32 @@ package configs
19
19
20
20
import (
21
21
"fmt"
22
+ "os"
22
23
"os/user"
23
24
"runtime"
24
25
25
26
"github.com/arduino/go-paths-helper"
26
27
"github.com/arduino/go-win32-utils"
27
28
)
28
29
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
+
29
43
// getDefaultConfigFilePath returns the default path for arduino-cli.yaml
30
44
func 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 )
36
48
37
49
switch runtime .GOOS {
38
50
case "linux" :
@@ -53,11 +65,9 @@ func getDefaultConfigFilePath() *paths.Path {
53
65
}
54
66
55
67
func 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 )
61
71
62
72
switch runtime .GOOS {
63
73
case "linux" :
@@ -77,16 +87,13 @@ func getDefaultArduinoDataDir() (*paths.Path, error) {
77
87
}
78
88
79
89
func 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 ()
84
91
85
92
switch runtime .GOOS {
86
93
case "linux" :
87
- return paths .New (usr . HomeDir ).Join ("Arduino" ), nil
94
+ return paths .New (userHomeDir ).Join ("Arduino" ), nil
88
95
case "darwin" :
89
- return paths .New (usr . HomeDir ).Join ("Documents" , "Arduino" ), nil
96
+ return paths .New (userHomeDir ).Join ("Documents" , "Arduino" ), nil
90
97
case "windows" :
91
98
documentsPath , err := win32 .GetDocumentsFolder ()
92
99
if err != nil {
0 commit comments