Skip to content

Commit

Permalink
fix profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLYC committed Dec 18, 2021
1 parent 7e1b5db commit 5480951
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 107 deletions.
3 changes: 2 additions & 1 deletion cmd/command/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ var useCmd = &cobra.Command{
Short: "Activate a command",
Run: func(cmd *cobra.Command, args []string) {
binDir := core.GetBinDir()
shimsDir := core.GetShimsDir()

runner := core.NewStepRunner(
core.NewDBClientMaker(),
core.NewSimpleCommandsQuerier(simpleCmdFlag.name, simpleCmdFlag.version),
core.NewCommandDeactivator(),
core.NewBinariesActivator(binDir),
core.NewBinariesActivator(binDir, shimsDir),
core.NewCommandActivator(),
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var doctorCmd = &cobra.Command{
"bin": binDir,
}),
core.NewBinariesInstaller(shimsDir),
core.NewBinariesActivator(binDir),
core.NewBinariesActivator(binDir, shimsDir),
)

utils.ExitWithError(runner.Run(cmd.Context()), "doctor failed")
Expand Down
4 changes: 2 additions & 2 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var setupCmd = &cobra.Command{
Short: "Setup cmdr",
Run: func(cmd *cobra.Command, args []string) {
shimsDir := core.GetShimsDir()
binDir := core.GetBinDir()
runner := core.NewStepRunner(
core.NewDirectoryMaker(map[string]string{
"shims": shimsDir,
"bin": core.GetBinDir(),
}),
core.NewDBClientMaker(),
core.NewDBMigrator(new(model.Command)),
core.NewShellProfiler(os.Getenv("SHELL")),
)

cmdrLocation, err := os.Executable()
Expand All @@ -46,7 +46,7 @@ var setupCmd = &cobra.Command{
if !setupCmdFlag.skipProfile {
runner.Add(
core.NewStepLoggerWithFields("writing profile"),
core.NewShellProfiler(os.Getenv("SHELL")),
core.NewShellProfiler(binDir, os.Getenv("SHELL")),
)
}

Expand Down
51 changes: 34 additions & 17 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"os"
"fmt"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/mrlyc/cmdr/core"
"github.com/mrlyc/cmdr/define"
Expand All @@ -13,6 +15,7 @@ import (
var upgradeCmdFlag struct {
release string
asset string
location string
keep bool
skipSetup bool
}
Expand All @@ -22,45 +25,58 @@ var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade cmdr",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
logger := define.Logger
runner := core.NewStepRunner()
shimsDir := core.GetShimsDir()
binDir := core.GetBinDir()
cmdrLocation, err := os.Executable()
utils.CheckError(err)
runner := core.NewStepRunner(
core.NewDBClientMaker(),
core.NewCommandDefiner(shimsDir, define.Name, define.Version, upgradeCmdFlag.location, true),
)

if !upgradeCmdFlag.skipSetup {
runArgs := []string{"setup", "--upgrade"}
runArgs = append(runArgs, args...)
runner.Add(core.NewUpgradeSetupRunner(runArgs...))
if upgradeCmdFlag.location == "" {
runner.Add(
core.NewReleaseSearcher(upgradeCmdFlag.release, upgradeCmdFlag.asset),
core.NewDownloader(),
)
}

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

if !upgradeCmdFlag.keep {
runner.Add(
core.NewCommandDeactivator(),
core.NewBinariesActivator(binDir),
core.NewBinariesActivator(binDir, shimsDir),
core.NewCommandActivator(),
core.NewSimpleCommandsQuerier(
define.Name, define.Version,
),
core.NewNamedCommandsQuerier(define.Name),
core.NewCommandUndefiner(),
core.NewBinariesUninstaller(),
)
}

utils.ExitWithError(runner.Run(cmd.Context()), "upgrade failed")
utils.ExitWithError(runner.Run(ctx), "upgrade failed")

logger.Info("upgraded command", map[string]interface{}{
"name": define.Name,
})

if upgradeCmdFlag.skipSetup {
return
}

runArgs := []string{"setup", "--upgrade"}
cmd.PersistentFlags().Visit(func(f *pflag.Flag) {
runArgs = append(runArgs, fmt.Sprintf("--%s=%s", f.Name, f.Value.String()))
})
runArgs = append(runArgs, args...)

logger.Info("setup command", map[string]interface{}{
"args": runArgs,
})

utils.ExitWithError(utils.WaitProcess(ctx, filepath.Join(core.GetBinDir(), define.Name), runArgs), "setup failed")
},
}

Expand All @@ -69,6 +85,7 @@ func init() {
flags := upgradeCmd.Flags()
flags.StringVarP(&upgradeCmdFlag.release, "release", "r", "latest", "cmdr release tag name")
flags.StringVarP(&upgradeCmdFlag.asset, "asset", "a", define.Asset, "cmdr release assert name")
flags.StringVarP(&upgradeCmdFlag.location, "location", "l", "", "cmdr binary local location")
flags.BoolVarP(&upgradeCmdFlag.keep, "keep", "k", false, "keep the last cmdr version")
flags.BoolVar(&upgradeCmdFlag.skipSetup, "skip-setup", false, "do not setup after cmdr installed")
}
19 changes: 12 additions & 7 deletions core/bianry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ var _ = Describe("Bianry", func() {
Expect(err).To(BeNil())

Expect(
afero.Exists(define.FS, core.GetCommandPath(shimsDir, command1.Name, command1.Version)),
afero.Exists(define.FS, core.GetCommandShimsPath(shimsDir, command1.Name, command1.Version)),
).To(BeTrue())
Expect(
afero.Exists(define.FS, core.GetCommandPath(shimsDir, command2.Name, command2.Version)),
afero.Exists(define.FS, core.GetCommandShimsPath(shimsDir, command2.Name, command2.Version)),
).To(BeTrue())
})

Expand All @@ -89,10 +89,10 @@ var _ = Describe("Bianry", func() {
Expect(err).NotTo(BeNil())

Expect(afero.Exists(
define.FS, core.GetCommandPath(shimsDir, command1.Name, command1.Version),
define.FS, core.GetCommandShimsPath(shimsDir, command1.Name, command1.Version),
)).To(BeFalse())
Expect(afero.Exists(
define.FS, core.GetCommandPath(shimsDir, command2.Name, command2.Version),
define.FS, core.GetCommandShimsPath(shimsDir, command2.Name, command2.Version),
)).To(BeTrue())
})

Expand All @@ -103,10 +103,10 @@ var _ = Describe("Bianry", func() {
Expect(err).To(BeNil())

Expect(afero.Exists(
define.FS, core.GetCommandPath(shimsDir, command1.Name, command1.Version),
define.FS, core.GetCommandShimsPath(shimsDir, command1.Name, command1.Version),
)).To(BeTrue())
Expect(afero.Exists(
define.FS, core.GetCommandPath(shimsDir, command2.Name, command2.Version),
define.FS, core.GetCommandShimsPath(shimsDir, command2.Name, command2.Version),
)).To(BeFalse())
})
})
Expand Down Expand Up @@ -168,7 +168,12 @@ var _ = Describe("Bianry", func() {
var activator *core.BinariesActivator

BeforeEach(func() {
activator = core.NewBinariesActivator(binDir)
command1.Managed = true
command2.Managed = false

Expect(define.FS.MkdirAll(core.GetCommandShimsDir(shimsDir, command1.Name), 0755)).To(Succeed())
Expect(define.FS.Rename(command1.Location, core.GetCommandShimsPath(shimsDir, command1.Name, command1.Version))).To(Succeed())
activator = core.NewBinariesActivator(binDir, shimsDir)
})

It("context not found", func() {
Expand Down
25 changes: 16 additions & 9 deletions core/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"context"
"path/filepath"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
Expand Down Expand Up @@ -38,8 +37,8 @@ func (i *BinariesInstaller) Run(ctx context.Context) (context.Context, error) {
name := command.Name
version := command.Version
location := command.Location
dir := GetCommandDir(i.shimsDir, name)
target := GetCommandPath(i.shimsDir, name, version)
dir := GetCommandShimsDir(i.shimsDir, name)
target := GetCommandShimsPath(i.shimsDir, name, version)

logger.Info("installing binary", map[string]interface{}{
"name": name,
Expand Down Expand Up @@ -134,7 +133,8 @@ func NewBinariesUninstaller() *BinariesUninstaller {

type BinariesActivator struct {
BaseStep
binDir string
shimsDir string
binDir string
}

func (s *BinariesActivator) String() string {
Expand All @@ -143,9 +143,10 @@ func (s *BinariesActivator) String() string {

func (s *BinariesActivator) cleanUpBinary(binPath string) {
fs := define.FS
lstater := utils.GetFsLstater()
logger := define.Logger

info, err := fs.Stat(binPath)
info, _, err := lstater.LstatIfPossible(binPath)
if err != nil {
return
}
Expand All @@ -162,7 +163,7 @@ func (s *BinariesActivator) cleanUpBinary(binPath string) {
}

func (s *BinariesActivator) activateBinary(name, location string) error {
binPath := filepath.Join(s.binDir, name)
binPath := GetCommandBinPath(s.binDir, name)
s.cleanUpBinary(binPath)

linker := utils.GetSymbolLinker()
Expand Down Expand Up @@ -194,7 +195,12 @@ func (s *BinariesActivator) Run(ctx context.Context) (context.Context, error) {

var errs error
for _, command := range commands {
err = s.activateBinary(command.Name, command.Location)
location := command.Location
if command.Managed {
location = GetCommandShimsPath(s.shimsDir, command.Name, command.Version)
}

err = s.activateBinary(command.Name, location)
if err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "activate %s(%s) binary failed", command.Name, command.Version))
continue
Expand All @@ -204,8 +210,9 @@ func (s *BinariesActivator) Run(ctx context.Context) (context.Context, error) {
return ctx, errs
}

func NewBinariesActivator(binDir string) *BinariesActivator {
func NewBinariesActivator(binDir, shimsDir string) *BinariesActivator {
return &BinariesActivator{
binDir: binDir,
binDir: binDir,
shimsDir: shimsDir,
}
}
2 changes: 1 addition & 1 deletion core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (i *CommandDefiner) Commit(ctx context.Context) error {
client := GetDBClientFromContext(ctx)

if i.command.Managed {
i.command.Location = GetCommandPath(i.shimsDir, i.command.Name, i.command.Version)
i.command.Location = GetCommandShimsPath(i.shimsDir, i.command.Name, i.command.Version)
}

logger.Debug("saving command", map[string]interface{}{
Expand Down
10 changes: 7 additions & 3 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ func GetDBName() string {
return define.Configuration.GetString("database.name")
}

func GetCommandDir(shimsDir, name string) string {
func GetCommandShimsDir(shimsDir, name string) string {
return filepath.Join(shimsDir, name)
}

func GetCommandPath(shimsDir, name, version string) string {
return filepath.Join(GetCommandDir(shimsDir, name), fmt.Sprintf("%s_%s", name, version))
func GetCommandShimsPath(shimsDir, name, version string) string {
return filepath.Join(GetCommandShimsDir(shimsDir, name), fmt.Sprintf("%s_%s", name, version))
}

func GetCommandBinPath(binDir, name string) string {
return filepath.Join(binDir, name)
}
2 changes: 1 addition & 1 deletion core/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *BrokenCommandsFixer) Run(ctx context.Context) (context.Context, error)
for _, command := range commands {
location := command.Location
if command.Managed {
location = GetCommandPath(s.shimsDir, command.Name, command.Version)
location = GetCommandShimsPath(s.shimsDir, command.Name, command.Version)
}

_, err := fs.Stat(location)
Expand Down
6 changes: 3 additions & 3 deletions core/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *ShellProfiler) Run(ctx context.Context) (context.Context, error) {
return ctx, errors.Wrapf(err, "failed to get user home dir")
}

script := `eval "$(cmdr init)`
script := s.script
var profile string
switch s.shell {
case "bash":
Expand Down Expand Up @@ -81,9 +81,9 @@ func (s *ShellProfiler) Run(ctx context.Context) (context.Context, error) {
return ctx, nil
}

func NewShellProfiler(shell string) *ShellProfiler {
func NewShellProfiler(binDir, shell string) *ShellProfiler {
return &ShellProfiler{
shell: filepath.Base(shell),
script: `eval "$(cmdr init)`,
script: fmt.Sprintf(`eval "$(%s init)"`, GetCommandBinPath(binDir, define.Name)),
}
}
5 changes: 3 additions & 2 deletions core/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"

"github.com/mrlyc/cmdr/define"
)
Expand Down Expand Up @@ -68,7 +69,7 @@ func (r *StepRunner) Run(ctx context.Context) (errs error) {
"error": err,
})
failed = true
errs = multierror.Append(errs, err)
errs = multierror.Append(errs, errors.WithMessagef(err, "run on step %s", step))
break
}

Expand All @@ -90,7 +91,7 @@ func (r *StepRunner) Run(ctx context.Context) (errs error) {
"step": step,
"error": err,
})
errs = multierror.Append(errs, err)
errs = multierror.Append(errs, errors.WithMessagef(err, "commit on step %s", step))
}
}(step)
}
Expand Down
Loading

0 comments on commit 5480951

Please sign in to comment.