Skip to content

Commit

Permalink
Add overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Mar 18, 2023
1 parent 9178a63 commit 8b7d9aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/config/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func (c GitHub) InstallFromRelease(ctx context.Context, owner, repo, tag string)

release, err := github.NewRelease(
ctx, owner, repo, tag,
github.WithOverwrite(),
github.WithWorkdir(c.GetHome()),
github.WithFilter(func(filename string) github.FilterFunc {
if filename == "" {
Expand Down
29 changes: 21 additions & 8 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type Release struct {
Tag string
Assets Assets

client *Client
workdir string
verbose bool
filter func(Assets) *Asset
client *Client
workdir string
verbose bool
overwrite bool
filter func(Assets) *Asset
}

// Asset represents GitHub release's asset.
Expand Down Expand Up @@ -89,6 +90,12 @@ type Option func(r *Release)

type FilterFunc func(assets Assets) *Asset

func WithOverwrite() Option {
return func(r *Release) {
r.overwrite = true
}
}

func WithWorkdir(workdir string) Option {
return func(r *Release) {
r.workdir = workdir
Expand Down Expand Up @@ -292,11 +299,17 @@ func (r *Release) Unarchive(asset Asset) error {
// because this logic renames a binary of 'jq-1.6' to 'jq'
//
target := filepath.Join(r.workdir, r.Name)
if _, err := os.Stat(target); err != nil {
log.Printf("[DEBUG] renamed from %s to %s", archive, target)
os.Rename(archive, target)
os.Chmod(target, 0755)
if _, err := os.Stat(target); err == nil {
if r.overwrite {
log.Printf("[WARN] %s: already exist. but overwrite", target)
} else {
log.Printf("[WARN] %s: already exist. so skip to unarchive", target)
return nil
}
}
log.Printf("[DEBUG] renamed from %s to %s", archive, target)
os.Rename(archive, target)
os.Chmod(target, 0755)
return nil
}

Expand Down

0 comments on commit 8b7d9aa

Please sign in to comment.