Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow local binary to be renamed. #240

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/update/versioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ type Versioner interface {
// GitHub is a versioner that uses GitHub releases.
type GitHub struct {
client *github.Client
binary string
org string
repo string
binary string // name of compiled binary
local string // name to use for binary once extracted
}

// NewGitHub returns a usable GitHub versioner utilizing the provided token.
Expand All @@ -43,6 +44,15 @@ func NewGitHub(ctx context.Context, org string, repo string, binary string) *Git
}
}

// RenameLocalBinary will rename the downloaded binary.
//
// NOTE: this exists so that we can, for example, rename a binary such as
// 'viceroy' to something less ambiguous like 'fastly-localtesting'.
func (g *GitHub) RenameLocalBinary(s string) error {
g.local = s
return nil
}

// LatestVersion implements the Versioner interface.
func (g GitHub) LatestVersion(ctx context.Context) (semver.Version, error) {
release, _, err := g.client.Repositories.GetLatestRelease(ctx, g.org, g.repo)
Expand Down Expand Up @@ -110,7 +120,16 @@ func (g GitHub) Download(ctx context.Context, version semver.Version) (filename
return filename, fmt.Errorf("error extracting binary: %w", err)
}

return filepath.Join(binaryPath, g.binary), nil
latestPath := filepath.Join(binaryPath, g.binary)

if g.local != "" {
if err := os.Rename(latestPath, filepath.Join(binaryPath, g.local)); err != nil {
return filename, fmt.Errorf("error renaming binary: %w", err)
}
latestPath = filepath.Join(binaryPath, g.local)
}

return latestPath, nil
}

func (g GitHub) getReleaseID(ctx context.Context, version semver.Version) (id int64, err error) {
Expand Down