Skip to content

Commit

Permalink
shell: SAVVY_NEXT_STEP defaults to zero for bash and one for zsh
Browse files Browse the repository at this point in the history
zsh: arrays are 1 indexed

bash: arrays are zero indexed
  • Loading branch information
joshi4 committed May 5, 2024
1 parent dddcf03 commit 2bcd0ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 5 additions & 1 deletion shell/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions shell/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
16 changes: 10 additions & 6 deletions shell/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ", "-")
Expand All @@ -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),
}
}

0 comments on commit 2bcd0ec

Please sign in to comment.