From 525072c77f875ae122a64662bcd72e33f8e3006c Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 18 Mar 2019 10:54:47 +0000 Subject: [PATCH] provider: Make homedir detection more robust --- go.mod | 1 + ovh/provider.go | 39 ++++++++++++--------------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 383876604..f833460bc 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/ovh/provider.go b/ovh/provider.go index a09d9b539..0592f56bb 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -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. @@ -101,16 +99,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 } @@ -123,6 +123,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) } @@ -144,19 +145,3 @@ func deprecated(r *schema.Resource, msg string) *schema.Resource { r.DeprecationMessage = msg return r } - -// 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 -}