diff --git a/cmd/sup/main.go b/cmd/sup/main.go index 46aceae..6de5ad9 100644 --- a/cmd/sup/main.go +++ b/cmd/sup/main.go @@ -3,7 +3,10 @@ package main import ( "flag" "fmt" + "io/ioutil" "os" + "os/user" + "path/filepath" "regexp" "strings" "text/tabwriter" @@ -50,7 +53,7 @@ func init() { flag.StringVar(&supfile, "f", "./Supfile", "Custom path to Supfile") flag.Var(&envVars, "e", "Set environment variables") flag.Var(&envVars, "env", "Set environment variables") - flag.StringVar(&sshConfig, "config", "", "Custom path to ssh_config file") + flag.StringVar(&sshConfig, "sshconfig", "", "Read SSH Config file, ie. ~/.ssh/config file") flag.StringVar(&onlyHosts, "only", "", "Filter hosts using regexp") flag.StringVar(&exceptHosts, "except", "", "Filter out hosts using regexp") @@ -184,6 +187,16 @@ func parseArgs(conf *sup.Supfile) (*sup.Network, []*sup.Command, error) { return &network, commands, nil } +func resolvePath(path string) string { + if path[:2] == "~/" { + usr, err := user.Current() + if err == nil { + path = filepath.Join(usr.HomeDir, path[2:]) + } + } + return path +} + func main() { flag.Parse() @@ -198,7 +211,12 @@ func main() { return } - conf, err := sup.NewSupfile(supfile) + data, err := ioutil.ReadFile(resolvePath(supfile)) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + conf, err := sup.NewSupfile(data) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -253,9 +271,9 @@ func main() { network.Hosts = hosts } - // --config flag location for ssh_config file + // --sshconfig flag location for ssh_config file if sshConfig != "" { - confHosts, err := sshconfig.ParseSSHConfig(sshConfig) + confHosts, err := sshconfig.ParseSSHConfig(resolvePath(sshConfig)) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -275,7 +293,8 @@ func main() { conf, found := confMap[host] if found { network.User = conf.User - network.IdentityFile = conf.IdentityFile + network.IdentityFile = resolvePath(conf.IdentityFile) + network.Hosts = []string{fmt.Sprintf("%s:%d", conf.HostName, conf.Port)} } } } diff --git a/supfile.go b/supfile.go index 09b338c..bedca0c 100644 --- a/supfile.go +++ b/supfile.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "os/exec" "strings" @@ -30,9 +29,9 @@ type Network struct { Hosts []string `yaml:"hosts"` Bastion string `yaml:"bastion"` // Jump host for the environment - // loaded from ssh_config - User string - IdentityFile string + // Should these live on Hosts too? We'd have to change []string to struct, even in Supfile. + User string // `yaml:"user"` + IdentityFile string // `yaml:"identity_file"` } // Networks is a list of user-defined networks @@ -256,14 +255,10 @@ func (e ErrUnsupportedSupfileVersion) Error() string { } // NewSupfile parses configuration file and returns Supfile or error. -func NewSupfile(file string) (*Supfile, error) { +func NewSupfile(data []byte) (*Supfile, error) { var conf Supfile - data, err := ioutil.ReadFile(file) - if err != nil { - return nil, err - } - err = yaml.Unmarshal(data, &conf) - if err != nil { + + if err := yaml.Unmarshal(data, &conf); err != nil { return nil, err }