backy/pkg/ssh/ssh.go

34 lines
666 B
Go
Raw Normal View History

2022-11-22 21:09:39 +00:00
package ssh
import (
"errors"
"github.com/kevinburke/ssh_config"
)
type SshConfig struct {
PrivateKey string
Port uint
HostName string
User string
}
func GetSSHConfig(host string) (SshConfig, error) {
var config SshConfig
hostName := ssh_config.Get(host, "HostName")
if hostName == "" {
return SshConfig{}, errors.New("hostname not found")
}
config.HostName = hostName
privKey, err := ssh_config.GetStrict(host, "IdentityFile")
if err != nil {
return SshConfig{}, err
}
config.PrivateKey = privKey
User := ssh_config.Get(host, "User")
if User == "" {
return SshConfig{}, errors.New("user not found")
}
return config, nil
}