diff --git a/shell/bash.go b/shell/bash.go index 7cf1b00..3cc9c93 100644 --- a/shell/bash.go +++ b/shell/bash.go @@ -204,7 +204,11 @@ func (b *bash) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook) } cmd := exec.CommandContext(ctx, b.shellCmd, "--rcfile", bashrc.Name()) - cmd.Env = append(os.Environ(), runbookRunMetadata(runbook)...) + cmd.Env = append(os.Environ(), runbookRunMetadata(runbook, b)...) cmd.WaitDelay = 500 * time.Millisecond return cmd, nil } + +func (b *bash) DefaultStartingArrayIndex() int { + return 0 +} diff --git a/shell/spawn.go b/shell/spawn.go index 848e5ce..12dc4b3 100644 --- a/shell/spawn.go +++ b/shell/spawn.go @@ -15,6 +15,7 @@ type Shell interface { TailHistory(ctx context.Context) ([]string, error) SpawnHistoryExpander(ctx context.Context) (*exec.Cmd, error) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook) (*exec.Cmd, error) + DefaultStartingArrayIndex() int } func New(logTarget string) Shell { @@ -54,3 +55,7 @@ func (t *todo) SpawnHistoryExpander(ctx context.Context) (*exec.Cmd, error) { func (t *todo) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook) (*exec.Cmd, error) { return nil, errors.New("savvy doesn't support your current shell") } + +func (t *todo) DefaultStartingArrayIndex() int { + return 0 +} diff --git a/shell/zsh.go b/shell/zsh.go index 746f4fc..3167f32 100644 --- a/shell/zsh.go +++ b/shell/zsh.go @@ -248,11 +248,15 @@ func (z *zsh) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook) ( cmd := exec.CommandContext(ctx, z.shellCmd) cmd.Env = append(os.Environ(), "ZDOTDIR="+tmp) - cmd.Env = append(cmd.Env, runbookRunMetadata(runbook)...) + cmd.Env = append(cmd.Env, runbookRunMetadata(runbook, z)...) cmd.WaitDelay = 500 * time.Millisecond return cmd, nil } +func (z *zsh) DefaultStartingArrayIndex() int { + return 1 +} + func computeRunbookAlias(runbook *client.Runbook) string { lc := strings.ToLower(runbook.Title) alias := strings.ReplaceAll(lc, " ", "-") @@ -261,28 +265,28 @@ func computeRunbookAlias(runbook *client.Runbook) string { return alias } -func nextRunbookStepToRun() int { +func nextRunbookStepToRun(sh Shell) int { // Inherit the next step from the environment if we are in a subshell nextStep := os.Getenv("SAVVY_NEXT_STEP") if nextStep == "" { - nextStep = "1" + return sh.DefaultStartingArrayIndex() } idx, err := strconv.Atoi(nextStep) if err != nil { - return 1 + return sh.DefaultStartingArrayIndex() } return idx } -func runbookRunMetadata(runbook *client.Runbook) []string { +func runbookRunMetadata(runbook *client.Runbook, sh Shell) []string { runbookCommands := strings.Join(runbook.Commands(), RunbookCommandDelimiter) runbookAlias := computeRunbookAlias(runbook) return []string{ "SAVVY_CONTEXT=run", fmt.Sprintf("SAVVY_RUNBOOK_COMMANDS=%s", runbookCommands), - fmt.Sprintf("SAVVY_NEXT_STEP=%d", nextRunbookStepToRun()), + fmt.Sprintf("SAVVY_NEXT_STEP=%d", nextRunbookStepToRun(sh)), fmt.Sprintf("SAVVY_RUNBOOK_ALIAS=%s", runbookAlias), } }