Skip to content

Commit

Permalink
support CMDR_PROFILE_PATH env
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLYC committed Dec 20, 2021
1 parent 257869c commit 470afb5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var setupCmd = &cobra.Command{
if !setupCmdFlag.skipProfile {
runner.Add(
core.NewStepLoggerWithFields("writing profile"),
core.NewShellProfiler(binDir, os.Getenv("SHELL")),
core.NewShellProfiler(binDir),
)
}

Expand Down
1 change: 1 addition & 0 deletions core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ var (
ErrCommandNotExists = fmt.Errorf("command not exists")
ErrContextValueNotFound = fmt.Errorf("context value not found")
ErrAssetNotFound = fmt.Errorf("asset not found")
ErrNotSupported = fmt.Errorf("not supported")
)
44 changes: 27 additions & 17 deletions core/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

type ShellProfiler struct {
BaseStep
shell string
script string
}

Expand All @@ -41,26 +40,38 @@ func (s *ShellProfiler) isContainsProfile(path string) bool {
return bytes.Contains(content, []byte(s.script))
}

func (s *ShellProfiler) Run(ctx context.Context) (context.Context, error) {
fs := define.FS
logger := define.Logger
func (s *ShellProfiler) getProfilePath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return ctx, errors.Wrapf(err, "failed to get user home dir")
return "", errors.Wrapf(err, "failed to get user home dir")
}

script := s.script
var profile string
switch s.shell {
profilePath := os.Getenv("CMDR_PROFILE_PATH")
if profilePath != "" {
return profilePath, nil
}

shell := filepath.Base(os.Getenv("SHELL"))

switch shell {
case "bash":
profile = filepath.Join(homeDir, ".bashrc")
profilePath = filepath.Join(homeDir, ".bashrc")
case "zsh":
profile = filepath.Join(homeDir, ".zshrc")
profilePath = filepath.Join(homeDir, ".zshrc")
default:
logger.Warn("shell is not supported, please execute this script to init cmdr environment", map[string]interface{}{
"shell": s.shell,
"script": script,
})
return "", errors.Wrapf(ErrNotSupported, shell)
}

return profilePath, nil
}

func (s *ShellProfiler) Run(ctx context.Context) (context.Context, error) {
fs := define.FS

script := s.script
profile, err := s.getProfilePath()
if err != nil {
return ctx, err
}

if s.isContainsProfile(profile) {
Expand All @@ -73,17 +84,16 @@ func (s *ShellProfiler) Run(ctx context.Context) (context.Context, error) {
}
defer utils.CallClose(file)

_, err = fmt.Fprintf(file, "\n%s\n", script)
_, err = fmt.Fprintf(file, "\n%s", script)
if err != nil {
return ctx, errors.Wrapf(err, "failed to write to profile file")
}

return ctx, nil
}

func NewShellProfiler(binDir, shell string) *ShellProfiler {
func NewShellProfiler(binDir string) *ShellProfiler {
return &ShellProfiler{
shell: filepath.Base(shell),
script: fmt.Sprintf(`eval "$(%s init)"`, GetCommandBinPath(binDir, define.Name)),
}
}

0 comments on commit 470afb5

Please sign in to comment.