Skip to content

Commit

Permalink
Create more functions for better legibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasfrahm committed May 15, 2021
1 parent 6771198 commit 6538762
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,13 @@ func main() {
sourceFiles := strings.Split(os.Getenv("SOURCE"), "\n")
targetFolder := strings.TrimSpace(os.Getenv("TARGET"))

transferredFiles := int64(0)

var transferredFiles int64
if direction == DirectionUpload {
log.Println("🔼 Uploading ...")
for _, sourceFile := range sourceFiles {
_, file := path.Split(sourceFile)
targetFile := path.Join(targetFolder, file)
if _, err := scp.CopyTo(targetClient, sourceFile, targetFile); err != nil {
log.Fatalf("Failed to upload file to remote: %v", err)
}
log.Println(sourceFile + " >> " + targetFile)

transferredFiles += 1
}
transferredFiles = Upload(targetClient, sourceFiles, targetFolder)
}

if direction == DirectionDownload {
log.Println("🔽 Downloading ...")
for _, sourceFile := range sourceFiles {
_, file := path.Split(sourceFile)
targetFile := path.Join(targetFolder, file)
if _, err := scp.CopyFrom(targetClient, sourceFile, targetFile); err != nil {
log.Fatalf("Failed to download file from remote: %v", err)
}
log.Println(sourceFile + " >> " + targetFile)

transferredFiles += 1
}
transferredFiles = Download(targetClient, sourceFiles, targetFolder)
}

log.Printf("📡 Transferred %d files\n", transferredFiles)
Expand All @@ -172,3 +151,41 @@ func VerifyFingerprint(expected string) ssh.HostKeyCallback {
return nil
}
}

// Upload uploads local files to a remote host.
func Upload(client *ssh.Client, sourceFiles []string, targetFolder string) int64 {
transferredFiles := int64(0)

log.Println("🔼 Uploading ...")
for _, sourceFile := range sourceFiles {
_, file := path.Split(sourceFile)
targetFile := path.Join(targetFolder, file)
if _, err := scp.CopyTo(client, sourceFile, targetFile); err != nil {
log.Fatalf("Failed to upload file to remote: %v", err)
}
log.Println(sourceFile + " >> " + targetFile)

transferredFiles += 1
}

return transferredFiles
}

// Download downloads files from a remote host to the local machine.
func Download(client *ssh.Client, sourceFiles []string, targetFolder string) int64 {
transferredFiles := int64(0)

log.Println("🔽 Downloading ...")
for _, sourceFile := range sourceFiles {
_, file := path.Split(sourceFile)
targetFile := path.Join(targetFolder, file)
if _, err := scp.CopyFrom(client, sourceFile, targetFile); err != nil {
log.Fatalf("Failed to download file from remote: %v", err)
}
log.Println(sourceFile + " >> " + targetFile)

transferredFiles += 1
}

return transferredFiles
}

0 comments on commit 6538762

Please sign in to comment.