Skip to content

Commit

Permalink
Add an helper to determine if .ssh/config is outdated (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Apr 8, 2016
1 parent 351bf8d commit 71edc86
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
21 changes: 1 addition & 20 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"fmt"
"os"

"github.com/codegangsta/cli"
Expand All @@ -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
Expand Down Expand Up @@ -89,6 +70,6 @@ var Commands = []cli.Command{
Name: "wrapper",
Usage: "Initialize assh, then run SSH",
Action: cmdWrapper,
Flags: SSHFlags,
Flags: config.SSHFlags,
},
}
22 changes: 18 additions & 4 deletions pkg/commands/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <target> 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)
}
44 changes: 44 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 71edc86

Please sign in to comment.