Skip to content

Commit

Permalink
Add --include-wildcards for sshc get, allowing the user to retrieve w…
Browse files Browse the repository at this point in the history
…ildcard-matching definitions
  • Loading branch information
delucks committed Jan 4, 2019
1 parent e0ddf0d commit 34bfee2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions sshc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ var (
JSONOutput bool
UseANSIColor bool
RegexIgnoreCase bool
IncludeWildcards bool
)

func FindSpecificHost(c *ssh_config.Config, alias string) *ssh_config.Host {
for _, host := range NonWildcardHosts(c) {
func FindSpecificHost(c *ssh_config.Config, alias string, includeWilcards bool) *ssh_config.Host {
for _, host := range filterHosts(c, includeWilcards) {
if host.Matches(alias) {
return host
}
}
return nil
}

func NonWildcardHosts(c *ssh_config.Config) []*ssh_config.Host {
func filterHosts(c *ssh_config.Config, includeWildcards bool) []*ssh_config.Host {
var filtered []*ssh_config.Host
for _, host := range c.Hosts {
if !strings.ContainsRune(host.Patterns[0].String(), '*') {
if strings.ContainsRune(host.Patterns[0].String(), '*') {
if len(host.Nodes) == 0 {
// This is the implicit * block added by the parser at the beginning of the file. Skip it to avoid distorting results
continue
}
if includeWildcards {
filtered = append(filtered, host)
}
} else {
filtered = append(filtered, host)
}
}
Expand Down Expand Up @@ -149,7 +158,7 @@ func RunCopy(hostname string, remote_uri string) error {
if err != nil {
return err
}
hostdef := FindSpecificHost(cfg, hostname)
hostdef := FindSpecificHost(cfg, hostname, false)
if hostdef == nil {
return ColorError("Host argument doesn't match any definitions in your SSH config file", Red)
}
Expand Down Expand Up @@ -268,7 +277,7 @@ func RunGet(hostname string) error {
if err != nil {
return err
}
hostdef := FindSpecificHost(cfg, hostname)
hostdef := FindSpecificHost(cfg, hostname, IncludeWildcards)
if hostdef == nil {
return ColorError("No matching hosts", Red)
}
Expand Down Expand Up @@ -314,6 +323,7 @@ func NewGetCommand() *cobra.Command {
},
}
get.Flags().BoolVarP(&JSONOutput, "json-output", "j", false, "Output this host in JSON format")
get.Flags().BoolVarP(&IncludeWildcards, "include-wildcards", "w", false, "Include wildcard host-definitions (which typically match everything)")
return get
}

Expand Down

0 comments on commit 34bfee2

Please sign in to comment.