diff --git a/cmd/dump/dump.go b/cmd/dump/dump.go index 2ffe8cc8..4d82499b 100644 --- a/cmd/dump/dump.go +++ b/cmd/dump/dump.go @@ -169,7 +169,7 @@ func run(cmd *cobra.Command, args []string) (err error) { err = t.Safe(func() error { return conf.Client.Exec( conf.Pod, - buildCommand(conf.Grammar, conf), + buildCommand(conf.Grammar, conf).String(), t.In, t.Out, os.Stderr, @@ -208,12 +208,12 @@ func run(cmd *cobra.Command, args []string) (err error) { return nil } -func buildCommand(db config.Databaser, conf config.Dump) []string { +func buildCommand(db config.Databaser, conf config.Dump) *command.Builder { cmd := db.DumpCommand(conf) if conf.OutputFormat != sqlformat.Custom { cmd.Push(command.Raw("|"), "gzip", "--force") } // base64 is required since TTYs use CRLF cmd.Push(command.Raw("|"), "base64", "--wrap=0") - return []string{"sh", "-c", cmd.String()} + return cmd } diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index fde6021c..6a34e34e 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -42,7 +42,7 @@ func run(cmd *cobra.Command, args []string) (err error) { return t.Safe(func() error { return conf.Client.Exec( conf.Pod, - buildCommand(conf.Grammar, conf, args), + buildCommand(conf.Grammar, conf, args).String(), t.In, t.Out, os.Stderr, @@ -52,15 +52,14 @@ func run(cmd *cobra.Command, args []string) (err error) { }) } -func buildCommand(db config.Databaser, conf config.Exec, args []string) []string { - var cmd *command.Builder +func buildCommand(db config.Databaser, conf config.Exec, args []string) *command.Builder { if len(args) == 0 { - cmd = db.ExecCommand(conf) - } else { - cmd = command.NewBuilder("exec") - for _, arg := range args { - cmd.Push(arg) - } + return db.ExecCommand(conf) } - return []string{"sh", "-c", cmd.String()} + + cmd := command.NewBuilder("exec") + for _, arg := range args { + cmd.Push(arg) + } + return cmd } diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index e5a0132d..44385495 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -170,10 +170,9 @@ func run(cmd *cobra.Command, args []string) (err error) { return nil } -func buildCommand(conf config.Restore, inputFormat sqlformat.Format) []string { - cmd := conf.Grammar.RestoreCommand(conf, inputFormat) - cmd.Unshift("gunzip", "--force", command.Raw("|")) - return []string{"sh", "-c", cmd.String()} +func buildCommand(conf config.Restore, inputFormat sqlformat.Format) *command.Builder { + return conf.Grammar.RestoreCommand(conf, inputFormat). + Unshift("gunzip", "--force", command.Raw("|")) } func gzipCopy(w io.Writer, r io.Reader) (err error) { @@ -198,7 +197,7 @@ func runInDatabasePod(r *io.PipeReader, ch chan error, inputFormat sqlformat.For _ = pr.Close() }(r) - err = conf.Client.Exec(conf.Pod, buildCommand(conf, inputFormat), r, os.Stdout, os.Stderr, false, nil) + err = conf.Client.Exec(conf.Pod, buildCommand(conf, inputFormat).String(), r, os.Stdout, os.Stderr, false, nil) if err != nil { _ = r.CloseWithError(err) ch <- err diff --git a/internal/config/flags/database.go b/internal/config/flags/database.go index 58430ca1..c1523a50 100644 --- a/internal/config/flags/database.go +++ b/internal/config/flags/database.go @@ -110,7 +110,7 @@ func queryInDatabase(cmd *cobra.Command, args []string, conf config.Exec, query r := strings.NewReader(query) var buf strings.Builder sqlCmd := conf.Grammar.ExecCommand(conf) - err := conf.Client.Exec(conf.Pod, []string{"sh", "-c", sqlCmd.String()}, r, &buf, os.Stderr, false, nil) + err := conf.Client.Exec(conf.Pod, sqlCmd.String(), r, &buf, os.Stderr, false, nil) if err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/internal/database/grammar/postgres.go b/internal/database/grammar/postgres.go index 70a17929..bd5d008d 100644 --- a/internal/database/grammar/postgres.go +++ b/internal/database/grammar/postgres.go @@ -73,18 +73,15 @@ func (Postgres) FilterPods(client kubernetes.KubeClient, pods []v1.Pod) ([]v1.Po if len(pods) > 0 && pods[0].Labels["app.kubernetes.io/name"] == "postgresql-ha" { // HA chart. Need to detect primary. log.Info("querying for primary instance") - cmd := []string{ - "sh", "-c", - command.NewBuilder( - command.NewEnv("DISABLE_WELCOME_MESSAGE", "true"), - "/opt/bitnami/scripts/postgresql-repmgr/entrypoint.sh", - "repmgr", "--config-file=/opt/bitnami/repmgr/conf/repmgr.conf", - "service", "status", "--csv", - ).String(), - } + cmd := command.NewBuilder( + command.NewEnv("DISABLE_WELCOME_MESSAGE", "true"), + "/opt/bitnami/scripts/postgresql-repmgr/entrypoint.sh", + "repmgr", "--config-file=/opt/bitnami/repmgr/conf/repmgr.conf", + "service", "status", "--csv", + ) var buf strings.Builder - err := client.Exec(pods[0], cmd, strings.NewReader(""), &buf, os.Stderr, false, nil) + err := client.Exec(pods[0], cmd.String(), strings.NewReader(""), &buf, os.Stderr, false, nil) if err != nil { return pods, err } diff --git a/internal/kubernetes/pod.go b/internal/kubernetes/pod.go index 5139d572..2c0ef36d 100644 --- a/internal/kubernetes/pod.go +++ b/internal/kubernetes/pod.go @@ -30,14 +30,14 @@ func (client KubeClient) GetNamespacedPods() (*v1.PodList, error) { return pods, nil } -func (client KubeClient) Exec(pod v1.Pod, command []string, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error { +func (client KubeClient) Exec(pod v1.Pod, cmd string, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error { req := client.ClientSet.CoreV1().RESTClient().Post(). Resource("pods"). Namespace(client.Namespace). Name(pod.Name). SubResource("exec"). VersionedParams(&v1.PodExecOptions{ - Command: command, + Command: []string{"sh", "-c", cmd}, Stdin: true, Stdout: true, Stderr: true,