Replaced all instances of mitchellh/go-homedir with an implementation based on os/user.

This commit is contained in:
Ali Rizvi-Santiago 2018-12-02 17:30:01 -06:00
parent 65b513fdfc
commit 1a3c3f2ffc
4 changed files with 61 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
@ -15,7 +16,6 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/hashicorp/packer/helper/useragent"
"github.com/mitchellh/go-homedir"
)
var (
@ -148,9 +148,13 @@ func tokenFromDeviceFlow(say func(string), oauthCfg adal.OAuthConfig, clientID,
// tokenCachePath returns the full path the OAuth 2.0 token should be saved at
// for given tenant ID.
func tokenCachePath(tenantID string) string {
dir, err := homedir.Dir()
if err != nil {
var dir string
u, err := user.Current()
if err != nil || u.HomeDir == "" {
dir, _ = filepath.Abs(os.Args[0])
} else {
dir = u.HomeDir
}
return filepath.Join(dir, ".azure", "packer", fmt.Sprintf("oauth-%s.json", tenantID))

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"strings"
@ -16,8 +17,6 @@ import (
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
ocicommon "github.com/oracle/oci-go-sdk/common"
"github.com/mitchellh/go-homedir"
)
type Config struct {
@ -95,11 +94,17 @@ func NewConfig(raws ...interface{}) (*Config, error) {
var keyContent []byte
if c.KeyFile != "" {
path, err := homedir.Expand(c.KeyFile)
u, err := user.Current()
if err != nil {
return nil, err
}
if u.HomeDir == "" {
return nil, fmt.Errorf("Unable to determine the home directory for the current user.")
}
path := filepath.Join(u.HomeDir, c.KeyFile)
// Read API signing key
keyContent, err = ioutil.ReadFile(path)
if err != nil {
@ -249,15 +254,19 @@ func NewConfig(raws ...interface{}) (*Config, error) {
return c, nil
}
// getDefaultOCISettingsPath uses mitchellh/go-homedir to compute the default
// getDefaultOCISettingsPath uses os/user to compute the default
// config file location ($HOME/.oci/config).
func getDefaultOCISettingsPath() (string, error) {
home, err := homedir.Dir()
u, err := user.Current()
if err != nil {
return "", err
}
path := filepath.Join(home, ".oci", "config")
if u.HomeDir == "" {
return "", fmt.Errorf("Unable to determine the home directory for the current user.")
}
path := filepath.Join(u.HomeDir, ".oci", "config")
if _, err := os.Stat(path); err != nil {
return "", err
}

View File

@ -6,6 +6,8 @@ import (
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
"time"
packerssh "github.com/hashicorp/packer/communicator/ssh"
@ -13,7 +15,6 @@ import (
helperssh "github.com/hashicorp/packer/helper/ssh"
"github.com/hashicorp/packer/template/interpolate"
"github.com/masterzen/winrm"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
@ -73,10 +74,16 @@ func (c *Config) ReadSSHPrivateKeyFile() ([]byte, error) {
var privateKey []byte
if c.SSHPrivateKeyFile != "" {
keyPath, err := homedir.Expand(c.SSHPrivateKeyFile)
u, err := user.Current()
if err != nil {
return privateKey, fmt.Errorf("Error expanding path for SSH private key: %s", err)
return []byte{}, fmt.Errorf("Error trying to determine the current user for the SSH private key: %s", err)
}
if u.HomeDir == "" {
return []byte{}, fmt.Errorf("Error locating home directory for the SSH private key")
}
keyPath := filepath.Join(u.HomeDir, c.SSHPrivateKeyFile)
privateKey, err = ioutil.ReadFile(keyPath)
if err != nil {
return privateKey, fmt.Errorf("Error on reading SSH private key: %s", err)
@ -260,8 +267,19 @@ func (c *Config) prepareSSH(ctx *interpolate.Context) []error {
errs = append(errs, errors.New("An ssh_username must be specified\n Note: some builders used to default ssh_username to \"root\"."))
}
// Figure out the current user's home directory
u, err := user.Current()
if err != nil {
errs = append(errs, fmt.Errorf("Unable to determine the current user from : %s", err))
} else if u.HomeDir == "" {
// If the home directory is empty, then set `err` so that later a failure will happen
err = fmt.Errorf("Unable to determine the home directory for the current user.")
}
if c.SSHPrivateKeyFile != "" {
path, err := homedir.Expand(c.SSHPrivateKeyFile)
path := filepath.Join(u.HomeDir, c.SSHPrivateKeyFile)
// The `err` variable here comes from an empty home directory
if err != nil {
errs = append(errs, fmt.Errorf(
"ssh_private_key_file is invalid: %s", err))
@ -279,7 +297,9 @@ 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 := homedir.Expand(c.SSHBastionPrivateKeyFile)
path := filepath.Join(u.HomeDir, c.SSHBastionPrivateKeyFile)
// The `err` variable here comes from an empty home directory
if err != nil {
errs = append(errs, fmt.Errorf(
"ssh_bastion_private_key_file is invalid: %s", err))

View File

@ -7,6 +7,8 @@ import (
"log"
"net"
"os"
"os/user"
"path/filepath"
"strings"
"time"
@ -14,7 +16,6 @@ import (
"github.com/hashicorp/packer/helper/multistep"
helperssh "github.com/hashicorp/packer/helper/ssh"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/go-homedir"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/net/proxy"
@ -178,9 +179,9 @@ func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, cancel <-chan stru
// Then we attempt to connect via SSH
config := &ssh.Config{
Connection: connFunc,
SSHConfig: sshConfig,
Pty: s.Config.SSHPty,
Connection: connFunc,
SSHConfig: sshConfig,
Pty: s.Config.SSHPty,
DisableAgentForwarding: s.Config.SSHDisableAgentForwarding,
UseSftp: s.Config.SSHFileTransferMethod == "sftp",
KeepAliveInterval: s.Config.SSHKeepAliveInterval,
@ -227,11 +228,17 @@ func sshBastionConfig(config *Config) (*gossh.ClientConfig, error) {
}
if config.SSHBastionPrivateKeyFile != "" {
path, err := homedir.Expand(config.SSHBastionPrivateKeyFile)
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf(
"Error expanding path for SSH bastion private key: %s", err)
return nil, fmt.Errorf("Unable to determine the current user for the SSH bastion private key: %s", err)
}
if u.HomeDir == "" {
return nil, fmt.Errorf("Unable to determine the current user's home directory for the SSH bastion private key.")
}
path := filepath.Join(u.HomeDir, config.SSHBastionPrivateKeyFile)
signer, err := helperssh.FileSigner(path)
if err != nil {
return nil, err