-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement download modes framework (#25)
Imeplement a download modes framework that allows specifying a mechanism to download. This commit implements "buffer" and "tar-extract" which mirror the original behaviors of pget. The '-x'/'--extract' option automatically sets the `tar-extract` mode. tar-extract mode simply wraps the buffer mode for now. Behavior and CLI options are not modified in this commit
- Loading branch information
1 parent
c40b7ab
commit 9581b69
Showing
5 changed files
with
74 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package download | ||
|
||
type modeFactoryFunc func() Mode | ||
|
||
var modes = map[string]modeFactoryFunc{ | ||
"buffer": func() Mode { return &BufferMode{Client: newClient()} }, | ||
"tar-extract": func() Mode { return &ExtractTarMode{} }, | ||
} | ||
|
||
type Mode interface { | ||
DownloadFile(url string, dest string) error | ||
} | ||
|
||
func GetMode(name string) Mode { | ||
return modes[name]() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package download | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/replicate/pget/pkg/extract" | ||
) | ||
|
||
type ExtractTarMode struct { | ||
} | ||
|
||
func (m *ExtractTarMode) DownloadFile(url string, dest string) error { | ||
downloader := &BufferMode{Client: newClient()} | ||
buffer, fileSize, err := downloader.fileToBuffer(url) | ||
if err != nil { | ||
return fmt.Errorf("error downloading file: %w", err) | ||
} | ||
err = extract.ExtractTarFile(buffer, dest, fileSize) | ||
if err != nil { | ||
return fmt.Errorf("error extracting file: %w", err) | ||
} | ||
return nil | ||
} |