Skip to content

Commit

Permalink
tail logs after restarting a resource (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitemongerer authored Nov 14, 2024
1 parent eafaa94 commit d436154
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
7 changes: 1 addition & 6 deletions cmd/deploycreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ var InteractiveDeployCreate = func(ctx context.Context, input types.DeployInput,
breadcrumb,
&input,
views.NewDeployCreateView(ctx, input, func(d *client.Deploy) tea.Cmd {
return InteractiveLogs(
ctx,
views.LogInput{
ResourceIDs: []string{input.ServiceID},
Tail: true,
}, "Logs")
return TailResourceLogs(ctx, input.ServiceID)
}))
}

Expand Down
9 changes: 9 additions & 0 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func nonInteractiveLogs(format *command.Output, cmd *cobra.Command, input views.
return nil
}

func TailResourceLogs(ctx context.Context, resourceID string) tea.Cmd {
return InteractiveLogs(
ctx,
views.LogInput{
ResourceIDs: []string{resourceID},
Tail: true,
}, "Logs")
}

func InteractiveLogs(ctx context.Context, input views.LogInput, breadcrumb string) tea.Cmd {
return command.AddToStackFunc(
ctx,
Expand Down
4 changes: 3 additions & 1 deletion cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ var restartCmd = &cobra.Command{
}

var InteractiveRestart = func(ctx context.Context, input views.RestartInput, breadcrumb string) tea.Cmd {
return command.AddToStackFunc(ctx, restartCmd, breadcrumb, &input, views.NewRestartView(ctx, input))
return command.AddToStackFunc(ctx, restartCmd, breadcrumb, &input, views.NewRestartView(ctx, input, func() tea.Cmd {
return TailResourceLogs(ctx, input.ResourceID)
}))
}

func init() {
Expand Down
21 changes: 13 additions & 8 deletions pkg/tui/views/restartview.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ func RestartResource(ctx context.Context, input RestartInput) (string, error) {
}

type RestartView struct {
model *tui.SimpleModel
restart tui.TypedCmd[string]
logs func() tea.Cmd
}

func NewRestartView(ctx context.Context, input RestartInput) *RestartView {
func NewRestartView(ctx context.Context, input RestartInput, logCmd func() tea.Cmd) *RestartView {
return &RestartView{
model: tui.NewSimpleModel(command.WrapInConfirm(
logs: logCmd,
restart: command.WrapInConfirm(
command.LoadCmd(ctx, RestartResource, input),
func() (string, error) {
res, err := resource.GetResource(ctx, input.ResourceID)
Expand All @@ -73,19 +75,22 @@ func NewRestartView(ctx context.Context, input RestartInput) *RestartView {

return fmt.Sprintf("Are you sure you want to restart resource %s?", res.Name()), nil
},
)),
),
}
}

func (v *RestartView) Init() tea.Cmd {
return v.model.Init()
return v.restart.Unwrap()
}

func (v *RestartView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
_, cmd := v.model.Update(msg)
return v, cmd
switch msg.(type) {
case tui.LoadDataMsg[string]:
return v, v.logs()
}
return v, nil
}

func (v *RestartView) View() string {
return v.model.View()
return "Loading..."
}

0 comments on commit d436154

Please sign in to comment.