Skip to content

Commit bbce93e

Browse files
authored
Merge pull request #19 from thbkrkr/make-user-current-portable
Fallback to get current home directory
2 parents 9ad914d + 4f25f0c commit bbce93e

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

ovh/provider.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ func init() {
7373
}
7474

7575
func configureProvider(d *schema.ResourceData) (interface{}, error) {
76-
usr, err := user.Current()
76+
userHome, err := currentUserHome()
7777
if err != nil {
7878
log.Fatal(err)
7979
}
8080
config := Config{
8181
Endpoint: d.Get("endpoint").(string),
8282
}
83-
configFile := fmt.Sprintf("%s/.ovh.conf", usr.HomeDir)
83+
configFile := fmt.Sprintf("%s/.ovh.conf", userHome)
8484
if _, err := os.Stat(configFile); err == nil {
8585
c, err := ini.Load(configFile)
8686
if err != nil {
@@ -111,3 +111,19 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
111111

112112
return &config, nil
113113
}
114+
115+
// currentUserHome attempts to get current user's home directory
116+
func currentUserHome() (string, error) {
117+
userHome := ""
118+
usr, err := user.Current()
119+
if err != nil {
120+
// Fallback by trying to read $HOME
121+
userHome = os.Getenv("HOME")
122+
if userHome != "" {
123+
err = nil
124+
}
125+
} else {
126+
userHome = usr.HomeDir
127+
}
128+
return userHome, nil
129+
}

0 commit comments

Comments
 (0)