Use go-homedir (as vault does)

This commit is contained in:
Jason Felice 2017-01-30 21:39:58 -05:00 committed by Martin Atkins
parent bb8b837ab9
commit 8a7a6c6059
1 changed files with 7 additions and 4 deletions

View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"strings" "strings"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
"github.com/mitchellh/go-homedir"
) )
func Provider() terraform.ResourceProvider { func Provider() terraform.ResourceProvider {
@ -127,13 +127,16 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
token := d.Get("token").(string) token := d.Get("token").(string)
if token == "" { if token == "" {
// Use the vault CLI's token, if present. // Use the vault CLI's token, if present.
tokenFile := fmt.Sprintf("%s/.vault-token", os.Getenv("HOME")) homePath, err := homedir.Dir()
tokenBytes, err := ioutil.ReadFile(tokenFile) if err != nil {
return nil, fmt.Errorf("No vault token found: %s", err)
}
tokenBytes, err := ioutil.ReadFile(homePath + "/.vault-token")
if err != nil { if err != nil {
return nil, fmt.Errorf("No vault token found: %s", err) return nil, fmt.Errorf("No vault token found: %s", err)
} }
token = string(tokenBytes) token = strings.TrimSpace(string(tokenBytes))
} }
// In order to enforce our relatively-short lease TTL, we derive a // In order to enforce our relatively-short lease TTL, we derive a