@@ -15,33 +15,40 @@ import (
1515
1616const LocalhostIP = "127.0.0.1"
1717
18- func AddConnection (uri fmt.Stringer , name , identity string , isDefault bool ) error {
18+ type connection struct {
19+ name string
20+ uri * url.URL
21+ }
22+
23+ func addConnection (cons []connection , identity string , isDefault bool ) error {
1924 if len (identity ) < 1 {
2025 return errors .New ("identity must be defined" )
2126 }
2227
2328 return config .EditConnectionConfig (func (cfg * config.ConnectionsFile ) error {
24- if _ , ok := cfg .Connection .Connections [name ]; ok {
25- return errors .New ("cannot overwrite connection" )
26- }
29+ for i , con := range cons {
30+ if _ , ok := cfg .Connection .Connections [con .name ]; ok {
31+ return fmt .Errorf ("cannot overwrite connection %q" , con .name )
32+ }
2733
28- dst := config.Destination {
29- URI : uri .String (),
30- IsMachine : true ,
31- Identity : identity ,
32- }
34+ dst := config.Destination {
35+ URI : con . uri .String (),
36+ IsMachine : true ,
37+ Identity : identity ,
38+ }
3339
34- if isDefault {
35- cfg .Connection .Default = name
36- }
40+ if isDefault && i == 0 {
41+ cfg .Connection .Default = con . name
42+ }
3743
38- if cfg .Connection .Connections == nil {
39- cfg .Connection .Connections = map [string ]config.Destination {
40- name : dst ,
44+ if cfg .Connection .Connections == nil {
45+ cfg .Connection .Connections = map [string ]config.Destination {
46+ con .name : dst ,
47+ }
48+ cfg .Connection .Default = con .name
49+ } else {
50+ cfg .Connection .Connections [con .name ] = dst
4151 }
42- cfg .Connection .Default = name
43- } else {
44- cfg .Connection .Connections [name ] = dst
4552 }
4653
4754 return nil
@@ -108,27 +115,19 @@ func RemoveFilesAndConnections(files []string, names ...string) {
108115 }
109116}
110117
111- type RemoteConnectionType string
112-
113- var SSHRemoteConnection RemoteConnectionType = "ssh"
114-
115- // MakeSSHURL
116- func (rc RemoteConnectionType ) MakeSSHURL (host , path , port , userName string ) url.URL {
117- // TODO Should this function have input verification?
118- userInfo := url .User (userName )
119- uri := url.URL {
120- Scheme : "ssh" ,
121- Opaque : "" ,
122- User : userInfo ,
123- Host : host ,
124- Path : path ,
125- RawPath : "" ,
126- ForceQuery : false ,
127- RawQuery : "" ,
128- Fragment : "" ,
129- }
118+ // makeSSHURL creates a URL from the given input
119+ func makeSSHURL (host , path , port , userName string ) * url.URL {
120+ var hostname string
130121 if len (port ) > 0 {
131- uri .Host = net .JoinHostPort (uri .Hostname (), port )
122+ hostname = net .JoinHostPort (host , port )
123+ } else {
124+ hostname = host
125+ }
126+ userInfo := url .User (userName )
127+ return & url.URL {
128+ Scheme : "ssh" ,
129+ User : userInfo ,
130+ Host : hostname ,
131+ Path : path ,
132132 }
133- return uri
134133}
0 commit comments