@@ -30,59 +30,72 @@ func extractPath(endpoint string) string {
3030 return endpoint [n + 1 :]
3131}
3232
33- func Copy (configs []ssh.SSHConfig , first , second string ) error {
34- firstHost := extractHost (first )
35- secondHost := extractHost (second )
33+ func findConfig (configs []ssh.SSHConfig , host string ) (ssh.SSHConfig , bool ) {
34+ for i := range configs {
35+ if configs [i ].Host == host {
36+ return configs [i ], true
37+ }
38+ }
39+ return ssh.SSHConfig {}, false
40+ }
41+
42+ func Copy (configs []ssh.SSHConfig , left , right string ) error {
43+ leftHost := extractHost (left )
44+ rightHost := extractHost (right )
3645
3746 var leftClient , rightClient * cssh.Client
3847 var leftConfig , rightConfig ssh.SSHConfig
3948 var err error
40- if firstHost != "" { // remote -> local | remote
41- for i := range configs {
42- if configs [i ].Host == firstHost {
43- leftConfig = configs [i ]
44- break
45- }
49+ if leftHost != "" { // remote -> local | remote
50+ var has bool
51+ leftConfig , has = findConfig (configs , leftHost )
52+ if ! has {
53+ return fmt .Errorf ("config %s not found" , leftHost )
4654 }
4755 leftClient , err = ssh .SSHClient (leftConfig )
4856 if err != nil {
4957 return err
5058 }
5159
52- if secondHost != "" { // remote -> remote
53- for i := range configs {
54- if configs [i ].Host == secondHost {
55- rightConfig = configs [i ]
56- break
57- }
60+ if rightHost != "" { // remote -> remote
61+ rightConfig , has = findConfig (configs , rightHost )
62+ if ! has {
63+ return fmt .Errorf ("config %s not found" , rightHost )
5864 }
5965 rightClient , err = ssh .SSHClient (rightConfig )
6066 if err != nil {
6167 return err
6268 }
6369 tempPath := filepath .Join (os .TempDir (),
64- removeSlashes ("s1h" + first + second ))
65- err = ssh .DownloadFile (leftClient , extractPath (firstHost ), tempPath )
70+ removeSlashes ("s1h" + left + right ))
71+ err = ssh .DownloadFile (leftClient , extractPath (leftHost ), tempPath )
6672 if err != nil {
6773 return err
6874 }
6975 defer os .Remove (tempPath )
70- err = ssh .UploadFile (rightClient , tempPath , extractPath (secondHost ))
76+ err = ssh .UploadFile (rightClient , tempPath , extractPath (rightHost ))
7177 } else { // remote -> local
72- err = ssh .DownloadFile (leftClient , extractPath (firstHost ), second )
78+ err = ssh .DownloadFile (leftClient , extractPath (leftHost ), right )
7379 }
7480 } else { // local -> remote
75- for i := range configs {
76- if configs [i ].Host == secondHost {
77- rightConfig = configs [i ]
78- break
79- }
81+ var has bool
82+ rightConfig , has = findConfig (configs , rightHost )
83+ if ! has {
84+ return fmt .Errorf ("config %s not found" , rightHost )
8085 }
8186 rightClient , err = ssh .SSHClient (rightConfig )
8287 if err != nil {
8388 return err
8489 }
85- err = ssh .UploadFile (rightClient , extractPath (firstHost ), second )
90+ err = ssh .UploadFile (rightClient , left , extractPath (right ) )
8691 }
8792 return err
8893}
94+
95+ func Shell (configs []ssh.SSHConfig , host string ) error {
96+ cfg , has := findConfig (configs , host )
97+ if ! has {
98+ return fmt .Errorf ("config %s not found" , host )
99+ }
100+ return ssh .ExecuteSSHShell (cfg )
101+ }
0 commit comments