Trying again one more time to prevent import cycles.. Moved common.ExpandUser into packer.ExpandUser..

This commit is contained in:
Ali Rizvi-Santiago 2018-12-02 18:56:33 -06:00
parent 79b68fb89c
commit 040a33567e
5 changed files with 50 additions and 48 deletions

View File

@ -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
}

View File

@ -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:])
}

View File

@ -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))

View File

@ -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)

View File

@ -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
}