Skip to content

Commit

Permalink
provider: Make homedir detection more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 18, 2019
1 parent ba2f696 commit a3c167a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/terraform-providers/terraform-provider-ovh
require (
github.com/hashicorp/go-cleanhttp v0.5.0
github.com/hashicorp/terraform v0.0.0-20190227065421-fc531f54a878
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014
gopkg.in/ini.v1 v1.42.0
)
39 changes: 12 additions & 27 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package ovh

import (
"fmt"
"log"
"os"
"os/user"

ini "gopkg.in/ini.v1"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/go-homedir"
ini "gopkg.in/ini.v1"
)

// Provider returns a schema.Provider for OVH.
Expand Down Expand Up @@ -96,16 +94,18 @@ func init() {
}

func configureProvider(d *schema.ResourceData) (interface{}, error) {
userHome, err := currentUserHome()
if err != nil {
log.Fatal(err)
}
config := Config{
Endpoint: d.Get("endpoint").(string),
}
configFile := fmt.Sprintf("%s/.ovh.conf", userHome)
if _, err := os.Stat(configFile); err == nil {
c, err := ini.Load(configFile)

rawPath := "~/.ovh.conf"
configPath, err := homedir.Expand(rawPath)
if err != nil {
return &config, fmt.Errorf("Failed to expand config path %q: %s", rawPath, err)
}

if _, err := os.Stat(configPath); err == nil {
c, err := ini.Load(configPath)
if err != nil {
return nil, err
}
Expand All @@ -118,6 +118,7 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
config.ApplicationSecret = section.Key("application_secret").String()
config.ConsumerKey = section.Key("consumer_key").String()
}

if v, ok := d.GetOk("application_key"); ok {
config.ApplicationKey = v.(string)
}
Expand All @@ -134,19 +135,3 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {

return &config, nil
}

// currentUserHome attempts to get current user's home directory
func currentUserHome() (string, error) {
userHome := ""
usr, err := user.Current()
if err != nil {
// Fallback by trying to read $HOME
userHome = os.Getenv("HOME")
if userHome != "" {
err = nil
}
} else {
userHome = usr.HomeDir
}
return userHome, nil
}

0 comments on commit a3c167a

Please sign in to comment.