From 040a33567e6e9043a56ba54bee33316d5ab1479d Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sun, 2 Dec 2018 18:56:33 -0600 Subject: [PATCH] Trying again one more time to prevent import cycles.. Moved common.ExpandUser into packer.ExpandUser.. --- builder/oracle/oci/config.go | 2 +- common/config.go | 42 ----------------------- helper/communicator/config.go | 8 ++--- helper/communicator/step_connect_ssh.go | 2 +- packer/config_file.go | 44 +++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 48 deletions(-) diff --git a/builder/oracle/oci/config.go b/builder/oracle/oci/config.go index dd2beaaba..d83fa6c15 100644 --- a/builder/oracle/oci/config.go +++ b/builder/oracle/oci/config.go @@ -94,7 +94,7 @@ func NewConfig(raws ...interface{}) (*Config, error) { var keyContent []byte if c.KeyFile != "" { - path, err := common.ExpandUser(c.KeyFile) + path, err := packer.ExpandUser(c.KeyFile) if err != nil { return nil, err } diff --git a/common/config.go b/common/config.go index 9dffab23c..70ebc8452 100644 --- a/common/config.go +++ b/common/config.go @@ -4,7 +4,6 @@ import ( "fmt" "net/url" "os" - "os/user" "path/filepath" "regexp" "runtime" @@ -207,44 +206,3 @@ func FileExistsLocally(original string) bool { } return true } - -func ExpandUser(path string) (string, error) { - var ( - u *user.User - err error - ) - - // refuse to do anything with a zero-length path - if len(path) == 0 { - return path - } - - // If no expansion was specified, then refuse that too - if path[0] != "~" { - return path, nil - } - - // Grab everything up to the first filepath.Separator - idx := strings.IndexAny(path, `/\`) - if idx == -1 { - idx = len(path) - } - - // Now we should be able to extract the username - username := path[:idx] - - // Check if the current user was requested - if username == "~" { - u, err = user.Current() - } else { - u, err = user.Lookup(username[1:]) - } - - // If we couldn't figure that out, then fail here - if err != nil { - return "", err - } - - // Now we can replace the path with u.HomeDir - return filepath.Join(u.HomeDir, path[idx:]) -} diff --git a/helper/communicator/config.go b/helper/communicator/config.go index 6c0477bf5..7d66254b5 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -8,10 +8,10 @@ import ( "os" "time" - "github.com/hashicorp/packer/common" packerssh "github.com/hashicorp/packer/communicator/ssh" "github.com/hashicorp/packer/helper/multistep" helperssh "github.com/hashicorp/packer/helper/ssh" + "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" "github.com/masterzen/winrm" "golang.org/x/crypto/ssh" @@ -73,7 +73,7 @@ func (c *Config) ReadSSHPrivateKeyFile() ([]byte, error) { var privateKey []byte if c.SSHPrivateKeyFile != "" { - keyPath, err := common.ExpandUser(c.SSHPrivateKeyFile) + keyPath, err := packer.ExpandUser(c.SSHPrivateKeyFile) if err != nil { return []byte{}, fmt.Errorf("Error expanding path for SSH private key: %s", err) } @@ -262,7 +262,7 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error { } if c.SSHPrivateKeyFile != "" { - path, err := common.ExpandUser(c.SSHPrivateKeyFile) + path, err := packer.ExpandUser(c.SSHPrivateKeyFile) if err != nil { errs = append(errs, fmt.Errorf( "ssh_private_key_file is invalid: %s", err)) @@ -280,7 +280,7 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error { errs = append(errs, errors.New( "ssh_bastion_password or ssh_bastion_private_key_file must be specified")) } else if c.SSHBastionPrivateKeyFile != "" { - path, err := common.ExpandUser(c.SSHBastionPrivateKeyFile) + path, err := packer.ExpandUser(c.SSHBastionPrivateKeyFile) if err != nil { errs = append(errs, fmt.Errorf( "ssh_bastion_private_key_file is invalid: %s", err)) diff --git a/helper/communicator/step_connect_ssh.go b/helper/communicator/step_connect_ssh.go index 121a7c307..0a2a8c757 100644 --- a/helper/communicator/step_connect_ssh.go +++ b/helper/communicator/step_connect_ssh.go @@ -226,7 +226,7 @@ func sshBastionConfig(config *Config) (*gossh.ClientConfig, error) { } if config.SSHBastionPrivateKeyFile != "" { - path, err := common.ExpandUser(config.SSHBastionPrivateKeyFile) + path, err := packer.ExpandUser(config.SSHBastionPrivateKeyFile) if err != nil { return nil, fmt.Errorf( "Error expanding path for SSH bastion private key: %s", err) diff --git a/packer/config_file.go b/packer/config_file.go index 043b40ae8..3bb9c8e51 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -5,6 +5,7 @@ import ( "os" "os/user" "path/filepath" + "strings" ) // ConfigFile returns the default path to the configuration file. On @@ -84,3 +85,46 @@ func configDir() (string, error) { return filepath.Join(dir, defaultConfigDir), nil } + +// Given a path, check to see if it's using ~ to reference a user directory. +// If so, then replace that component with the requrested user's directory. +func ExpandUser(path string) (string, error) { + var ( + u *user.User + err error + ) + + // refuse to do anything with a zero-length path + if len(path) == 0 { + return path, nil + } + + // If no expansion was specified, then refuse that too + if path[0] != '~' { + return path, nil + } + + // Grab everything up to the first filepath.Separator + idx := strings.IndexAny(path, `/\`) + if idx == -1 { + idx = len(path) + } + + // Now we should be able to extract the username + username := path[:idx] + + // Check if the current user was requested + if username == "~" { + u, err = user.Current() + } else { + u, err = user.Lookup(username[1:]) + } + + // If we couldn't figure that out, then fail here + if err != nil { + return "", err + } + + // Now we can replace the path with u.HomeDir + return filepath.Join(u.HomeDir, path[idx:]), nil +}