Skip to content

Commit

Permalink
fix: parsing ssh config when Host only (charmbracelet#7)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 authored Jan 12, 2022
1 parent a90d33d commit 71865f5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions _example/config.ssh_config
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Host foo

# all other SSH options are ignored
# wildcard Hosts are also ignored

# Having only the Host key also works, it'll be both the endpoint's name and
# hostname, using 22 as default port
Host ssh.example.com
8 changes: 4 additions & 4 deletions sshconfig/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package sshconfig
import (
"fmt"
"io"
"log"
"net"
"os"
"strings"
Expand Down Expand Up @@ -35,12 +34,12 @@ func ParseReader(r io.Reader) ([]*wishlist.Endpoint, error) {
for _, h := range config.Hosts {
for _, pattern := range h.Patterns {
name := pattern.String()
info := infos[name]

if strings.Contains(name, "*") {
continue // ignore wildcards
}

info := infos[name]
for _, n := range h.Nodes {
node := strings.TrimSpace(n.String())
if node == "" {
Expand All @@ -62,8 +61,6 @@ func ParseReader(r io.Reader) ([]*wishlist.Endpoint, error) {
info.User = value
case "Port":
info.Port = value
default:
log.Printf("ignoring invalid node type %q on host %q", key, name)
}
}

Expand All @@ -73,6 +70,9 @@ func ParseReader(r io.Reader) ([]*wishlist.Endpoint, error) {

var endpoints []*wishlist.Endpoint
for name, info := range infos {
if info.Hostname == "" {
info.Hostname = name // Host foo.bar, use foo.bar as name and HostName
}
if info.Port == "" {
info.Port = "22"
}
Expand Down
8 changes: 8 additions & 0 deletions sshconfig/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func TestParseFile(t *testing.T) {
Address: "multi3.foo.local:22",
User: "overridden",
},
{
Name: "no.hostname",
Address: "no.hostname:23231",
},
{
Name: "only.host",
Address: "only.host:22",
},
}, endpoints)
})

Expand Down
5 changes: 5 additions & 0 deletions sshconfig/testdata/good.ssh_config
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ Host multiple2
Host multiple3
HostName multi3.foo.local
User overridden

Host no.hostname
Port 23231

Host only.host

0 comments on commit 71865f5

Please sign in to comment.