@@ -2,6 +2,7 @@ package git
22
33import (
44 "bufio"
5+ "io"
56 "os"
67 "path/filepath"
78 "regexp"
@@ -10,13 +11,19 @@ import (
1011 "github.com/mitchellh/go-homedir"
1112)
1213
13- const (
14- hostReStr = "(?i)^[ \t ]*(host|hostname)[ \t ]+(.+)$"
14+ var (
15+ sshHostRE ,
16+ sshTokenRE * regexp.Regexp
1517)
1618
17- type SSHConfig map [string ]string
19+ func init () {
20+ sshHostRE = regexp .MustCompile ("(?i)^[ \t ]*(host|hostname)[ \t ]+(.+)$" )
21+ sshTokenRE = regexp .MustCompile (`%[%h]` )
22+ }
23+
24+ type sshAliasMap map [string ]string
1825
19- func newSSHConfigReader () * SSHConfigReader {
26+ func sshParseFiles () sshAliasMap {
2027 configFiles := []string {
2128 "/etc/ssh_config" ,
2229 "/etc/ssh/ssh_config" ,
@@ -25,38 +32,33 @@ func newSSHConfigReader() *SSHConfigReader {
2532 userConfig := filepath .Join (homedir , ".ssh" , "config" )
2633 configFiles = append ([]string {userConfig }, configFiles ... )
2734 }
28- return & SSHConfigReader {
29- Files : configFiles ,
30- }
31- }
3235
33- type SSHConfigReader struct {
34- Files []string
36+ openFiles := []io.Reader {}
37+ for _ , file := range configFiles {
38+ f , err := os .Open (file )
39+ if err != nil {
40+ continue
41+ }
42+ defer f .Close ()
43+ openFiles = append (openFiles , f )
44+ }
45+ return sshParse (openFiles ... )
3546}
3647
37- func (r * SSHConfigReader ) Read () SSHConfig {
38- config := make (SSHConfig )
39- hostRe := regexp .MustCompile (hostReStr )
40-
41- for _ , filename := range r .Files {
42- r .readFile (config , hostRe , filename )
48+ func sshParse (r ... io.Reader ) sshAliasMap {
49+ config := sshAliasMap {}
50+ for _ , file := range r {
51+ sshParseConfig (config , file )
4352 }
44-
4553 return config
4654}
4755
48- func (r * SSHConfigReader ) readFile (c SSHConfig , re * regexp.Regexp , f string ) error {
49- file , err := os .Open (f )
50- if err != nil {
51- return err
52- }
53- defer file .Close ()
54-
56+ func sshParseConfig (c sshAliasMap , file io.Reader ) error {
5557 hosts := []string {"*" }
5658 scanner := bufio .NewScanner (file )
5759 for scanner .Scan () {
5860 line := scanner .Text ()
59- match := re .FindStringSubmatch (line )
61+ match := sshHostRE .FindStringSubmatch (line )
6062 if match == nil {
6163 continue
6264 }
@@ -67,7 +69,7 @@ func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) err
6769 } else {
6870 for _ , host := range hosts {
6971 for _ , name := range names {
70- c [host ] = expandTokens (name , host )
72+ c [host ] = sshExpandTokens (name , host )
7173 }
7274 }
7375 }
@@ -76,9 +78,8 @@ func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) err
7678 return scanner .Err ()
7779}
7880
79- func expandTokens (text , host string ) string {
80- re := regexp .MustCompile (`%[%h]` )
81- return re .ReplaceAllStringFunc (text , func (match string ) string {
81+ func sshExpandTokens (text , host string ) string {
82+ return sshTokenRE .ReplaceAllStringFunc (text , func (match string ) string {
8283 switch match {
8384 case "%h" :
8485 return host
0 commit comments