Skip to content

Commit

Permalink
cmd/ask: Save and run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Jul 25, 2024
1 parent 50635de commit 3017db7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
88 changes: 49 additions & 39 deletions cmd/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,50 @@ var askCmd = &cobra.Command{
}

if state.createRunbook {
result, err := cl.SaveRunbook(ctx, state.runbook)
result, err := createRunbook(ctx, cl, state.runbook)
if err != nil {
if errors.Is(err, client.ErrCannotUseGuest) {
loginAndCreateRunbook(ctx, cl, state.runbook)
return
}
display.Error(err)
return
display.ErrorWithSupportCTA(err)
os.Exit(1)
}
display.Success("Runbook created successfully!")
display.Success(fmt.Sprintf("Runbook %s created successfully!", result.Runbook.Title))
browser.Open(result.URL)
return
}

if state.createRunbookAndExecute {
result, err := createRunbook(ctx, cl, state.runbook)
if err != nil {
display.ErrorWithSupportCTA(err)
os.Exit(1)
}
display.Success("Runbook created successfully!")
if err := runRunbook(ctx, &result.Runbook); err != nil {
display.ErrorWithSupportCTA(
fmt.Errorf("failed to run runbook %s: %w", result.Runbook.Title, err),
)
return
}
}
},
}

func loginAndCreateRunbook(ctx context.Context, cl client.Client, runbook *client.Runbook) {
func createRunbook(ctx context.Context, cl client.Client, runbook *client.Runbook) (*client.GeneratedRunbook, error) {
result, err := cl.SaveRunbook(ctx, runbook)
if err == nil {
return result, nil
} else if errors.Is(err, client.ErrCannotUseGuest) {
return loginAndCreateRunbook(ctx, runbook)
}
return result, err
}

func loginAndCreateRunbook(ctx context.Context, runbook *client.Runbook) (*client.GeneratedRunbook, error) {
runLogin()
// then create Runbook
cl, err := client.New()
if err != nil {
display.Error(err)
os.Exit(1)
}

result, err := cl.SaveRunbook(ctx, runbook)
if err != nil {
display.Error(err)
os.Exit(1)
return nil, err
}

display.Success("Runbook created successfully!")

browser.Open(result.URL)
return cl.SaveRunbook(ctx, runbook)
}

type AskParams struct {
Expand All @@ -141,11 +151,11 @@ type AskParams struct {
}

type runAskTerminalState struct {
selectedCommand string
refinePrompt bool
createRunbook bool
execute bool
runbook *client.Runbook
selectedCommand string
refinePrompt bool
createRunbook bool
createRunbookAndExecute bool
runbook *client.Runbook
}

func runAsk(ctx context.Context, cl client.Client, question string, askParams *AskParams) *runAskTerminalState {
Expand Down Expand Up @@ -221,26 +231,26 @@ func runAsk(ctx context.Context, cl client.Client, question string, askParams *A

if m, ok := result.(*askCommands); ok {
return &runAskTerminalState{
selectedCommand: m.l.SelectedCommand(),
refinePrompt: m.refinePrompt,
createRunbook: m.saveAsRunbook,
execute: m.executeCommands,
runbook: runbook,
selectedCommand: m.l.SelectedCommand(),
refinePrompt: m.refinePrompt,
createRunbook: m.saveAsRunbook,
createRunbookAndExecute: m.createRunbookAndExecute,
runbook: runbook,
}
}
return nil
}

type askCommands struct {
l list.Model
refinePrompt bool
saveAsRunbook bool
executeCommands bool
l list.Model
refinePrompt bool
saveAsRunbook bool
createRunbookAndExecute bool
}

var RefinePromptHelpBinding = list.NewHelpBinding("p", "refine prompt")
var SaveAsRunbookHelpBinding = list.NewHelpBinding("s", "save as runbook")
var ExecuteCommandsHelpBinding = list.NewHelpBinding("x", "execute commands")
var ExecuteCommandsHelpBinding = list.NewHelpBinding("r", "save and run commands")

func newAskCommandsModel(runbook *component.Runbook) (*askCommands, error) {
if runbook == nil {
Expand All @@ -266,8 +276,8 @@ func (dc *askCommands) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case list.SaveAsRunbookMsg:
dc.saveAsRunbook = true
return dc, tea.Quit
case list.ExecuteCommandsMsg:
dc.executeCommands = true
case list.SaveAsRunbookAndExecuteMsg:
dc.createRunbookAndExecute = true
return dc, tea.Quit
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/component/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type NopMsg struct{}

type RefinePromptMsg struct{}
type SaveAsRunbookMsg struct{}
type ExecuteCommandsMsg struct{}
type SaveAsRunbookAndExecuteMsg struct{}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
Expand All @@ -148,8 +148,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, func() tea.Msg { return SaveAsRunbookMsg{} }
}

if msg.String() == "x" && m.list.FilterState() == list.Unfiltered && slice.Has(m.helpKeys, "x") {
return m, func() tea.Msg { return ExecuteCommandsMsg{} }
if msg.String() == "r" && m.list.FilterState() == list.Unfiltered && slice.Has(m.helpKeys, "r") {
return m, func() tea.Msg { return SaveAsRunbookAndExecuteMsg{} }
}

case tea.WindowSizeMsg:
Expand Down

0 comments on commit 3017db7

Please sign in to comment.