Skip to content

Commit

Permalink
add -e multi-flag for custom shell
Browse files Browse the repository at this point in the history
  • Loading branch information
akavel committed Oct 29, 2020
1 parent 5786314 commit 5dc341c
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var (
outputScript = pflag.StringP("output-script", "o", "", "save the command to specified `file` if Ctrl-X is pressed (default: up<N>.sh)")
debugMode = pflag.Bool("debug", false, "debug mode")
noColors = pflag.Bool("no-colors", false, "disable interface colors")
shellFlag = pflag.StringArrayP("exec", "e", nil, "`command` to pass $PIPELINE through; use multiple times to pass multi-word command; defaults to '-e=$SHELL -e=-c'")
)

func main() {
Expand All @@ -93,26 +94,28 @@ func main() {

// Find out what is the user's preferred login shell. This also allows user
// to choose the "engine" used for command execution.
log.Println("checking $SHELL...")
shell := os.Getenv("SHELL")
{
if shell != "" {
shell := *shellFlag
if len(shell) == 0 {
log.Println("checking $SHELL...")
sh := os.Getenv("SHELL")
if sh != "" {
goto shell_found
}
log.Println("checking bash...")
shell, _ = exec.LookPath("bash")
if shell != "" {
sh, _ = exec.LookPath("bash")
if sh != "" {
goto shell_found
}
log.Println("checking sh...")
shell, _ = exec.LookPath("sh")
if shell != "" {
sh, _ = exec.LookPath("sh")
if sh != "" {
goto shell_found
}
die("cannot find shell: $SHELL is empty, neither bash nor sh are in $PATH")
die("cannot find shell: no -e flag, $SHELL is empty, neither bash nor sh are in $PATH")
shell_found:
log.Println("found shell:", shell)
shell = []string{sh, "-c"}
}
log.Println("found shell:", shell)

// Initialize TUI infrastructure
tui := initTUI()
Expand Down Expand Up @@ -662,15 +665,15 @@ type Subprocess struct {
cancel context.CancelFunc
}

func StartSubprocess(shell, command string, stdin *Buf, notify func()) *Subprocess {
func StartSubprocess(shell []string, command string, stdin *Buf, notify func()) *Subprocess {
ctx, cancel := context.WithCancel(context.TODO())
r, w := io.Pipe()
p := &Subprocess{
Buf: NewBuf().StartCapturing(r, notify),
cancel: cancel,
}

cmd := exec.CommandContext(ctx, shell, "-c", command)
cmd := exec.CommandContext(ctx, shell[0], append(shell[1:], command)...)
cmd.Stdout = w
cmd.Stderr = w
cmd.Stdin = stdin.NewReader(true)
Expand Down Expand Up @@ -705,7 +708,7 @@ func getKey(ev *tcell.EventKey) key { return key(ev.Modifiers())<<16 + key(ev.Ke
func altKey(base tcell.Key) key { return key(tcell.ModAlt)<<16 + key(base) }
func ctrlKey(base tcell.Key) key { return key(tcell.ModCtrl)<<16 + key(base) }

func writeScript(shell, command string, tui tcell.Screen) {
func writeScript(shell []string, command string, tui tcell.Screen) {
os.Stderr.WriteString("up: Ultimate Plumber v" + version + " https://github.com/akavel/up\n")
var f *os.File
var err error
Expand Down Expand Up @@ -735,7 +738,8 @@ func writeScript(shell, command string, tui tcell.Screen) {
goto fallback_tmp

try_file:
_, err = fmt.Fprintf(f, "#!%s\n%s\n", shell, command)
// NOTE: currently not supporting multi-word shell in upNNN.sh unfortunately :(
_, err = fmt.Fprintf(f, "#!%s\n%s\n", shell[0], command)
if err != nil {
goto fallback_tmp
}
Expand Down

0 comments on commit 5dc341c

Please sign in to comment.