@@ -128,34 +128,13 @@ func main() {
128128 sourceFiles := strings .Split (os .Getenv ("SOURCE" ), "\n " )
129129 targetFolder := strings .TrimSpace (os .Getenv ("TARGET" ))
130130
131- transferredFiles := int64 (0 )
132-
131+ var transferredFiles int64
133132 if direction == DirectionUpload {
134- log .Println ("🔼 Uploading ..." )
135- for _ , sourceFile := range sourceFiles {
136- _ , file := path .Split (sourceFile )
137- targetFile := path .Join (targetFolder , file )
138- if _ , err := scp .CopyTo (targetClient , sourceFile , targetFile ); err != nil {
139- log .Fatalf ("Failed to upload file to remote: %v" , err )
140- }
141- log .Println (sourceFile + " >> " + targetFile )
142-
143- transferredFiles += 1
144- }
133+ transferredFiles = Upload (targetClient , sourceFiles , targetFolder )
145134 }
146135
147136 if direction == DirectionDownload {
148- log .Println ("🔽 Downloading ..." )
149- for _ , sourceFile := range sourceFiles {
150- _ , file := path .Split (sourceFile )
151- targetFile := path .Join (targetFolder , file )
152- if _ , err := scp .CopyFrom (targetClient , sourceFile , targetFile ); err != nil {
153- log .Fatalf ("Failed to download file from remote: %v" , err )
154- }
155- log .Println (sourceFile + " >> " + targetFile )
156-
157- transferredFiles += 1
158- }
137+ transferredFiles = Download (targetClient , sourceFiles , targetFolder )
159138 }
160139
161140 log .Printf ("📡 Transferred %d files\n " , transferredFiles )
@@ -172,3 +151,41 @@ func VerifyFingerprint(expected string) ssh.HostKeyCallback {
172151 return nil
173152 }
174153}
154+
155+ // Upload uploads local files to a remote host.
156+ func Upload (client * ssh.Client , sourceFiles []string , targetFolder string ) int64 {
157+ transferredFiles := int64 (0 )
158+
159+ log .Println ("🔼 Uploading ..." )
160+ for _ , sourceFile := range sourceFiles {
161+ _ , file := path .Split (sourceFile )
162+ targetFile := path .Join (targetFolder , file )
163+ if _ , err := scp .CopyTo (client , sourceFile , targetFile ); err != nil {
164+ log .Fatalf ("Failed to upload file to remote: %v" , err )
165+ }
166+ log .Println (sourceFile + " >> " + targetFile )
167+
168+ transferredFiles += 1
169+ }
170+
171+ return transferredFiles
172+ }
173+
174+ // Download downloads files from a remote host to the local machine.
175+ func Download (client * ssh.Client , sourceFiles []string , targetFolder string ) int64 {
176+ transferredFiles := int64 (0 )
177+
178+ log .Println ("🔽 Downloading ..." )
179+ for _ , sourceFile := range sourceFiles {
180+ _ , file := path .Split (sourceFile )
181+ targetFile := path .Join (targetFolder , file )
182+ if _ , err := scp .CopyFrom (client , sourceFile , targetFile ); err != nil {
183+ log .Fatalf ("Failed to download file from remote: %v" , err )
184+ }
185+ log .Println (sourceFile + " >> " + targetFile )
186+
187+ transferredFiles += 1
188+ }
189+
190+ return transferredFiles
191+ }
0 commit comments