Skip to content

Commit

Permalink
update step interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLYC committed Dec 18, 2021
1 parent af3c208 commit 7e1b5db
Show file tree
Hide file tree
Showing 26 changed files with 271 additions and 284 deletions.
9 changes: 2 additions & 7 deletions cmd/command/define.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ var defineCmd = &cobra.Command{

runner := core.NewStepRunner(
core.NewDBClientMaker(),
core.NewCommandDefiner(shimsDir),
core.NewCommandDefiner(shimsDir, simpleCmdFlag.name, simpleCmdFlag.version, simpleCmdFlag.location, false),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: simpleCmdFlag.name,
define.ContextKeyVersion: simpleCmdFlag.version,
define.ContextKeyLocation: simpleCmdFlag.location,
define.ContextKeyCommandManaged: false,
})), "install failed")
utils.ExitWithError(runner.Run(cmd.Context()), "install failed")

define.Logger.Info("defined command", map[string]interface{}{
"name": simpleCmdFlag.name,
Expand Down
9 changes: 2 additions & 7 deletions cmd/command/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ var installCmd = &cobra.Command{

runner := core.NewStepRunner(
core.NewDBClientMaker(),
core.NewCommandDefiner(shimsDir, simpleCmdFlag.name, simpleCmdFlag.version, simpleCmdFlag.location, true),
core.NewDownloader(),
core.NewBinariesInstaller(shimsDir),
core.NewCommandDefiner(shimsDir),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: simpleCmdFlag.name,
define.ContextKeyVersion: simpleCmdFlag.version,
define.ContextKeyLocation: simpleCmdFlag.location,
define.ContextKeyCommandManaged: true,
})), "install failed")
utils.ExitWithError(runner.Run(cmd.Context()), "install failed")

define.Logger.Info("installed command", map[string]interface{}{
"name": simpleCmdFlag.name,
Expand Down
7 changes: 1 addition & 6 deletions cmd/command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/spf13/cobra"

"github.com/mrlyc/cmdr/core"
"github.com/mrlyc/cmdr/define"
"github.com/mrlyc/cmdr/utils"
)

Expand All @@ -28,11 +27,7 @@ var listCmd = &cobra.Command{
core.NewCommandPrinter(os.Stdout),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: simpleCmdFlag.name,
define.ContextKeyVersion: simpleCmdFlag.version,
define.ContextKeyLocation: simpleCmdFlag.location,
})), "list failed")
utils.ExitWithError(runner.Run(cmd.Context()), "list failed")
},
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/command/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ var uninstallCmd = &cobra.Command{
core.NewBinariesUninstaller(),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: simpleCmdFlag.name,
define.ContextKeyVersion: simpleCmdFlag.version,
})), "list failed")
utils.ExitWithError(runner.Run(cmd.Context()), "list failed")

define.Logger.Info("uninstalled command", map[string]interface{}{
"name": simpleCmdFlag.name,
"version": simpleCmdFlag.version,
"location": simpleCmdFlag.location,
"name": simpleCmdFlag.name,
"version": simpleCmdFlag.version,
})
},
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/command/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ var unsetCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
runner := core.NewStepRunner(
core.NewDBClientMaker(),
core.NewNamedCommandsQuerier(simpleCmdFlag.name),
core.NewCommandDeactivator(),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: simpleCmdFlag.name,
})), "deactivate failed")
utils.ExitWithError(runner.Run(cmd.Context()), "deactivate failed")

define.Logger.Info("unset command", map[string]interface{}{
"name": simpleCmdFlag.name,
Expand Down
6 changes: 1 addition & 5 deletions cmd/command/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ var useCmd = &cobra.Command{
core.NewCommandActivator(),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: simpleCmdFlag.name,
define.ContextKeyVersion: simpleCmdFlag.version,
define.ContextKeyLocation: simpleCmdFlag.location,
})), "activate failed")
utils.ExitWithError(runner.Run(cmd.Context()), "activate failed")

