NewCmd creates the "psql" command
()
| 29 | |
| 30 | // NewCmd creates the "psql" command |
| 31 | func NewCmd() *cobra.Command { |
| 32 | var replica bool |
| 33 | var allocateTTY bool |
| 34 | var passStdin bool |
| 35 | |
| 36 | cmd := &cobra.Command{ |
| 37 | Use: "psql CLUSTER [-- PSQL_ARGS...]", |
| 38 | Short: "Start a psql session targeting a CloudNativePG cluster", |
| 39 | Args: validatePsqlArgs, |
| 40 | ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 41 | return plugin.CompleteClusters(cmd.Context(), args, toComplete), cobra.ShellCompDirectiveNoFileComp |
| 42 | }, |
| 43 | Long: "This command will start an interactive psql session inside a PostgreSQL Pod created by CloudNativePG.", |
| 44 | GroupID: plugin.GroupIDMiscellaneous, |
| 45 | RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | clusterName := args[0] |
| 47 | psqlArgs := args[1:] |
| 48 | psqlOptions := CommandOptions{ |
| 49 | Replica: replica, |
| 50 | Namespace: plugin.Namespace, |
| 51 | Context: plugin.KubeContext, |
| 52 | AllocateTTY: allocateTTY, |
| 53 | PassStdin: passStdin, |
| 54 | Args: psqlArgs, |
| 55 | Name: clusterName, |
| 56 | } |
| 57 | |
| 58 | psqlCommand, err := NewCommand(cmd.Context(), psqlOptions) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | return psqlCommand.Exec() |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | cmd.Flags().BoolVar( |
| 68 | &replica, |
| 69 | "replica", |
| 70 | false, |
| 71 | "Connects to the first replica on the pod list (by default connects to the primary)", |
| 72 | ) |
| 73 | |
| 74 | cmd.Flags().BoolVarP( |
| 75 | &allocateTTY, |
| 76 | "tty", |
| 77 | "t", |
| 78 | true, |
| 79 | "Whether to allocate a TTY for the psql process", |
| 80 | ) |
| 81 | |
| 82 | cmd.Flags().BoolVarP( |
| 83 | &passStdin, |
| 84 | "stdin", |
| 85 | "i", |
| 86 | true, |
| 87 | "Whether to pass stdin to the container", |
| 88 | ) |
no test coverage detected