forked from toolshed/abra
refactor: better SSH connection details handling
This commit is contained in:
@ -19,29 +19,32 @@ type HostConfig struct {
|
||||
}
|
||||
|
||||
// GetHostConfig retrieves a ~/.ssh/config config for a host.
|
||||
func GetHostConfig(hostname string, hasIdentityFile bool) (HostConfig, error) {
|
||||
func GetHostConfig(hostname, username, port string, hasIdentityFile bool) (HostConfig, error) {
|
||||
var hostConfig HostConfig
|
||||
|
||||
var host, sshUser, port, idf string
|
||||
var host, idf string
|
||||
|
||||
if host = ssh_config.Get(hostname, "Hostname"); host == "" {
|
||||
logrus.Debugf("no hostname found in SSH config, assuming %s", hostname)
|
||||
host = hostname
|
||||
}
|
||||
|
||||
if sshUser = ssh_config.Get(hostname, "User"); sshUser == "" {
|
||||
systemUser, err := user.Current()
|
||||
if err != nil {
|
||||
return hostConfig, err
|
||||
if username == "" {
|
||||
if username = ssh_config.Get(hostname, "User"); username == "" {
|
||||
systemUser, err := user.Current()
|
||||
if err != nil {
|
||||
return hostConfig, err
|
||||
}
|
||||
logrus.Debugf("no username found in SSH config or passed on command-line, assuming %s", username)
|
||||
username = systemUser.Username
|
||||
}
|
||||
username := systemUser.Username
|
||||
logrus.Debugf("no username found in SSH config, assuming %s", username)
|
||||
sshUser = username
|
||||
}
|
||||
|
||||
if port = ssh_config.Get(hostname, "Port"); port == "" {
|
||||
logrus.Debugf("no port found in SSH config, assuming 22")
|
||||
port = "22"
|
||||
if port == "" {
|
||||
if port = ssh_config.Get(hostname, "Port"); port == "" {
|
||||
logrus.Debugf("no port found in SSH config or passed on command-line, assuming 22")
|
||||
port = "22"
|
||||
}
|
||||
}
|
||||
|
||||
dummyVal := "~/.ssh/identity"
|
||||
@ -52,7 +55,7 @@ func GetHostConfig(hostname string, hasIdentityFile bool) (HostConfig, error) {
|
||||
hostConfig.Host = host
|
||||
hostConfig.IdentityFile = idf
|
||||
hostConfig.Port = port
|
||||
hostConfig.User = sshUser
|
||||
hostConfig.User = username
|
||||
|
||||
logrus.Debugf("constructed SSH config %s for %s", hostConfig, hostname)
|
||||
|
||||
@ -60,7 +63,7 @@ func GetHostConfig(hostname string, hasIdentityFile bool) (HostConfig, error) {
|
||||
}
|
||||
|
||||
// New creates a new SSH client connection.
|
||||
func New(domainName, sshAuth string) (*simplessh.Client, error) {
|
||||
func New(domainName, sshAuth, username, port string) (*simplessh.Client, error) {
|
||||
var client *simplessh.Client
|
||||
|
||||
hasIdentityFile := true
|
||||
@ -68,7 +71,7 @@ func New(domainName, sshAuth string) (*simplessh.Client, error) {
|
||||
hasIdentityFile = false
|
||||
}
|
||||
|
||||
hostConfig, err := GetHostConfig(domainName, hasIdentityFile)
|
||||
hostConfig, err := GetHostConfig(domainName, username, port, hasIdentityFile)
|
||||
if err != nil {
|
||||
return client, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user