forked from coop-cloud/abra
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
|
package ssh
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/kevinburke/ssh_config"
|
||
|
)
|
||
|
|
||
|
// HostConfig is a SSH host config.
|
||
|
type HostConfig struct {
|
||
|
Host string
|
||
|
IdentityFile string
|
||
|
Port string
|
||
|
User string
|
||
|
}
|
||
|
|
||
|
// GetHostConfig retrieves a ~/.ssh/config config for a host.
|
||
|
func GetHostConfig(hostname string, hasIdentityFile bool) (HostConfig, error) {
|
||
|
var hostConfig HostConfig
|
||
|
|
||
|
var host, user, port, idf string
|
||
|
|
||
|
if host = ssh_config.Get(hostname, "Hostname"); host == "" {
|
||
|
return hostConfig, fmt.Errorf("SSH hostname missing for %s from SSH config", hostname)
|
||
|
}
|
||
|
|
||
|
if user = ssh_config.Get(hostname, "User"); user == "" {
|
||
|
return hostConfig, fmt.Errorf("SSH user missing for %s from SSH config", hostname)
|
||
|
}
|
||
|
|
||
|
if port = ssh_config.Get(hostname, "Port"); port == "" {
|
||
|
return hostConfig, fmt.Errorf("SSH port missing for %s from SSH config", hostname)
|
||
|
}
|
||
|
|
||
|
if idf = ssh_config.Get(hostname, "IdentityFile"); idf == "" && hasIdentityFile {
|
||
|
return hostConfig, fmt.Errorf("SSH identity file missing for %s from SSH config", hostname)
|
||
|
}
|
||
|
|
||
|
hostConfig.Host = host
|
||
|
hostConfig.IdentityFile = idf
|
||
|
hostConfig.Port = port
|
||
|
hostConfig.User = user
|
||
|
|
||
|
return hostConfig, nil
|
||
|
}
|