Skip to content

Commit

Permalink
add shortcut to copy id (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitemongerer authored Dec 6, 2024
1 parent 9a4dc61 commit 1c60cf0
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var InteractiveEnvironment = func(ctx context.Context, input views.EnvironmentIn
}, e.Name)
},
tui.WithCustomOptions[*client.Environment]([]tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
}),
))
Expand Down
1 change: 1 addition & 0 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func InteractiveLogs(ctx context.Context, input views.LogInput, breadcrumb strin

func getLogsOptions(ctx context.Context, breadcrumb string) []tui.CustomOption {
return []tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
WithProjectFilter(ctx, servicesCmd, "Project Filter", &views.LogInput{}, func(ctx context.Context, project *client.Project) tea.Cmd {
logInput := views.LogInput{}
Expand Down
28 changes: 28 additions & 0 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"context"
"fmt"

"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
btable "github.com/evertras/bubble-table/table"
"github.com/spf13/cobra"
Expand All @@ -23,6 +25,32 @@ func WithWorkspaceSelection(ctx context.Context) tui.CustomOption {
}
}

func WithCopyID(ctx context.Context, cmd *cobra.Command) tui.CustomOption {
return tui.CustomOption{
Key: "c",
Title: "Copy ID",
Function: func(row btable.Row) tea.Cmd {
return func() tea.Msg {
id, ok := row.Data["ID"]
if !ok {
return nil
}

idstr, ok := id.(string)
if !ok {
return nil
}

err := clipboard.WriteAll(idstr)
if err != nil {
return command.AddErrToStack(ctx, cmd, fmt.Errorf("could not copy to clipboard: %w", err))
}
return nil
}
},
}
}

type ProjectHandler func(ctx context.Context, project *client.Project) tea.Cmd

func WithProjectFilter(ctx context.Context, cmd *cobra.Command, breadcrumb string, in any, h ProjectHandler) tui.CustomOption {
Expand Down
1 change: 1 addition & 0 deletions cmd/pgcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func InteractivePGCLIView(ctx context.Context, input *views.PSQLInput) tea.Cmd {

func getPGCLITableOptions(ctx context.Context, input *views.PSQLInput) []tui.CustomOption {
return []tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
WithProjectFilter(ctx, pgcliCmd, "pgcli", input, func(ctx context.Context, project *client.Project) tea.Cmd {
if project != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var InteractiveProjectList = func(ctx context.Context) {
}, p.Name)
},
tui.WithCustomOptions[*client.Project]([]tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
}),
))
Expand Down
1 change: 1 addition & 0 deletions cmd/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func InteractivePSQLView(ctx context.Context, input *views.PSQLInput) tea.Cmd {

func getPsqlTableOptions(ctx context.Context, input *views.PSQLInput) []tui.CustomOption {
return []tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
WithProjectFilter(ctx, psqlCmd, "psql", input, func(ctx context.Context, project *client.Project) tea.Cmd {
if project != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/rediscli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func InteractiveRedisView(ctx context.Context, input *views.RedisCLIInput) tea.C

func getRedisTableOptions(ctx context.Context) []tui.CustomOption {
return []tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
WithProjectFilter(ctx, redisCLICmd, "redisCLI", &views.RedisCLIInput{}, func(ctx context.Context, project *client.Project) tea.Cmd {
input := &views.RedisCLIInput{}
Expand Down
1 change: 1 addition & 0 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func InteractiveServices(ctx context.Context, in views.ListResourceInput, breadc

func getServiceTableOptions(ctx context.Context) []tui.CustomOption {
return []tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
WithProjectFilter(ctx, servicesCmd, "Project Filter", &views.ListResourceInput{}, func(ctx context.Context, project *client.Project) tea.Cmd {
listResourceInput := views.ListResourceInput{}
Expand Down
1 change: 1 addition & 0 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func InteractiveSSHView(ctx context.Context, input *views.SSHInput, breadcrumb s

func getSSHTableOptions(ctx context.Context, breadcrumb string) []tui.CustomOption {
return []tui.CustomOption{
WithCopyID(ctx, servicesCmd),
WithWorkspaceSelection(ctx),
WithProjectFilter(ctx, servicesCmd, "Project Filter", &views.SSHInput{}, func(ctx context.Context, project *client.Project) tea.Cmd {
input := views.SSHInput{}
Expand Down
6 changes: 5 additions & 1 deletion pkg/tui/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (m *StackModel) Push(model ModelWithCmd) tea.Cmd {
return tea.Sequence(model.Model.Init(), tea.WindowSize())
}

func (m *StackModel) PushError(err error) tea.Cmd {
return m.Push(ModelWithCmd{Model: NewErrorModel(err)})
}

func (m *StackModel) Pop() *ModelWithCmd {
if len(m.stack) > 0 {
popped := m.stack[len(m.stack)-1]
Expand Down Expand Up @@ -138,7 +142,7 @@ func (m *StackModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if len(m.stack) > 0 {
err := clipboard.WriteAll(m.stack[len(m.stack)-1].Cmd)
if err != nil {
m.Push(ModelWithCmd{Model: NewErrorModel(fmt.Errorf("Failed to copy command to clipboard"))})
m.PushError(fmt.Errorf("Failed to copy command to clipboard"))
}
}
}
Expand Down

0 comments on commit 1c60cf0

Please sign in to comment.