Skip to content

Commit

Permalink
cmd/internal/previous: allow users to go back when executing runbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Aug 5, 2024
1 parent fa0f694 commit 73c05ad
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/internal/previous.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package internal

import (
"context"
"fmt"
"os"

"github.com/getsavvyinc/savvy-cli/display"
"github.com/getsavvyinc/savvy-cli/server/run"
"github.com/spf13/cobra"
)

// previousCommand represents the next command
var previousCmd = &cobra.Command{
Use: "previous",
Hidden: true,
Short: "Update runbook state to the previous step",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
cl, err := run.NewDefaultClient(ctx)
if err != nil {
display.ErrorWithSupportCTA(err)
return
}

state, err := cl.CurrentState()
if err != nil {
display.ErrorWithSupportCTA(err)
os.Exit(1)
}

if forcePrevious {
updated, err := previousCommand(ctx, cl)
if err != nil {
display.ErrorWithSupportCTA(err)
os.Exit(1)
}
fmt.Printf("%d", updated.Index)
return
}
fmt.Printf("%d", state.Index)
},
}

func previousCommand(ctx context.Context, cl run.Client) (*run.State, error) {
if err := cl.PreviousCommand(); err != nil {
return nil, err
}

updatedState, err := cl.CurrentState()
if err != nil {
return nil, err
}
return updatedState, nil
}

var forcePrevious bool

func init() {
InternalCmd.AddCommand(previousCmd)
previousCmd.Flags().BoolVarP(&forcePrevious, "force", "f", false, "force previous command regardless of current state")
}

0 comments on commit 73c05ad

Please sign in to comment.