Skip to content

Commit

Permalink
Merge pull request #124 from pressly/pxue
Browse files Browse the repository at this point in the history
Follow-up for #105
  • Loading branch information
VojtechVitek authored Jan 16, 2018
2 parents 1d7a1fa + 76e79f4 commit 2bdad7e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
29 changes: 24 additions & 5 deletions cmd/sup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)}
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions supfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 2bdad7e

Please sign in to comment.