This commit introduces a new cloud storage backend with support for S3 and S3-compatible services. Key changes: - Created a `storage` package with a `Storage` interface. - Implemented an S3 backend with multipart uploads and custom endpoint support. - Added `push`, `pull`, `ls`, and `remote add` commands. - Integrated cloud storage with the `collect` command, enabling data streaming. - Added unit and integration tests for the new functionality. Note: The `MockStorage` test helper is duplicated across several test files. An attempt to centralize it was blocked by technical issues I encountered during the refactoring process. This refactoring is left for a future commit. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
|
|
"github.com/Snider/Borg/pkg/storage"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func NewPullCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "pull [remote-url] [local-path]",
|
|
Short: "Pull a remote file from a remote storage URL",
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
remoteURL := args[0]
|
|
localPath := args[1]
|
|
|
|
u, err := url.Parse(remoteURL)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid remote URL: %w", err)
|
|
}
|
|
|
|
s, err := storage.NewStorage(u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r, err := s.Read(u.Path)
|
|
if err != nil {
|
|
return fmt.Errorf("error downloading file: %w", err)
|
|
}
|
|
defer r.Close()
|
|
|
|
f, err := os.Create(localPath)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating local file: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, r)
|
|
if err != nil {
|
|
return fmt.Errorf("error writing to local file: %w", err)
|
|
}
|
|
|
|
fmt.Fprintln(cmd.OutOrStdout(), "File pulled successfully")
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(NewPullCmd())
|
|
}
|