Skip to content

Commit

Permalink
Let users pass arbitrary flags to psql ssh etc (#159)
Browse files Browse the repository at this point in the history
* Allow passing along args

* Add to docs

* Use `ArgsLenAtDash` func

* Exit exec model after closing

* Fix panic
  • Loading branch information
mdbenjam authored Dec 5, 2024
1 parent 2dcde2f commit 2ac8ed6
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 19 deletions.
16 changes: 12 additions & 4 deletions cmd/pgcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

// pgcliCmd represents the pgcli command
var pgcliCmd = &cobra.Command{
Use: "pgcli [postgresID]",
Args: cobra.MaximumNArgs(1),
Short: "Open a pgcli session to a PostgreSQL database",
Long: `Open a pgcli session to a PostgreSQL database. Optionally pass the database id as an argument.`,
Use: "pgcli [postgresID]",
Short: "Open a pgcli session to a PostgreSQL database",
Long: `Open a pgcli session to a PostgreSQL database. Optionally pass the database id as an argument.
To pass arguments to pgcli, use the following syntax: render pgcli [postgresID] -- [pgcli args]`,
GroupID: GroupSession.ID,
}

Expand Down Expand Up @@ -57,6 +57,14 @@ func init() {
return err
}

if cmd.ArgsLenAtDash() == 0 {
input.PostgresID = ""
}

if cmd.ArgsLenAtDash() >= 0 {
input.Args = args[cmd.ArgsLenAtDash():]
}

InteractivePGCLIView(ctx, &input)
return nil
}
Expand Down
16 changes: 12 additions & 4 deletions cmd/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

// psqlCmd represents the psql command
var psqlCmd = &cobra.Command{
Use: "psql [postgresID]",
Args: cobra.MaximumNArgs(1),
Short: "Open a psql session to a PostgreSQL database",
Long: `Open a psql session to a PostgreSQL database. Optionally pass the database id as an argument.`,
Use: "psql [postgresID]",
Short: "Open a psql session to a PostgreSQL database",
Long: `Open a psql session to a PostgreSQL database. Optionally pass the database id as an argument.
To pass arguments to psql, use the following syntax: render psql [postgresID] -- [psql args]`,
GroupID: GroupSession.ID,
}

Expand Down Expand Up @@ -57,6 +57,14 @@ func init() {
return err
}

if cmd.ArgsLenAtDash() == 0 {
input.PostgresID = ""
}

if cmd.ArgsLenAtDash() >= 0 {
input.Args = args[cmd.ArgsLenAtDash():]
}

InteractivePSQLView(ctx, &input)
return nil
}
Expand Down
16 changes: 12 additions & 4 deletions cmd/rediscli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

// redisCLICmd represents the redisCLI command
var redisCLICmd = &cobra.Command{
Use: "redis-cli [redisID]",
Args: cobra.MaximumNArgs(1),
Short: "Open a redis-cli session to a Redis instance",
Long: `Open a redis-cli session to a Redis instance. Optionally pass the redis id as an argument.`,
Use: "redis-cli [redisID]",
Short: "Open a redis-cli session to a Redis instance",
Long: `Open a redis-cli session to a Redis instance. Optionally pass the redis id as an argument.
To pass arguments to redis-cli, use the following syntax: render redis-cli [redisID] -- [redis-cli args]`,
GroupID: GroupSession.ID,
}

Expand Down Expand Up @@ -56,6 +56,14 @@ func init() {
return err
}

if cmd.ArgsLenAtDash() == 0 {
input.RedisID = ""
}

if cmd.ArgsLenAtDash() >= 0 {
input.Args = args[cmd.ArgsLenAtDash():]
}

InteractiveRedisView(ctx, &input)
return nil
}
Expand Down
16 changes: 12 additions & 4 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (

// sshCmd represents the ssh command
var sshCmd = &cobra.Command{
Use: "ssh [serviceID]",
Args: cobra.MaximumNArgs(1),
Short: "SSH into a service instance",
Long: `SSH into a service instance. Optionally pass the service id as an argument.`,
Use: "ssh [serviceID]",
Short: "SSH into a service instance",
Long: `SSH into a service instance. Optionally pass the service id as an argument.
To pass arguments to ssh, use the following syntax: render ssh [serviceID] -- [ssh args]`,
GroupID: GroupSession.ID,
}

Expand Down Expand Up @@ -59,6 +59,14 @@ func init() {
return err
}

if cmd.ArgsLenAtDash() == 0 {
input.ServiceID = ""
}

if cmd.ArgsLenAtDash() >= 0 {
input.Args = args[cmd.ArgsLenAtDash():]
}

InteractiveSSHView(ctx, &input, "SSH")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tui/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *ExecModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

return nil
return ExecDone{}
})
case ExecDone:
return m, func() tea.Msg {
Expand Down
9 changes: 8 additions & 1 deletion pkg/tui/views/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type PSQLInput struct {
Project *client.Project
EnvironmentIDs []string
Tool PSQLTool

Args []string
}

type PSQLView struct {
Expand Down Expand Up @@ -104,7 +106,12 @@ func loadDataPSQL(ctx context.Context, in *PSQLInput) (*exec.Cmd, error) {
return nil, err
}

return exec.Command(string(in.Tool), connectionInfo.ExternalConnectionString), nil
args := []string{connectionInfo.ExternalConnectionString}
for _, arg := range in.Args {
args = append(args, arg)
}

return exec.Command(string(in.Tool), args...), nil
}

func hasAccessToPostgres(pg *client.PostgresDetail, userIP net.IP) (bool, error) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/tui/views/rediscli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type RedisCLIInput struct {
RedisID string `cli:"arg:0"`
Project *client.Project
EnvironmentIDs []string

Args []string
}

type RedisCLIView struct {
Expand Down Expand Up @@ -94,6 +96,10 @@ func loadDataRedisCLI(ctx context.Context, in *RedisCLIInput) (*exec.Cmd, error)
}
}

for _, arg := range in.Args {
cmdArgs = append(cmdArgs, arg)
}

cmd := exec.Command("redis-cli", cmdArgs...)
cmd.Env = env
return cmd, nil
Expand Down
9 changes: 8 additions & 1 deletion pkg/tui/views/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type SSHInput struct {
ServiceID string `cli:"arg:0"`
Project *client.Project
EnvironmentIDs []string

Args []string
}

type SSHView struct {
Expand Down Expand Up @@ -123,7 +125,12 @@ func loadDataSSH(ctx context.Context, in *SSHInput) (*exec.Cmd, error) {
return nil, fmt.Errorf("service does not support ssh")
}

return exec.Command("ssh", *sshAddress), nil
args := []string{*sshAddress}
for _, arg := range in.Args {
args = append(args, arg)
}

return exec.Command("ssh", args...), nil
}

func (v *SSHView) Init() tea.Cmd {
Expand Down

0 comments on commit 2ac8ed6

Please sign in to comment.