Skip to content

Commit

Permalink
feat!: Remove --update flag from install (#11)
Browse files Browse the repository at this point in the history
Updates are now directly allowed with install.

BREAKING CHANGE: -u,--update flag is no longer supported by shed
install. Install works without it.
  • Loading branch information
cszatmary committed Apr 16, 2021
1 parent 9a478c3 commit a5ec6c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 41 deletions.
21 changes: 3 additions & 18 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ func (s *Shed) writeLockfile() error {
// Install does not modify any state, therefore, if you wish to abort the install simply
// discard the returned InstallSet.
//
// If a tool name is provided with a version and the same tool already exists in the
// lockfile with a different version, then Install will return an error, unless allowUpdates
// is set in which case the given tool version will overwrite the one in the lockfile.
func (s *Shed) Install(allowUpdates bool, toolNames ...string) (*InstallSet, error) {
// All tool names provided must be full import paths, not binary names.
// If a tool name is invalid, Install will return an error.
func (s *Shed) Install(toolNames ...string) (*InstallSet, error) {
// Collect all the tools that need to be installed.
// Merge the given tools with what exists in the lockfile.
seenTools := make(map[string]bool)
Expand All @@ -166,20 +165,6 @@ func (s *Shed) Install(allowUpdates bool, toolNames ...string) (*InstallSet, err
errs = append(errs, errors.WithMessagef(err, "invalid tool name %s", toolName))
continue
}

existing, err := s.lf.GetTool(toolName)
if errors.Is(err, lockfile.ErrIncorrectVersion) {
if !allowUpdates {
err := errors.Errorf("trying to install %s, but %s is in the lockfile", t, existing.Version)
errs = append(errs, err)
continue
}
} else if err != nil && !errors.Is(err, lockfile.ErrNotFound) {
// Shouldn't happen, but handle just to be safe
errs = append(errs, errors.WithMessagef(err, "failed to check if tool exists in lockfile: %s", t))
continue
}

seenTools[t.ImportPath] = true
tools = append(tools, t)
}
Expand Down
16 changes: 5 additions & 11 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func TestInstall(t *testing.T) {
name string
lockfileTools []tool.Tool
installTools []string
allowUpdates bool
wantLen int
wantTools []tool.Tool
}{
Expand All @@ -175,8 +174,7 @@ func TestInstall(t *testing.T) {
"github.com/golangci/golangci-lint/cmd/golangci-lint",
"github.com/Shopify/ejson/cmd/ejson",
},
allowUpdates: false,
wantLen: 3,
wantLen: 3,
wantTools: []tool.Tool{
{ImportPath: "github.com/cszatmary/go-fish", Version: "v0.1.0"},
{ImportPath: "github.com/golangci/golangci-lint/cmd/golangci-lint", Version: "v1.33.0"},
Expand All @@ -191,8 +189,7 @@ func TestInstall(t *testing.T) {
"github.com/golangci/golangci-lint/cmd/golangci-lint@v1.28.3",
"github.com/Shopify/ejson/cmd/ejson@v1.1.0",
},
allowUpdates: false,
wantLen: 3,
wantLen: 3,
wantTools: []tool.Tool{
{ImportPath: "github.com/cszatmary/go-fish", Version: "v0.0.0-20201203230243-22d10c9b658d"},
{ImportPath: "github.com/golangci/golangci-lint/cmd/golangci-lint", Version: "v1.28.3"},
Expand All @@ -207,7 +204,6 @@ func TestInstall(t *testing.T) {
{ImportPath: "github.com/Shopify/ejson/cmd/ejson", Version: "v1.1.0"},
},
installTools: nil,
allowUpdates: false,
wantLen: 3,
wantTools: []tool.Tool{
{ImportPath: "github.com/cszatmary/go-fish", Version: "v0.1.0"},
Expand All @@ -225,8 +221,7 @@ func TestInstall(t *testing.T) {
installTools: []string{
"github.com/golangci/golangci-lint/cmd/golangci-lint@v1.33.0",
},
allowUpdates: true,
wantLen: 3,
wantLen: 3,
wantTools: []tool.Tool{
{ImportPath: "github.com/cszatmary/go-fish", Version: "v0.1.0"},
{ImportPath: "github.com/golangci/golangci-lint/cmd/golangci-lint", Version: "v1.33.0"},
Expand Down Expand Up @@ -257,7 +252,7 @@ func TestInstall(t *testing.T) {
t.Fatalf("failed to create shed client %v", err)
}

installSet, err := s.Install(tt.allowUpdates, tt.installTools...)
installSet, err := s.Install(tt.installTools...)
if err != nil {
t.Errorf("want nil error, got %v", err)
}
Expand Down Expand Up @@ -308,7 +303,6 @@ func TestInstallError(t *testing.T) {
}

_, err = s.Install(
false,
"github.com/cszatmary/go-fish",
"golangci-lint",
"github.com/Shopify/ejson/cmd/ejson@v1.2.2",
Expand All @@ -317,7 +311,7 @@ func TestInstallError(t *testing.T) {
if !ok {
t.Errorf("want error to be lockfile.ErrorList, got %s: %T", err, err)
}
wantLen := 2
wantLen := 1
if len(errList) != wantLen {
t.Errorf("got %d errors, want %d", len(errList), wantLen)
}
Expand Down
13 changes: 1 addition & 12 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
"github.com/spf13/cobra"
)

type installOptions struct {
allowUpdates bool
}

var installOpts installOptions

var installCmd = &cobra.Command{
Use: "install [tools...]",
Args: cobra.ArbitraryArgs,
Expand All @@ -33,10 +27,6 @@ an '@', just like with 'go get' in module-aware mode. If no version is provided,
If no tools are provided, then shed will simply install all tools in the lockfile.
By default shed prevents installing a tool if a different version of the same tool already exists in the shed.lock file.
If you wish to update the tool, the '-u' or '--update' flag can be used. This will cause shed to install the new version
of the tool specified and update the shed.lock file.
Examples:
Install the latest version of a tool:
Expand All @@ -54,7 +44,7 @@ Install all tools specified in shed.lock:
logger := newLogger()
setwd(logger)
shed := mustShed(client.WithLogger(logger))
installSet, err := shed.Install(installOpts.allowUpdates, args...)
installSet, err := shed.Install(args...)
if err != nil {
fatal.ExitErrf(err, "Failed to determine list of tools to install")
}
Expand Down Expand Up @@ -101,6 +91,5 @@ Install all tools specified in shed.lock:
}

func init() {
installCmd.Flags().BoolVarP(&installOpts.allowUpdates, "update", "u", false, "allow updating already installed tools")
rootCmd.AddCommand(installCmd)
}

0 comments on commit a5ec6c5

Please sign in to comment.