Skip to content

Commit

Permalink
Merge pull request #56 from sirlatrom/feature/no-semantic-downgrades
Browse files Browse the repository at this point in the history
Prevent semantic downgrades in `bin update`
  • Loading branch information
sirlatrom authored Mar 19, 2021
2 parents cf4e7af + 9f1f191 commit 03717bd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 118 deletions.
38 changes: 18 additions & 20 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/apex/log"
"github.com/fatih/color"
"github.com/hashicorp/go-version"
"github.com/marcosnils/bin/pkg/config"
"github.com/marcosnils/bin/pkg/providers"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,30 +53,26 @@ func newUpdateCmd() *updateCmd {

toUpdate := map[*updateInfo]*config.Binary{}
cfg := config.Get()
binsToProcess := cfg.Bins

// Update single binary
if bin != "" {
bin, err := getBinPath(bin)
if err != nil {
return err
}

b := cfg.Bins[bin]

if ui, err := getLatestVersion(b); err != nil {
binsToProcess = map[string]*config.Binary{bin: cfg.Bins[bin]}
}
for _, b := range binsToProcess {
p, err := providers.New(b.URL, b.Provider)
if err != nil {
return err
}
if ui, err := getLatestVersion(b, p); err != nil {
return err
} else if ui != nil {
toUpdate[ui] = b
}

} else {
for _, b := range cfg.Bins {
if ui, err := getLatestVersion(b); err != nil {
return err
} else if ui != nil {
toUpdate[ui] = b
}
}
}

if len(toUpdate) == 0 {
Expand Down Expand Up @@ -140,13 +137,7 @@ func newUpdateCmd() *updateCmd {
return root
}

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

if err != nil {
return nil, err
}

func getLatestVersion(b *config.Binary, p providers.Provider) (*updateInfo, error) {
log.Debugf("Checking updates for %s", b.Path)
v, u, err := p.GetLatestVersion()

Expand All @@ -157,6 +148,13 @@ func getLatestVersion(b *config.Binary) (*updateInfo, error) {
if b.Version == v {
return nil, nil
}

bSemver, bSemverErr := version.NewVersion(b.Version)
vSemver, vSemverErr := version.NewVersion(v)
if bSemverErr == nil && vSemverErr == nil && vSemver.LessThanOrEqual(bSemver) {
return nil, nil
}

log.Debugf("Found new version %s for %s at %s", v, b.Path, u)
log.Infof("%s %s -> %s (%s)", b.Path, color.YellowString(b.Version), color.GreenString(v), u)
return &updateInfo{v, u}, nil
Expand Down
73 changes: 73 additions & 0 deletions cmd/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"reflect"
"testing"

"github.com/marcosnils/bin/pkg/config"
"github.com/marcosnils/bin/pkg/providers"
)

type mockProvider struct {
providers.Provider
latestVersion string
latestVersionURL string
err error
}

func (m mockProvider) GetLatestVersion() (string, string, error) {
return m.latestVersion, m.latestVersionURL, m.err
}

func (m mockProvider) GetID() string {
return "github"
}

func TestGetLatestVersion(t *testing.T) {
type mockValues struct {
latestVersion string
latestVersionURL string
err error
}
cases := []struct {
in *config.Binary
m mockValues
out *updateInfo
}{
{
&config.Binary{
Path: "/home/user/bin/launchpad",
Version: "1.1.0",
URL: "https://github.com/Mirantis/launchpad/releases/download/1.1.0/launchpad-linux-x64",
RemoteName: "launchpad-linux-x64",
Provider: "github",
},
mockValues{"1.1.1", "https://github.com/Mirantis/launchpad/releases/download/1.1.1/launchpad-linux-x64", nil},
&updateInfo{
version: "1.1.1",
url: "https://github.com/Mirantis/launchpad/releases/download/1.1.1/launchpad-linux-x64",
},
},
{
&config.Binary{
Path: "/home/user/bin/launchpad",
Version: "1.2.0-rc.1",
URL: "https://github.com/Mirantis/launchpad/releases/download/1.2.0-rc.1/launchpad-linux-x64",
RemoteName: "launchpad-linux-x64",
Provider: "github",
},
mockValues{"1.1.1", "https://github.com/Mirantis/launchpad/releases/download/1.1.1/launchpad-linux-x64", nil},
nil,
},
}

for _, c := range cases {
p := mockProvider{latestVersion: c.m.latestVersion, latestVersionURL: c.m.latestVersionURL, err: c.m.err}
if v, err := getLatestVersion(c.in, p); err != nil {
t.Fatalf("Error during getLatestVersion(%#v, %#v): %v", c.in, p, err)
} else if !reflect.DeepEqual(v, c.out) {
t.Fatalf("For case %#v: %#v does not match %#v", c.in, v, c.out)
}
}

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/google/go-github/v31 v31.0.0
github.com/gorilla/mux v1.7.4 // indirect
github.com/h2non/filetype v1.1.0
github.com/hashicorp/go-version v1.2.1
github.com/kr/text v0.2.0 // indirect
github.com/krolaw/zipstream v0.0.0-20180621105154-0a2661891f94
github.com/mattn/go-colorable v0.1.7 // indirect
Expand Down
Loading

0 comments on commit 03717bd

Please sign in to comment.