From 3b73175b3327b8eb32e3021bff7ba8c90477aa77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= Date: Thu, 14 Sep 2023 10:28:31 -0300 Subject: [PATCH] Download remote file using fallback --- pkg/remote/remote.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index 90e2d1f3..b1753500 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -2,11 +2,13 @@ package remote import ( "bufio" + "context" "fmt" "io" "net" "net/url" "os" + "os/exec" "path/filepath" "strconv" "strings" @@ -89,6 +91,11 @@ func WithDetection() heartbeat.HandleOption { if err != nil { log.Errorf("failed to download file to temporary folder: %s", err) + err = c.DownloadFileFallback(tmpFile.Name()) + if err != nil { + log.Errorf("failed to download remote file using fallback option: %s", err) + } + deleteLocalFile(tmpFile.Name()) continue @@ -225,6 +232,19 @@ func (c Client) DownloadFile(localFile string) error { return nil } +// DownloadFileFallback downloads a remote file and copy to a local file using machine's ssh. +func (c Client) DownloadFileFallback(localFile string) error { + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSecs*time.Second) + defer cancel() + + log.Infoln("downloading remote file using fallback option") + + cmd := exec.CommandContext(ctx, "scp", fmt.Sprintf("%s:%s", c.OriginalHost, c.Path), localFile) // nolint:gosec + cmd.Stderr = os.Stderr + + return cmd.Run() +} + // Connect connects to sftp host. func (c Client) Connect() (*ssh.Client, *sftp.Client, error) { // Initialize client configuration