Skip to content

Commit

Permalink
cmd/run: savvy run works for zsh
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Apr 27, 2024
1 parent 66a4c41 commit a9de502
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
25 changes: 13 additions & 12 deletions cmd/setup/savvy.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
SAVVY_INPUT_FILE=/tmp/savvy-socket

autoload -Uz add-zsh-hook
autoload -Uz add-zle-hook-widget

step_id=""

Expand Down Expand Up @@ -32,31 +33,31 @@ function __savvy_record_pre_exec__() {
function __savvy_run_pre_exec__() {
local cmd=$3
if [[ "${SAVVY_CONTEXT}" == "run" ]] ; then
if [[ "${cmd}" -eq "${SAVVY_COMMANDS[SAVVY_NEXT_STEP]}" ]] ; then
export SAVVY_NEXT_STEP=$((SAVVY_NEXT_STEP+1))
if [[ "${cmd}" == "${SAVVY_COMMANDS[SAVVY_NEXT_STEP]}" ]] ; then
SAVVY_NEXT_STEP=$((SAVVY_NEXT_STEP+1))
fi
fi
}

# SAVVY_RUNBOOK_COMMANDS is a list of commands that savvy should run in the run context

SAVVY_COMMANDS=("${(s:,:)SAVVY_RUNBOOK_COMMANDS}")
num_commands=${#SAVVY_COMMANDS}
function __savvy_runbook_runner__() {
next_step_idx=${SAVVY_NEXT_STEP:1}
BUFFER=${SAVVY_COMMANDS[next_step_idx]} # Initial text to be edited by the user
zle end-of-line # Accept the line for editing
if [[ "${SAVVY_CONTEXT}" == "run" ]] ; then
next_step_idx=${SAVVY_NEXT_STEP}
BUFFER="${SAVVY_COMMANDS[next_step_idx]}"
zle end-of-line # Accept the line for editing
fi
}



# NOTE: If you change any function names, you must also change the corresponding check in shell/check_setup.go, shell/zsh.go
#
# TODO: use templates to avoid the need to manually change shell checks

SAVVY_COMMANDS=()
SAVVY_NEXT_STEP=1
if [[ "${SAVVY_CONTEXT}" == "run" ]] ; then
zle -N zle-line-init __savvy_runbook_runner__
add-zle-hook-widget zle-line-init
add-zle-hook-widget line-init __savvy_runbook_runner__
# SAVVY_RUNBOOK_COMMANDS is a list of commands that savvy should run in the run context
SAVVY_COMMANDS=("${(s:,:)SAVVY_RUNBOOK_COMMANDS}")
fi

add-zsh-hook preexec __savvy_record_pre_exec__
Expand Down
9 changes: 8 additions & 1 deletion shell/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/user"
"path/filepath"
"slices"
"strconv"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -249,8 +250,14 @@ func (z *zsh) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook) (
nextStep = "1"
}

var nextStepIdx int
nextStepIdx, err = strconv.Atoi(nextStep)
if err != nil {
nextStepIdx = 1
}

cmd := exec.CommandContext(ctx, z.shellCmd)
cmd.Env = append(os.Environ(), "ZDOTDIR="+tmp, "SAVVY_CONTEXT=run", fmt.Sprintf("SAVVY_RUNBOOK_COMMANDS=%s", strings.Join(runbook.Commands(), ",")), fmt.Sprintf("SAVVY_NEXT_STEP=%s", nextStep))
cmd.Env = append(os.Environ(), "ZDOTDIR="+tmp, "SAVVY_CONTEXT=run", fmt.Sprintf("SAVVY_RUNBOOK_COMMANDS=%s", strings.Join(runbook.Commands(), ",")), fmt.Sprintf("SAVVY_NEXT_STEP=%d", nextStepIdx))
cmd.WaitDelay = 500 * time.Millisecond
return cmd, nil
}

0 comments on commit a9de502

Please sign in to comment.