Skip to content

Commit

Permalink
♻️ Pass command into sh by default
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 25, 2022
1 parent 763c3b6 commit bc777ee
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
6 changes: 3 additions & 3 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
19 changes: 9 additions & 10 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
9 changes: 4 additions & 5 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/config/flags/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 7 additions & 10 deletions internal/database/grammar/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bc777ee

Please sign in to comment.