Skip to content

Commit

Permalink
Allow local binary to be renamed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Mar 29, 2021
1 parent d1dd34c commit b7193f9
Showing 1 changed file with 21 additions and 2 deletions.
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

0 comments on commit b7193f9

Please sign in to comment.