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

Adds a "provider" flag to update a install commands #49

Merged
merged 2 commits into from
Mar 16, 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
16 changes: 9 additions & 7 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -18,19 +18,20 @@ type installCmd struct {
}

type installOpts struct {
force bool
force bool
provider string
}

func newInstallCmd() *installCmd {
var root = &installCmd{}
// nolint: dupl
var cmd = &cobra.Command{
Use: "install",
Use: "install <url>",
Aliases: []string{"i"},
Short: "Installs the specified project",
Short: "Installs the specified project from a url",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(2),
Args: cobra.MinimumNArgs(1),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this change is strictly necessary, as the number of Args is not the same as the number of flags. However, if it is needed to make something else work, I don't see a problem with keeping it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this change is unrelated to this PR. I just realized that we didn't configure that correctly and I'm fixing that here since bin install always requires a minimum of a 1 argument to work :D

RunE: func(cmd *cobra.Command, args []string) error {
//TODO implement --force(-f) flag for install
// to override the binary if exists
@@ -59,7 +60,7 @@ func newInstallCmd() *installCmd {
//TODO check if binary already exists in config
// and triger the update process if that's the case

p, err := providers.New(u)
p, err := providers.New(u, root.opts.provider)
if err != nil {
return err
}
@@ -76,8 +77,7 @@ func newInstallCmd() *installCmd {
return err
}

force, _ := cmd.LocalFlags().GetBool("force")
if err = saveToDisk(pResult, path, force); err != nil {
if err = saveToDisk(pResult, path, root.opts.force); err != nil {
return fmt.Errorf("Error installing binary: %w", err)
}

@@ -87,6 +87,7 @@ func newInstallCmd() *installCmd {
Version: pResult.Version,
Hash: fmt.Sprintf("%x", pResult.Hash.Sum(nil)),
URL: u,
Provider: p.GetID(),
})

if err != nil {
@@ -101,6 +102,7 @@ func newInstallCmd() *installCmd {

root.cmd = cmd
root.cmd.Flags().BoolVarP(&root.opts.force, "force", "f", false, "Force the installation even if the file already exists")
root.cmd.Flags().StringVarP(&root.opts.provider, "provider", "p", "", "Forces to use a specific provider")
return root
}

4 changes: 2 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ func newUpdateCmd() *updateCmd {
//use the same code in both places
for ui, b := range toUpdate {

p, err := providers.New(ui.url)
p, err := providers.New(ui.url, b.Provider)

if err != nil {
return err
@@ -141,7 +141,7 @@ func newUpdateCmd() *updateCmd {
}

func getLatestVersion(b *config.Binary) (*updateInfo, error) {
p, err := providers.New(b.URL)
p, err := providers.New(b.URL, b.Provider)

if err != nil {
return nil, err
98 changes: 98 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ type Binary struct {
Version string `json:"version"`
Hash string `json:"hash"`
URL string `json:"url"`
Provider string `json:"provider"`
}

func CheckAndLoad() error {
4 changes: 4 additions & 0 deletions pkg/providers/docker.go
Original file line number Diff line number Diff line change
@@ -50,6 +50,10 @@ func (d *docker) GetLatestVersion() (string, string, error) {
return d.tag, "", nil
}

func (d *docker) GetID() string {
return "docker"
}

func newDocker(imageURL string) (Provider, error) {
imageURL = strings.TrimPrefix(imageURL, "docker://")

4 changes: 4 additions & 0 deletions pkg/providers/github.go
Original file line number Diff line number Diff line change
@@ -81,6 +81,10 @@ func (g *gitHub) GetLatestVersion() (string, string, error) {
return release.GetTagName(), release.GetHTMLURL(), nil
}

func (g *gitHub) GetID() string {
return "github"
}

func newGitHub(u *url.URL) (Provider, error) {
s := strings.Split(u.Path, "/")
if len(s) < 2 {
4 changes: 4 additions & 0 deletions pkg/providers/gitlab.go
Original file line number Diff line number Diff line change
@@ -102,6 +102,10 @@ func (g *gitLab) Fetch() (*File, error) {
return f, nil
}

func (g *gitLab) GetID() string {
return "gitlab"
}

//GetLatestVersion checks the latest repo release and
//returns the corresponding name and url to fetch the version
func (g *gitLab) GetLatestVersion() (string, string, error) {
4 changes: 4 additions & 0 deletions pkg/providers/hashicorp.go
Original file line number Diff line number Diff line change
@@ -76,6 +76,10 @@ func (g *hashiCorpFileInfo) String() string {
return g.name
}

func (g *hashiCorp) GetID() string {
return "hashicorp"
}

func (g *hashiCorp) Fetch() (*File, error) {

var release *hashiCorpRelease
11 changes: 7 additions & 4 deletions pkg/providers/providers.go
Original file line number Diff line number Diff line change
@@ -27,14 +27,17 @@ type Provider interface {
// GetLatestVersion returns the version and the URL of the
// latest version for this binary
GetLatestVersion() (string, string, error)

// GetID returns the unique identiifer of this provider
GetID() string
}

var (
httpUrlPrefix = regexp.MustCompile("^https?://")
dockerUrlPrefix = regexp.MustCompile("^docker://")
)

func New(u string) (Provider, error) {
func New(u, provider string) (Provider, error) {
if dockerUrlPrefix.MatchString(u) {
return newDocker(u)
}
@@ -48,15 +51,15 @@ func New(u string) (Provider, error) {
return nil, err
}

if strings.Contains(purl.Host, "github") {
if strings.Contains(purl.Host, "github") || provider == "github" {
return newGitHub(purl)
}

if strings.Contains(purl.Host, "gitlab") {
if strings.Contains(purl.Host, "gitlab") || provider == "gitlab" {
return newGitLab(purl)
}

if strings.Contains(purl.Host, "releases.hashicorp.com") {
if strings.Contains(purl.Host, "releases.hashicorp.com") || provider == "hashicorp" {
return newHashiCorp(purl)
}