github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/postgres/local_command.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package postgres 21 22 import ( 23 "bytes" 24 "os/exec" 25 26 "github.com/pkg/errors" 27 ) 28 29 type execCommand struct { 30 *exec.Cmd 31 Stdout *bytes.Buffer 32 Stderr *bytes.Buffer 33 } 34 35 func NewExecCommander(name string, args ...string) LocalCommand { 36 execCmd := exec.Command(name, args...) 37 38 var stdout, stderr bytes.Buffer 39 execCmd.Stdout = &stdout 40 execCmd.Stderr = &stderr 41 return &execCommand{ 42 Cmd: execCmd, 43 Stdout: &stdout, 44 Stderr: &stderr, 45 } 46 } 47 48 func (cmd *execCommand) GetStdout() *bytes.Buffer { 49 return cmd.Stdout 50 } 51 52 func (cmd *execCommand) GetStderr() *bytes.Buffer { 53 return cmd.Stderr 54 } 55 56 var LocalCommander = NewExecCommander 57 58 func ExecCommand(name string, args ...string) (string, error) { 59 cmd := LocalCommander(name, args...) 60 61 err := cmd.Run() 62 if err != nil || cmd.GetStderr().String() != "<nil>" { 63 return "", errors.Errorf("exec command %s failed, err:%v, stderr:%s", name, err, cmd.GetStderr().String()) 64 } 65 66 return cmd.GetStdout().String(), nil 67 } 68 69 func Psql(args ...string) (string, error) { 70 return ExecCommand("psql", args...) 71 } 72 73 func PgCtl(arg string) (string, error) { 74 args := []string{"-c"} 75 args = append(args, "pg_ctl "+arg) 76 args = append(args, "postgres") 77 78 return ExecCommand("su", args...) 79 } 80 81 func PgWalDump(args ...string) (string, error) { 82 return ExecCommand("pg_waldump", args...) 83 } 84 85 func PgRewind(args ...string) (string, error) { 86 return ExecCommand("pg_rewind", args...) 87 } 88 89 type FakeCommand struct { 90 Stdout *bytes.Buffer 91 Stderr *bytes.Buffer 92 RunnerFunc func() error 93 } 94 95 func NewFakeCommander(f func() error, stdout, stderr *bytes.Buffer) func(name string, args ...string) LocalCommand { 96 return func(name string, args ...string) LocalCommand { 97 return &FakeCommand{ 98 RunnerFunc: f, 99 Stdout: stdout, 100 Stderr: stderr, 101 } 102 } 103 } 104 105 func (cmd *FakeCommand) Run() error { 106 return cmd.RunnerFunc() 107 } 108 109 func (cmd *FakeCommand) GetStdout() *bytes.Buffer { 110 return cmd.Stdout 111 } 112 113 func (cmd *FakeCommand) GetStderr() *bytes.Buffer { 114 return cmd.Stderr 115 }