diff --git a/README.md b/README.md index 1ee65ec..0579756 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # CMDR -[![test](https://github.com/MrLYC/cmdr/actions/workflows/unittest.yml/badge.svg)](https://github.com/MrLYC/cmdr/actions/workflows/unittest.yml) [![codecov](https://codecov.io/gh/MrLYC/cmdr/branch/master/graph/badge.svg?token=mo4TJP4mQt)](https://codecov.io/gh/MrLYC/cmdr) ![Go version](https://img.shields.io/github/go-mod/go-version/mrlyc/cmdr) ![release](https://img.shields.io/github/v/release/mrlyc/cmdr?label=version) +[![test](https://github.com/MrLYC/cmdr/actions/workflows/test.yml/badge.svg)](https://github.com/MrLYC/cmdr/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/MrLYC/cmdr/branch/master/graph/badge.svg?token=mo4TJP4mQt)](https://codecov.io/gh/MrLYC/cmdr) ![Go version](https://img.shields.io/github/go-mod/go-version/mrlyc/cmdr) ![release](https://img.shields.io/github/v/release/mrlyc/cmdr?label=version) CMDR is a simple command version management tool that helps you quickly switch from multiple command versions. diff --git a/core/initializer/command.go b/core/initializer/command.go index 96db007..634c661 100644 --- a/core/initializer/command.go +++ b/core/initializer/command.go @@ -34,20 +34,6 @@ func (c *CmdrUpdater) getActivatedCmdrVersion() string { return command.GetVersion() } -func (c *CmdrUpdater) install() error { - _, err := c.manager.Define(c.name, c.version, c.location) - if err != nil { - return errors.Wrapf(err, "failed to define command %s", c.name) - } - - err = c.manager.Activate(c.name, c.version) - if err != nil { - return errors.Wrapf(err, "failed to activate command %s", c.name) - } - - return nil -} - func (c *CmdrUpdater) removeLegacies(safeVersions []string) error { logger := core.GetLogger() @@ -69,6 +55,7 @@ func (c *CmdrUpdater) removeLegacies(safeVersions []string) error { logger.Debug("checking legacy command", map[string]interface{}{ "command": command, }) + if command.GetActivated() { continue } @@ -112,12 +99,17 @@ func (c *CmdrUpdater) Init(isUpgrade bool) error { }) if !isUpgrade { - err := c.install() + _, err := c.manager.Define(c.name, c.version, c.location) if err != nil { - return errors.WithMessagef(err, "failed to install command %s", c.name) + return errors.Wrapf(err, "failed to define command %s", c.name) } } + err := c.manager.Activate(c.name, c.version) + if err != nil { + return errors.Wrapf(err, "failed to activate command %s", c.name) + } + return c.removeLegacies(safeVersion) } diff --git a/core/initializer/command_test.go b/core/initializer/command_test.go index b44133e..e53cc4f 100644 --- a/core/initializer/command_test.go +++ b/core/initializer/command_test.go @@ -62,4 +62,10 @@ var _ = Describe("Command", func() { Expect(updater.Init(false)).To(Succeed()) }) + It("should upgrade cmdr", func() { + manager.EXPECT().Activate(name, version) + manager.EXPECT().Undefine(name, legacyCommand.GetVersion()) + + Expect(updater.Init(true)).To(Succeed()) + }) })