Skip to content

Commit

Permalink
Download remote file using fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Sep 14, 2023
1 parent 785401c commit 3b73175
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package remote

import (
"bufio"
"context"
"fmt"
"io"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3b73175

Please sign in to comment.