@@ -2,13 +2,15 @@ package cmd
22
33import (
44 "context"
5+ "fmt"
56
67 tea "github.com/charmbracelet/bubbletea"
78 "github.com/spf13/cobra"
89
910 "github.com/render-oss/cli/pkg/client"
1011 "github.com/render-oss/cli/pkg/command"
1112 "github.com/render-oss/cli/pkg/postgres"
13+ "github.com/render-oss/cli/pkg/text"
1214 "github.com/render-oss/cli/pkg/tui"
1315 "github.com/render-oss/cli/pkg/tui/flows"
1416 "github.com/render-oss/cli/pkg/tui/views"
@@ -19,7 +21,13 @@ var psqlCmd = &cobra.Command{
1921 Use : "psql [postgresID|postgresName]" ,
2022 Short : "Open a psql session to a PostgreSQL database" ,
2123 Long : `Open a psql session to a PostgreSQL database. Optionally pass the database id or name as an argument.
22- To pass arguments to psql, use the following syntax: render psql [postgresID|postgresName] -- [psql args]` ,
24+ To pass arguments to psql, use the following syntax: render psql [postgresID|postgresName] -- [psql args]
25+
26+ For non-interactive usage, use the --command flag:
27+ render psql [postgresID|postgresName] -c "SELECT * FROM users;" -o text
28+
29+ Additional psql flags can be passed after --:
30+ render psql [postgresID|postgresName] -c "SELECT 1;" -o json -- --csv -q` ,
2331 GroupID : GroupSession .ID ,
2432}
2533
@@ -51,10 +59,12 @@ func getPsqlTableOptions(ctx context.Context, input *views.PSQLInput) []tui.Cust
5159func init () {
5260 rootCmd .AddCommand (psqlCmd )
5361
62+ psqlCmd .Flags ().StringP ("command" , "c" , "" , "SQL command to execute (enables non-interactive mode)" )
63+
5464 psqlCmd .RunE = func (cmd * cobra.Command , args []string ) error {
5565 ctx := cmd .Context ()
5666 var input views.PSQLInput
57- err := command .ParseCommandInteractiveOnly (cmd , args , & input )
67+ err := command .ParseCommand (cmd , args , & input )
5868 if err != nil {
5969 return err
6070 }
@@ -67,6 +77,27 @@ func init() {
6777 input .Args = args [cmd .ArgsLenAtDash ():]
6878 }
6979
80+ input .Tool = views .PSQL
81+
82+ outputFormat := command .GetFormatFromContext (ctx )
83+ if outputFormat != nil && ! outputFormat .Interactive () {
84+ if input .Command == "" {
85+ return fmt .Errorf ("--command flag is required in non-interactive mode\n Usage: render psql <postgresID> --command \" SELECT ...\" -o json" )
86+ }
87+
88+ if input .PostgresIDOrName == "" {
89+ return fmt .Errorf ("postgres ID or name is required in non-interactive mode" )
90+ }
91+
92+ result , err := views .ExecutePSQLNonInteractive (ctx , & input )
93+ if err != nil {
94+ return err
95+ }
96+
97+ _ , err = command .PrintData (cmd , result , text .PSQLResultText )
98+ return err
99+ }
100+
70101 InteractivePSQLView (ctx , & input )
71102 return nil
72103 }
0 commit comments