-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
bury
command, archives all release assets as in #12
- Loading branch information
1 parent
65c6080
commit 7ce90da
Showing
11 changed files
with
242 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package bury | ||
|
||
import ( | ||
"github.com/leslieleung/reaper/internal/config" | ||
"github.com/leslieleung/reaper/internal/release" | ||
"github.com/leslieleung/reaper/internal/rip" | ||
"github.com/leslieleung/reaper/internal/typedef" | ||
"github.com/leslieleung/reaper/internal/ui" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "bury", | ||
Short: "bury immediately downloads all release assets of a repo", | ||
Run: runBury, | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
var storageName string | ||
|
||
func runBury(cmd *cobra.Command, args []string) { | ||
repoName := args[0] | ||
|
||
storageMap := config.GetStorageMap() | ||
storages := make([]typedef.MultiStorage, 0) | ||
if storageName != "" { | ||
if s, ok := storageMap[storageName]; !ok { | ||
ui.Errorf("Storage %s not found in config", storageName) | ||
return | ||
} else { | ||
storages = append(storages, s) | ||
} | ||
} else { | ||
for _, storage := range storageMap { | ||
storages = append(storages, storage) | ||
} | ||
} | ||
|
||
for _, repo := range rip.GetRepositories(repoName) { | ||
storages := make([]typedef.MultiStorage, 0) | ||
for _, storage := range repo.Storage { | ||
if s, ok := storageMap[storage]; !ok { | ||
ui.Errorf("Storage %s not found in config", storage) | ||
continue | ||
} else { | ||
storages = append(storages, s) | ||
} | ||
} | ||
ui.Printf("Running %s", repo.Name) | ||
if err := release.DownloadAllAssets(repo, storages); err != nil { | ||
ui.Errorf("Error running %s, %s", repo.Name, err) | ||
// move on to next repo | ||
} | ||
} | ||
ui.Printf("Done") | ||
} | ||
|
||
func init() { | ||
Cmd.Flags().StringVarP(&storageName, "storage", "s", "", | ||
"storage to use, if not specified, all storages will be used") | ||
} |
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,63 @@ | ||
package release | ||
|
||
import ( | ||
"fmt" | ||
"github.com/leslieleung/reaper/internal/scm" | ||
"github.com/leslieleung/reaper/internal/scm/github" | ||
"github.com/leslieleung/reaper/internal/storage" | ||
"github.com/leslieleung/reaper/internal/typedef" | ||
"github.com/leslieleung/reaper/internal/ui" | ||
"io" | ||
) | ||
|
||
func DownloadAllAssets(repo typedef.Repository, storages []typedef.MultiStorage) error { | ||
r, err := scm.NewRepository(repo.URL) | ||
if err != nil { | ||
return err | ||
} | ||
c, err := github.New() | ||
if err != nil { | ||
return err | ||
} | ||
// get all releases | ||
releases, err := c.GetReleases(r.Owner, r.Name) | ||
if err != nil { | ||
return err | ||
} | ||
for _, release := range releases { | ||
ui.Printf("Downloading %s", release.GetTagName()) | ||
// get all assets | ||
assets, err := c.GetReleaseAssets(r.Owner, r.Name, release.GetID()) | ||
if err != nil { | ||
return err | ||
} | ||
for _, asset := range assets { | ||
if asset.GetState() != "uploaded" { | ||
continue | ||
} | ||
ui.Printf("Downloading asset %s", asset.GetName()) | ||
path := fmt.Sprintf("%s-%s/%s", repo.Name, release.GetTagName(), asset.GetName()) | ||
// download asset | ||
rc, err := c.DownloadAsset(r.Owner, r.Name, asset.GetID()) | ||
if err != nil { | ||
return err | ||
} | ||
// put rc to file | ||
data, err := io.ReadAll(rc) | ||
if err != nil { | ||
return err | ||
} | ||
for _, s := range storages { | ||
backend, err := storage.GetStorage(s) | ||
if err != nil { | ||
return err | ||
} | ||
err = backend.PutObject(path, data) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,28 @@ | ||
package scm | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
type Repository struct { | ||
Host string | ||
Owner string | ||
Name string | ||
} | ||
|
||
var ( | ||
ErrInvalidURL = errors.New("invalid url") | ||
) | ||
|
||
func NewRepository(url string) (*Repository, error) { | ||
r := &Repository{} | ||
l := strings.Split(url, "/") | ||
if len(l) < 3 { | ||
return nil, ErrInvalidURL | ||
} | ||
r.Host = l[0] | ||
r.Owner = l[1] | ||
r.Name = l[2] | ||
return r, nil | ||
} |
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