From 3374f310f83f26abece79ec6a71843ec8d7f7c47 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 19 Aug 2014 12:21:01 -0700 Subject: [PATCH] Replace implementation with Mitchell's go-homedir --- helper/ssh/provisioner.go | 35 ++-------------------------------- helper/ssh/provisioner_test.go | 25 ------------------------ 2 files changed, 2 insertions(+), 58 deletions(-) diff --git a/helper/ssh/provisioner.go b/helper/ssh/provisioner.go index a46bdcaa66..cd76803318 100644 --- a/helper/ssh/provisioner.go +++ b/helper/ssh/provisioner.go @@ -5,12 +5,11 @@ import ( "fmt" "io/ioutil" "log" - "os/user" - "strings" "time" "code.google.com/p/go.crypto/ssh" "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/go-homedir/homedir" "github.com/mitchellh/mapstructure" ) @@ -97,36 +96,6 @@ func safeDuration(dur string, defaultDur time.Duration) time.Duration { return d } -// expandUserPath replaces a leading ~ in a filepath with the path to a HOME -// directory. -// TODO: Does this work on Windows? -func expandUserPath(path string) (string, error) { - if !strings.HasPrefix(path, "~") { - return path, nil - } - i := strings.IndexByte(path, '/') - if i < 0 { - i = len(path) - } - userhome := "" - if i == 1 { - u, err := user.Current() - if err != nil { - return "", err - } - - userhome = u.HomeDir - } else { - u, err := user.Lookup(path[1:i]) - if err != nil { - return path, nil - } - userhome = u.HomeDir - } - userhome = strings.TrimSuffix(userhome, "/") - return userhome + path[i:], nil -} - // PrepareConfig is used to turn the *SSHConfig provided into a // usable *Config for client initialization. func PrepareConfig(conf *SSHConfig) (*Config, error) { @@ -134,7 +103,7 @@ func PrepareConfig(conf *SSHConfig) (*Config, error) { User: conf.User, } if conf.KeyFile != "" { - fullPath, err := expandUserPath(conf.KeyFile) + fullPath, err := homedir.Expand(conf.KeyFile) if err != nil { return nil, fmt.Errorf("Failed to expand home directory: %v", err) } diff --git a/helper/ssh/provisioner_test.go b/helper/ssh/provisioner_test.go index b9661449ae..c215e8d29e 100644 --- a/helper/ssh/provisioner_test.go +++ b/helper/ssh/provisioner_test.go @@ -1,36 +1,11 @@ package ssh import ( - "os/user" - "strings" "testing" "github.com/hashicorp/terraform/terraform" ) -func Test_expandUserPath(t *testing.T) { - path, err := expandUserPath("~/path.pem") - if err != nil { - t.Fatalf("err: %v", err) - } - u, err := user.Current() - if err != nil { - t.Fatalf("err: %v", err) - } - expected := strings.TrimSuffix(u.HomeDir, "/") + "/path.pem" - if path != expected { - t.Fatalf("bad: %v", path) - } - - path, err = expandUserPath("~" + u.Username + "/path.pem") - if err != nil { - t.Fatalf("err: %v", err) - } - if path != expected { - t.Fatalf("bad: %v, %v", path) - } -} - func TestResourceProvider_verifySSH(t *testing.T) { r := &terraform.ResourceState{ ConnInfo: map[string]string{