Skip to content

Commit

Permalink
Check shell using env var instead
Browse files Browse the repository at this point in the history
  • Loading branch information
saheljalal committed Sep 7, 2019
1 parent dbd4a94 commit 73815a7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
)

var useZsh bool
var writeCompletion bool

// completionCmd represents the completion command
Expand Down
6 changes: 1 addition & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"github.com/spf13/cobra"
)

var useZsh bool

// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run [command] [args]",
Expand Down Expand Up @@ -40,12 +38,10 @@ commands that need to be run across the scope of the command.`,
Args: cobra.MinimumNArgs(1),
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
task.Run(args, useZsh)
task.Run(args)
},
}

func init() {
rootCmd.AddCommand(runCmd)

runCmd.Flags().BoolVarP(&useZsh, "zsh", "z", false, "Use zsh for shell exec")
}
17 changes: 13 additions & 4 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const (
)

// Run a command on the shell
func Run(command string, shell Shell, verbose bool) error {
func Run(command string, verbose bool) error {
if len(command) == 0 {
return fmt.Errorf("cannot run empty command")
}

command = strings.TrimSuffix(command, "\n")

name, args := buildExecArgs(shell, command)
name, args := buildExecArgs(command)
if verbose {
log.Debugf("executing: %s %s\n", name, strings.Join(args, " "))
}
Expand Down Expand Up @@ -88,8 +88,17 @@ func InitFileLines() (string, error) {
return prefFiles[0].makeAliasBlock(), nil
}

func buildExecArgs(sh Shell, cmd string) (string, []string) {
if sh == Zsh {
// Which shell is currently running
func Which() Shell {
sh := os.Getenv("SHELL")
if strings.Contains(sh, "zsh") {
return Zsh
}
return Bash
}

func buildExecArgs(cmd string) (string, []string) {
if Which() == Zsh {
return "zsh", []string{"-ic", cmd}
}
return "bash", []string{"-ic", cmd}
Expand Down
7 changes: 1 addition & 6 deletions shell/startupfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,9 @@ func (s *startupFile) makeAliasBlock() string {
return ""
}

zsh := ""
if strings.Contains(s.path, "zsh") {
zsh = "--zsh"
}

aliases := []string{}
for _, a := range s.aliases {
alias := strings.TrimSpace(fmt.Sprintf("alias %s='nostromo run %s \"$*\"' %s", a, a, zsh))
alias := strings.TrimSpace(fmt.Sprintf("alias %s='nostromo run %s \"$*\"'", a, a))
aliases = append(aliases, alias)
}
return fmt.Sprintf("\n%s\n%s\n%s\n", beginBlockComment, strings.Join(aliases, "\n"), endBlockComment)
Expand Down
8 changes: 2 additions & 6 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func RemoveSubstitution(keyPath, alias string) {
}

// Run a command from the manifest
func Run(args []string, useZsh bool) {
func Run(args []string) {
cfg := checkConfig()
if cfg == nil {
return
Expand All @@ -208,11 +208,7 @@ func Run(args []string, useZsh bool) {
return
}

sh := shell.Bash
if useZsh {
sh = shell.Zsh
}
err = shell.Run(cmd, sh, cfg.Manifest.Config.Verbose)
err = shell.Run(cmd, cfg.Manifest.Config.Verbose)
if err != nil {
log.Error(err)
}
Expand Down

0 comments on commit 73815a7

Please sign in to comment.