Skip to content

Commit

Permalink
Wrapper returns cmd for loading data (#74)
Browse files Browse the repository at this point in the history
* Add TypeCmd

* Updating cmds to use new TypedCmd

* Run confirmation on load data

* Update exec commands

* Write errors at top of stack

* Handle loading at the top level

* Add comment

* Remove extra init
  • Loading branch information
mdbenjam authored Oct 25, 2024
1 parent a193bbc commit 5bd0447
Show file tree
Hide file tree
Showing 25 changed files with 268 additions and 409 deletions.
31 changes: 5 additions & 26 deletions cmd/deploycreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func createDeploy(ctx context.Context, input types.DeployInput) (*client.Deploy,
return d, nil
}

func renderCreateDeploy(ctx context.Context, loadData func(types.DeployInput) (*client.Deploy, error), input types.DeployInput) (tea.Model, error) {
func renderCreateDeploy(ctx context.Context, loadData func(types.DeployInput) tui.TypedCmd[*client.Deploy], input types.DeployInput) (tea.Model, error) {
c, err := client.NewDefaultClient()
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
Expand Down Expand Up @@ -108,37 +108,16 @@ func renderCreateDeploy(ctx context.Context, loadData func(types.DeployInput) (*
}

deployForm := huh.NewForm(huh.NewGroup(inputs...))

logData := func(in LogInput) (*LogResult, error) {
return loadLogData(ctx, in)
}

logModelFunc := func(string) (tea.Model, error) {
model, err := renderLogs(ctx, logData, LogInput{
logAction := func(_ *client.Deploy) tea.Cmd {
return InteractiveLogs(ctx, LogInput{
ResourceIDs: []string{input.ServiceID},
Tail: true,
})
if err != nil {
return nil, err
}
model.Init()
return model, nil
}

onSubmit := func() tea.Cmd {
return func() tea.Msg {
_, err := loadData(input)
if err != nil {
return tui.ErrorMsg{Err: fmt.Errorf("failed to trigger deploy: %w", err)}
}

return tea.Println("Deploy triggered")
}
}

action := tui.NewFormAction(
logModelFunc,
onSubmit,
logAction,
loadData(input),
)

return tui.NewFormWithAction(action, deployForm), nil
Expand Down
8 changes: 2 additions & 6 deletions cmd/deploylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ func loadDeployList(ctx context.Context, input DeployListInput) ([]*client.Deplo
return deploys, nil
}

func renderDeployList(ctx context.Context, loadData func(DeployListInput) ([]*client.Deploy, error), input DeployListInput) (tea.Model, error) {
loadFunc := func() ([]*client.Deploy, error) {
return loadData(input)
}

func renderDeployList(ctx context.Context, loadData func(DeployListInput) tui.TypedCmd[[]*client.Deploy], input DeployListInput) (tea.Model, error) {
list := tui.NewList(
"Deploys",
loadFunc,
loadData(input),
func(d *client.Deploy) tui.ListItem {
return deploy.NewListItem(d)
},
Expand Down
8 changes: 2 additions & 6 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ func loadEnvironments(ctx context.Context, in EnvironmentInput) ([]*client.Envir
return environmentRepo.ListEnvironments(ctx, in.ToParams())
}

func renderEnvironments(ctx context.Context, loadData func(EnvironmentInput) ([]*client.Environment, error), input EnvironmentInput) (tea.Model, error) {
func renderEnvironments(ctx context.Context, loadData func(EnvironmentInput) tui.TypedCmd[[]*client.Environment], input EnvironmentInput) (tea.Model, error) {
columns := []btable.Column{
btable.NewColumn("ID", "ID", 25).WithFiltered(true),
btable.NewFlexColumn("Name", "Name", 3).WithFiltered(true),
btable.NewFlexColumn("Project", "Project", 3).WithFiltered(true),
btable.NewFlexColumn("Protected", "Protected", 2).WithFiltered(true),
}

loadDataFunc := func() ([]*client.Environment, error) {
return loadData(input)
}

createRowFunc := func(env *client.Environment) btable.Row {
return btable.NewRow(btable.RowData{
"ID": env.Id,
Expand Down Expand Up @@ -93,7 +89,7 @@ func renderEnvironments(ctx context.Context, loadData func(EnvironmentInput) ([]

t := tui.NewTable(
columns,
loadDataFunc,
loadData(input),
createRowFunc,
onSelect,
tui.WithCustomOptions[*client.Environment](customOptions),
Expand Down
24 changes: 9 additions & 15 deletions cmd/jobcancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/renderinc/render-cli/pkg/client"
clientjob "github.com/renderinc/render-cli/pkg/client/jobs"
"github.com/renderinc/render-cli/pkg/command"
"github.com/renderinc/render-cli/pkg/job"
"github.com/renderinc/render-cli/pkg/service"
Expand Down Expand Up @@ -44,28 +43,23 @@ type JobCancelInput struct {
JobID string `cli:"arg:1"`
}

func cancelJob(ctx context.Context, input JobCancelInput) (*clientjob.Job, error) {
func cancelJob(ctx context.Context, input JobCancelInput) (string, error) {
c, err := client.NewDefaultClient()
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
return "", fmt.Errorf("failed to create client: %w", err)
}

jobRepo := job.NewRepo(c)

return jobRepo.CancelJob(ctx, input.ServiceID, input.JobID)
}

func renderJobCancel(ctx context.Context, cancelJobFunc func(JobCancelInput) (*clientjob.Job, error), input JobCancelInput) (tea.Model, error) {
loadFunc := func() (string, error) {
j, err := cancelJobFunc(input)
if err != nil {
return "", fmt.Errorf("failed to cancel job: %w", err)
}

return fmt.Sprintf("Job %s successfuly cancelled", j.Id), nil
_, err = jobRepo.CancelJob(ctx, input.ServiceID, input.JobID)
if err != nil {
return "", fmt.Errorf("failed to cancel job: %w", err)
}
return fmt.Sprintf("Job %s successfuly cancelled", input.JobID), nil
}

return tui.NewSimpleModel(loadFunc), nil
func renderJobCancel(ctx context.Context, cancelJobFunc func(JobCancelInput) tui.TypedCmd[string], input JobCancelInput) (tea.Model, error) {
return tui.NewSimpleModel(cancelJobFunc(input)), nil
}

func init() {
Expand Down
31 changes: 6 additions & 25 deletions cmd/jobcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,24 @@ func createJob(ctx context.Context, input JobCreateInput) (*clientjob.Job, error
})
}

func renderJobCreate(ctx context.Context, createJobFunc func(JobCreateInput) (*clientjob.Job, error), in JobCreateInput) (tea.Model, error) {
func renderJobCreate(ctx context.Context, createJobFunc func(JobCreateInput) tui.TypedCmd[*clientjob.Job], in JobCreateInput) (tea.Model, error) {
form, result := command.HuhForm(jobCreateCmd, &in)
var jobCreateInput JobCreateInput
err := command.StructFromFormValues(result, &jobCreateInput)
if err != nil {
return nil, err
}

logData := func(in LogInput) (*LogResult, error) {
return loadLogData(ctx, in)
}

logModelFunc := func(resourceID string) (tea.Model, error) {
model, err := renderLogs(ctx, logData, LogInput{
ResourceIDs: []string{resourceID},
logAction := func(j *clientjob.Job) tea.Cmd {
return InteractiveLogs(ctx, LogInput{
ResourceIDs: []string{j.Id},
Tail: true,
})
if err != nil {
return nil, err
}
return model, nil
}

onSubmit := func() tea.Cmd {
return func() tea.Msg {
createdJob, err := createJobFunc(jobCreateInput)
if err != nil {
return tui.ErrorMsg{Err: fmt.Errorf("failed to create job: %w", err)}
}

return tui.SubmittedMsg{ID: createdJob.Id}
}
}

action := tui.NewFormAction(
logModelFunc,
onSubmit,
logAction,
createJobFunc(jobCreateInput),
)

return tui.NewFormWithAction(action, form), nil
Expand Down
8 changes: 2 additions & 6 deletions cmd/joblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ func loadJobListData(ctx context.Context, input JobListInput) ([]*clientjob.Job,
})
}

func renderJobList(ctx context.Context, loadData func(JobListInput) ([]*clientjob.Job, error), input JobListInput) (tea.Model, error) {
loadFunc := func() ([]*clientjob.Job, error) {
return loadData(input)
}

func renderJobList(ctx context.Context, loadData func(JobListInput) tui.TypedCmd[[]*clientjob.Job], input JobListInput) (tea.Model, error) {
list := tui.NewList(
"Jobs",
loadFunc,
loadData(input),
func(j *clientjob.Job) tui.ListItem {
return job.NewListItem(j)
},
Expand Down
31 changes: 7 additions & 24 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ type LogInput struct {
Tail bool `cli:"tail"`
}

type LogResult struct {
Logs *client.Logs200Response
LogChannel <-chan *lclient.Log
}

func (l LogInput) ToParam() (*client.ListLogsParams, error) {
now := time.Now()
ownerID, err := config.WorkspaceID()
Expand Down Expand Up @@ -107,7 +102,7 @@ func mapDirection(direction string) lclient.LogDirection {
}
}

func loadLogData(ctx context.Context, in LogInput) (*LogResult, error) {
func loadLogData(ctx context.Context, in LogInput) (*tui.LogResult, error) {
c, err := client.NewDefaultClient()
if err != nil {
return nil, err
Expand All @@ -126,14 +121,14 @@ func loadLogData(ctx context.Context, in LogInput) (*LogResult, error) {
if err != nil {
return nil, fmt.Errorf("error tailing logs: %v", err)
}
return &LogResult{Logs: &client.Logs200Response{}, LogChannel: logChan}, nil
return &tui.LogResult{Logs: &client.Logs200Response{}, LogChannel: logChan}, nil
}

logs, err := logRepo.ListLogs(ctx, params)
if err != nil {
return nil, fmt.Errorf("error listing logs: %v", err)
}
return &LogResult{Logs: logs, LogChannel: nil}, nil
return &tui.LogResult{Logs: logs, LogChannel: nil}, nil
}

func logForm(ctx context.Context, in LogInput) *tui.FilterModel {
Expand All @@ -149,16 +144,8 @@ func logForm(ctx context.Context, in LogInput) *tui.FilterModel {
})
}

func renderLogs(ctx context.Context, loadData func(LogInput) (*LogResult, error), in LogInput) (tea.Model, error) {
loadLogs := func() (*client.Logs200Response, <-chan *lclient.Log, error) {
result, err := loadData(in)
if err != nil {
return nil, nil, err
}

return result.Logs, result.LogChannel, nil
}
model := tui.NewLogModel(logForm(ctx, in), loadLogs)
func renderLogs(ctx context.Context, loadData func(LogInput) tui.TypedCmd[*tui.LogResult], in LogInput) (tea.Model, error) {
model := tui.NewLogModel(logForm(ctx, in), loadData(in))
return model, nil
}

Expand Down Expand Up @@ -208,13 +195,9 @@ func nonInteractiveLogs(format *command.Output, cmd *cobra.Command, input LogInp
return nil
}

func renderResourcesForLogs(ctx context.Context, loadData func(input ListResourceInput) ([]resource.Resource, error), in ListResourceInput) (tea.Model, error) {
func renderResourcesForLogs(ctx context.Context, loadData func(input ListResourceInput) tui.TypedCmd[[]resource.Resource], in ListResourceInput) (tea.Model, error) {
columns := resourcetui.ColumnsForResources()

loadDataFunc := func() ([]resource.Resource, error) {
return loadData(in)
}

createRowFunc := func(r resource.Resource) btable.Row {
return resourcetui.RowForResource(r)
}
Expand Down Expand Up @@ -244,7 +227,7 @@ func renderResourcesForLogs(ctx context.Context, loadData func(input ListResourc

t := tui.NewTable(
columns,
loadDataFunc,
loadData(in),
createRowFunc,
onSelect,
tui.WithCustomOptions[resource.Resource](customOptions),
Expand Down
10 changes: 3 additions & 7 deletions cmd/palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ const columnDescriptionKey = "Description"

func renderPalette(
ctx context.Context,
loadData func(PaletteCommandInput) ([]PaletteCommand, error),
loadData func(PaletteCommandInput) tui.TypedCmd[[]PaletteCommand],
in PaletteCommandInput,
) (tea.Model, error) {
columns := []btable.Column{
btable.NewColumn(columnCommandKey, "Command", 15).WithFiltered(true),
btable.NewFlexColumn(columnDescriptionKey, "Description", 3),
}

loadDataFunc := func() ([]PaletteCommand, error) {
return loadData(in)
}

createRowFunc := func(cmd PaletteCommand) btable.Row {
return btable.NewRow(map[string]any{
columnCommandKey: cmd.Name,
Expand All @@ -64,7 +60,7 @@ func renderPalette(
return nil
}

commands, err := loadData(in)
commands, err := loadCommandPalette(ctx, in)
if err != nil {
return func() tea.Msg {
return tui.ErrorMsg{Err: err}
Expand All @@ -81,7 +77,7 @@ func renderPalette(

t := tui.NewTable(
columns,
loadDataFunc,
loadData(in),
createRowFunc,
onSelect,
)
Expand Down
8 changes: 2 additions & 6 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ func loadProjects(ctx context.Context, _ ProjectInput) ([]*client.Project, error
return projectRepo.ListProjects(ctx)
}

func renderProjects(ctx context.Context, loadData func(ProjectInput) ([]*client.Project, error), in ProjectInput) (tea.Model, error) {
func renderProjects(ctx context.Context, loadData func(ProjectInput) tui.TypedCmd[[]*client.Project], in ProjectInput) (tea.Model, error) {
columns := []btable.Column{
btable.NewColumn("ID", "ID", 25).WithFiltered(true),
btable.NewFlexColumn("Name", "Name", 40).WithFiltered(true),
}

loadDataFunc := func() ([]*client.Project, error) {
return loadData(in)
}

createRowFunc := func(p *client.Project) btable.Row {
return btable.NewRow(btable.RowData{
"ID": p.Id,
Expand Down Expand Up @@ -78,7 +74,7 @@ func renderProjects(ctx context.Context, loadData func(ProjectInput) ([]*client.

t := tui.NewTable(
columns,
loadDataFunc,
loadData(in),
createRowFunc,
onSelect,
tui.WithCustomOptions[*client.Project](customOptions),
Expand Down
Loading

0 comments on commit 5bd0447

Please sign in to comment.