diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 7a491694a..ae4ffeee7 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -1,7 +1,6 @@ package commands import ( - "fmt" "os" "github.com/codegangsta/cli" @@ -10,26 +9,8 @@ import ( // . "github.com/moul/advanced-ssh-config/pkg/logger" ) -var SSHFlags = []cli.Flag{} - func init() { config.ASSHBinary = os.Args[0] - - // Populate SSHFlags - boolFlags := []string{"1", "2", "4", "6", "A", "a", "C", "f", "G", "g", "K", "k", "M", "N", "n", "q", "s", "T", "t", "V", "v", "X", "x", "Y", "y"} - stringFlags := []string{"b", "c", "D", "E", "e", "F", "I", "i", "L", "l", "m", "O", "o", "p", "Q", "R", "S", "W", "w"} - for _, flag := range boolFlags { - SSHFlags = append(SSHFlags, cli.BoolFlag{ - Name: flag, - }) - } - for _, flag := range stringFlags { - SSHFlags = append(SSHFlags, cli.StringFlag{ - Name: flag, - Value: "", - }) - } - fmt.Println(SSHFlags) } // Commands is the list of cli commands @@ -89,6 +70,6 @@ var Commands = []cli.Command{ Name: "wrapper", Usage: "Initialize assh, then run SSH", Action: cmdWrapper, - Flags: SSHFlags, + Flags: config.SSHFlags, }, } diff --git a/pkg/commands/wrapper.go b/pkg/commands/wrapper.go index 375a72ad4..a22be4b4b 100644 --- a/pkg/commands/wrapper.go +++ b/pkg/commands/wrapper.go @@ -5,17 +5,31 @@ import ( "github.com/codegangsta/cli" + "github.com/moul/advanced-ssh-config/pkg/config" . "github.com/moul/advanced-ssh-config/pkg/logger" ) func cmdWrapper(c *cli.Context) { - /*conf, err := config.Open() + if len(c.Args()) < 1 { + Logger.Fatalf("Missing argument. See usage with 'assh wrapper -h'.") + } + + target := c.Args()[0] + command := c.Args()[1:] + options := []string{} + // FIXME: populate options + + Logger.Debugf("Wrapper called with target=%v command=%v ssh-options=%v", target, command, options) + + conf, err := config.Open() if err != nil { Logger.Fatalf("Cannot open configuration file: %v", err) - }*/ + } - Logger.Debugf("Wrapper called with %v", c.Args()) - fmt.Println(c.Args()) + fmt.Println(conf.NeedsARebuildForTarget(target)) + //host := conf.GetHostSafe(target) + //fmt.Println(host) + //fmt.Println(host.name) //conf.WriteSshConfigTo(os.Stdout) } diff --git a/pkg/config/config.go b/pkg/config/config.go index e2cd68ea3..ca839cd7a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -190,6 +190,50 @@ func (c *Config) GetHostSafe(name string) *Host { return host } +// NeedsARebuildForTarget returns true if the .ssh/config file needs to be rebuild for a specific target +func (c *Config) NeedsARebuildForTarget(target string) bool { + parts := strings.Split(target, "/") + + // compute lists + aliases := map[string]bool{} + for _, host := range c.Hosts { + for _, alias := range host.Aliases { + aliases[alias] = true + } + } + + patterns := []string{} + for origPattern, host := range c.Hosts { + patterns = append(patterns, origPattern) + patterns = append(patterns, host.Aliases...) + } + + for _, part := range parts { + // check for direct hostname matching + if _, ok := c.Hosts[part]; ok { + continue + } + + // check for direct alias matching + if _, ok := aliases[part]; ok { + continue + } + + // check for pattern matching + for _, pattern := range patterns { + matched, err := path.Match(pattern, part) + if err != nil { + continue + } + if matched { + return true + } + } + } + + return false +} + // LoadConfig loads the content of an io.Reader source func (c *Config) LoadConfig(source io.Reader) error { buf, err := ioutil.ReadAll(source)