define.Logger.Info("used command", map[string]interface{}{
"name": simpleCmdFlag.name,
Expand Down
6 changes: 1 addition & 5 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/spf13/cobra"

"github.com/mrlyc/cmdr/core"
"github.com/mrlyc/cmdr/define"
"github.com/mrlyc/cmdr/model"
"github.com/mrlyc/cmdr/utils"
)
Expand Down Expand Up @@ -38,10 +37,7 @@ var doctorCmd = &cobra.Command{
core.NewBinariesActivator(binDir),
)

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: define.Name,
define.ContextKeyCommandManaged: true,
})), "doctor failed")
utils.ExitWithError(runner.Run(cmd.Context()), "doctor failed")
},
}

Expand Down
10 changes: 2 additions & 8 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ var setupCmd = &cobra.Command{

if !setupCmdFlag.skipInstall && !setupCmdFlag.upgrade {
runner.Add(
core.NewStepLoggerWithFields("installing command", define.ContextKeyName, define.ContextKeyVersion),
core.NewCommandDefiner(shimsDir, define.Name, define.Version, cmdrLocation, true),
core.NewBinariesInstaller(shimsDir),
core.NewCommandDefiner(shimsDir),
)
}

Expand All @@ -51,12 +50,7 @@ var setupCmd = &cobra.Command{
)
}

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: define.Name,
define.ContextKeyVersion: define.Version,
define.ContextKeyLocation: cmdrLocation,
define.ContextKeyCommandManaged: true,
})), "setup failed")
utils.ExitWithError(runner.Run(cmd.Context()), "setup failed")
},
}

Expand Down
15 changes: 6 additions & 9 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/mrlyc/cmdr/core"
Expand All @@ -24,6 +26,8 @@ var upgradeCmd = &cobra.Command{
runner := core.NewStepRunner()
shimsDir := core.GetShimsDir()
binDir := core.GetBinDir()
cmdrLocation, err := os.Executable()
utils.CheckError(err)

if !upgradeCmdFlag.skipSetup {
runArgs := []string{"setup", "--upgrade"}
Expand All @@ -33,33 +37,26 @@ var upgradeCmd = &cobra.Command{

runner.Add(
core.NewDBClientMaker(),
core.NewCommandDefiner(shimsDir, define.Name, define.Version, cmdrLocation, true),
core.NewReleaseSearcher(upgradeCmdFlag.release, upgradeCmdFlag.asset),
core.NewDownloader(),
core.NewBinariesInstaller(shimsDir),
core.NewCommandDefiner(shimsDir),
)

if !upgradeCmdFlag.keep {
runner.Add(
core.NewCommandDeactivator(),
core.NewBinariesActivator(binDir),
core.NewCommandActivator(),
core.NewContextValueSetter(map[define.ContextKey]interface{}{
define.ContextKeyVersion: define.Version,
}),
core.NewSimpleCommandsQuerier(
define.Name, define.Version,
),
core.NewStepLoggerWithFields("uninstalling cmdr", define.ContextKeyVersion),
core.NewCommandUndefiner(),
core.NewBinariesUninstaller(),
)
}

utils.ExitWithError(runner.Run(utils.SetIntoContext(cmd.Context(), map[define.ContextKey]interface{}{
define.ContextKeyName: define.Name,
define.ContextKeyCommandManaged: true,
})), "upgrade failed")
utils.ExitWithError(runner.Run(cmd.Context()), "upgrade failed")

logger.Info("upgraded command", map[string]interface{}{
"name": define.Name,
Expand Down
1 change: 1 addition & 0 deletions core/bianry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,6 @@ var _ = Describe("Bianry", func() {
afero.Exists(define.FS, filepath.Join(binDir, command2.Name)),
).To(BeTrue())
})

})
})
2 changes: 1 addition & 1 deletion core/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (s *BinariesActivator) activateBinary(name, location string) error {
binPath := filepath.Join(s.binDir, name)
s.cleanUpBinary(binPath)

linker := define.GetSymbolLinker()
linker := utils.GetSymbolLinker()
err := linker.SymlinkIfPossible(location, binPath)
if err != nil {
return errors.Wrapf(err, "create symbol link failed")
Expand Down
Loading

0 comments on commit 7e1b5db

Please sign in to comment.