Skip to content

Commit

Permalink
Handle multiple hosts per line
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Feb 11, 2022
1 parent dff7b8a commit af04fda
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,33 @@ func runGenerate(cmd *cobra.Command, args []string) {
}

data := string(bytes)
rx := regexp.MustCompile(`^((\[(?P<HostWithPort>.*?)\]:(?P<Port>\d+))|((?P<DomainName>.*?),(?P<IP>.*?))|(?P<Host>.*?))[ ]`)
rx := regexp.MustCompile(`^(\[(?P<Host>.*?)\]:(?P<Port>\d+))|(?P<SingleHost>.*?)$`)

lines := strings.Split(data, "\n")
for _, line := range lines {
if line == "" {
continue
}

config := NewKnownHostConfig()
targets := strings.Split(strings.Split(line, " ")[0], ",")
for _, target := range targets {
config := NewKnownHostConfig()

matches := rx.FindStringSubmatch(line)
matches := rx.FindStringSubmatch(target)

if host := matches[rx.SubexpIndex("HostWithPort")]; host != "" {
config.Host = host
config.HostName = host
config.Port = matches[rx.SubexpIndex("Port")]
} else if host := matches[rx.SubexpIndex("DomainName")]; host != "" {
config.Host = host
config.HostName = matches[rx.SubexpIndex("IP")]
} else if host := matches[rx.SubexpIndex("Host")]; host != "" {
config.Host = host
config.HostName = host
}
if host := matches[rx.SubexpIndex("Host")]; host != "" {
port := matches[rx.SubexpIndex("Port")]

config.Host = host + ":" + port
config.HostName = host
config.Port = port
} else if host := matches[rx.SubexpIndex("SingleHost")]; host != "" {
config.Host = host
config.HostName = host
}

configs = append(configs, config)
configs = append(configs, config)
}
}
}

Expand Down

0 comments on commit af04fda

Please sign in to comment.