Use os/user for home directory

This was PR feedback from @justinvp, and ensures we're consistent
with how we retrive the user's home directory elsewhere.
This commit is contained in:
joeduffy 2018-02-20 15:35:07 -08:00
parent 88dcdd8d2b
commit b3c3c0797b
3 changed files with 14 additions and 15 deletions

2
Gopkg.lock generated
View file

@ -485,6 +485,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "8100b89b567aad8154da62fbce99ae8e56aed20d6a04b2427f68863c22ed59c3"
inputs-digest = "1de7bd262e1e77d3cc0d7d0bba2a51e124c40a6b7e18f0fcc0566121b364f068"
solver-name = "gps-cdcl"
solver-version = 1

View file

@ -4,11 +4,10 @@ package workspace
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"github.com/pkg/errors"
)
@ -73,13 +72,13 @@ type Credentials struct {
func getCredsFilePath() (string, error) {
user, err := user.Current()
if user == nil || err != nil {
return "", fmt.Errorf("getting creds file path: failed to get current user")
return "", errors.Wrapf(err, "getting creds file path: failed to get current user")
}
pulumiFolder := path.Join(user.HomeDir, BookkeepingDir)
pulumiFolder := filepath.Join(user.HomeDir, BookkeepingDir)
err = os.MkdirAll(pulumiFolder, 0700)
if err != nil {
return "", fmt.Errorf("failed to create '%s'", pulumiFolder)
return "", errors.Wrapf(err, "failed to create '%s'", pulumiFolder)
}
// If we are running as part of unit tests, we want to save/restore a different set
@ -89,7 +88,7 @@ func getCredsFilePath() (string, error) {
credentialsFile = "alt-credentials.json"
}
return path.Join(pulumiFolder, credentialsFile), nil
return filepath.Join(pulumiFolder, credentialsFile), nil
}
// GetStoredCredentials returns any credentials stored on the local machine.
@ -104,12 +103,12 @@ func GetStoredCredentials() (Credentials, error) {
if os.IsNotExist(err) {
return Credentials{}, nil
}
return Credentials{}, errors.Wrapf(err, "reading '%s': %v", credsFile)
return Credentials{}, errors.Wrapf(err, "reading '%s'", credsFile)
}
var creds Credentials
if err = json.Unmarshal(c, &creds); err != nil {
return Credentials{}, fmt.Errorf("unmarshalling credentials file: %v", err)
return Credentials{}, errors.Wrapf(err, "unmarshalling credentials file")
}
return creds, nil
}
@ -132,7 +131,7 @@ func StoreCredentials(creds Credentials) error {
raw, err := json.MarshalIndent(creds, "", " ")
if err != nil {
return fmt.Errorf("marshalling credentials object: %v", err)
return errors.Wrapf(err, "marshalling credentials object")
}
return ioutil.WriteFile(credsFile, raw, 0600)
}

View file

@ -10,6 +10,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"runtime"
@ -18,7 +19,6 @@ import (
"github.com/blang/semver"
"github.com/djherbis/times"
"github.com/golang/glog"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/util/contract"
@ -214,11 +214,11 @@ func HasPlugin(plug PluginInfo) bool {
// GetPluginDir returns the directory in which plugins on the current machine are managed.
func GetPluginDir() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", err
u, err := user.Current()
if u == nil || err != nil {
return "", errors.Wrapf(err, "getting user home directory")
}
return filepath.Join(home, BookkeepingDir, PluginDir), nil
return filepath.Join(u.HomeDir, BookkeepingDir, PluginDir), nil
}
// GetPlugins returns a list of installed plugins.