Skip to content

Commit

Permalink
cmd/history: add selectAndExpandHistory func for future reuse
Browse files Browse the repository at this point in the history
selectAndExpandHistory is a function that abstracts over terminal/shell
details and lets users select and expands commands from their shell
history.
  • Loading branch information
joshi4 committed Aug 5, 2024
1 parent 73c05ad commit 3e28d09
Showing 1 changed file with 69 additions and 56 deletions.
125 changes: 69 additions & 56 deletions cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,71 +48,18 @@ func recordHistory(cmd *cobra.Command, _ []string) {
os.Exit(1)
}

commandProcessedChan := make(chan bool, 1)
defer close(commandProcessedChan)

hook := func(cmd string) {
logger.Debug("command recorded", "command", cmd)
commandProcessedChan <- true
}

ctx, cancelCtx := context.WithCancel(ctx)
defer cancelCtx()

ss, err := server.NewUnixSocketServerWithDefaultPath(server.WithCommandRecordedHook(hook))
if errors.Is(err, server.ErrAbortRecording) {
display.Info("Recording aborted")
return
}

historyCmds, err := selectAndExpandHistory(ctx, logger)
if err != nil {
display.FatalErrWithSupportCTA(err)
return
}
defer ss.Close()

go func() {
ss.ListenAndServe()
// kill b/g shell if we exit early
cancelCtx()
os.Exit(1)
}()

if err != nil {
display.FatalErrWithSupportCTA(err)
}

sh := shell.New("/tmp/savvy-socket")
lines, err := sh.TailHistory(ctx)
if err != nil {
display.FatalErrWithSupportCTA(err)
}

selectedHistory := allowUserToSelectCommands(lines)
if len(selectedHistory) == 0 {
display.Error(errors.New("No commands were selected"))
if len(historyCmds) == 0 {
return
}

var commands []*server.RecordedCommand
if err := huhSpinner.New().Title("Processing selected commands").Action(func() {
var err error

commands, err = expandHistory(ctx, logger, ss, sh, selectedHistory, commandProcessedChan)
if err != nil {
display.FatalErrWithSupportCTA(err)
}

if len(commands) == 0 {
display.Error(errors.New("No commands were recorded"))
return
}
}).Run(); err != nil {
logger.Debug("failed to run spinner", "error", err.Error())
}

gctx, cancel := context.WithCancel(ctx)
gm := component.NewGenerateRunbookModel(commands, cl)
gm := component.NewGenerateRunbookModel(historyCmds, cl)
p := tea.NewProgram(gm, tea.WithOutput(programOutput), tea.WithContext(gctx))
if _, err := p.Run(); err != nil {
err = fmt.Errorf("failed to generate runbook: %w", err)
Expand Down Expand Up @@ -249,3 +196,69 @@ func expandHistory(ctx context.Context,
logger.Debug("c.Wait() finished")
return srv.Commands(), nil
}

func selectAndExpandHistory(ctx context.Context, logger *slog.Logger) ([]*server.RecordedCommand, error) {
commandProcessedChan := make(chan bool, 1)
defer close(commandProcessedChan)

hook := func(cmd string) {
logger.Debug("command recorded", "command", cmd)
commandProcessedChan <- true
}

ctx, cancelCtx := context.WithCancel(ctx)
defer cancelCtx()

ss, err := server.NewUnixSocketServerWithDefaultPath(server.WithCommandRecordedHook(hook))
if errors.Is(err, server.ErrAbortRecording) {
display.Info("Recording aborted")
return nil, nil
}

if err != nil {
return nil, err
}
defer ss.Close()

go func() {
ss.ListenAndServe()
// kill b/g shell if we exit early
cancelCtx()
os.Exit(1)
}()

if err != nil {
display.FatalErrWithSupportCTA(err)
return nil, nil
}

sh := shell.New("/tmp/savvy-socket")
lines, err := sh.TailHistory(ctx)
if err != nil {
return nil, err
}

selectedHistory := allowUserToSelectCommands(lines)
if len(selectedHistory) == 0 {
return nil, nil
}

var commands []*server.RecordedCommand
if err := huhSpinner.New().Title("Processing selected commands").Action(func() {
var err error

commands, err = expandHistory(ctx, logger, ss, sh, selectedHistory, commandProcessedChan)
if err != nil {
display.FatalErrWithSupportCTA(err)
return
}

if len(commands) == 0 {
display.Error(errors.New("No commands were recorded"))
return
}
}).Run(); err != nil {
logger.Debug("failed to run spinner", "error", err.Error())
}
return commands, nil
}

0 comments on commit 3e28d09

Please sign in to comment.