From 6658b705b408a5cfcda8d51c9439c3aa526d69ac Mon Sep 17 00:00:00 2001 From: Sadi Hakan Date: Tue, 23 Aug 2022 15:34:49 +0300 Subject: [PATCH] Add: Predefined paths --- internal/command.go | 40 ++++++++++++++++++++++++-------- internal/detect_binaries.go | 46 ++++++++++++++++++++++++++++++++++++- internal/postgres.go | 1 + internal/postgres_test.go | 7 +++++- 4 files changed, 83 insertions(+), 11 deletions(-) diff --git a/internal/command.go b/internal/command.go index 1690bb8..9960801 100644 --- a/internal/command.go +++ b/internal/command.go @@ -36,42 +36,64 @@ const ( // CreateCheckBinaryCommand ... func CreateCheckBinaryCommand(ctx context.Context, sourceType config.SourceType) *exec.Cmd { - return homeDirCommand(exec.CommandContext(ctx, util.Which(), getCheckCommand(sourceType)...)) + return homeDirCommand(ctx, util.Which(), getCheckCommand(sourceType)) } // CreateCheckBinaryPathCommand ... func CreateCheckBinaryPathCommand(ctx context.Context, cfg config.Config) *exec.Cmd { - return homeDirCommand(exec.CommandContext(ctx, util.Which(), getCheckBinaryPathCommand(cfg)...)) + return homeDirCommand(ctx, "", getCheckBinaryPathCommand(cfg)) } // CreateImportBinaryCommand ... func CreateImportBinaryCommand(ctx context.Context, sourceType config.SourceType) *exec.Cmd { - return homeDirCommand(exec.CommandContext(ctx, util.Which(), getImportCommand(sourceType)...)) + return homeDirCommand(ctx, util.Which(), getImportCommand(sourceType)) } // CreateVersionCommand ... func CreateVersionCommand(ctx context.Context, binaryPath string, sourceType config.SourceType) *exec.Cmd { - return homeDirCommand(exec.CommandContext(ctx, binaryPath, getVersionCommandArg(sourceType)...)) + return homeDirCommand(ctx, binaryPath, getVersionCommandArg(sourceType)) } // CreateExportBinaryCommand ... func CreateExportBinaryCommand(ctx context.Context, sourceType config.SourceType) *exec.Cmd { - return homeDirCommand(exec.CommandContext(ctx, util.Which(), getExportCommand(sourceType)...)) + return homeDirCommand(ctx, util.Which(), getExportCommand(sourceType)) } // CreateExportCommand ... func CreateExportCommand(ctx context.Context, cfg config.Config) *exec.Cmd { - arg := getExportCommandArg(cfg) - return homeDirCommand(exec.CommandContext(ctx, cfg.BinaryPath, arg...)) + return homeDirCommand(ctx, cfg.BinaryPath, getExportCommandArg(cfg)) } // CreateImportCommand ... func CreateImportCommand(ctx context.Context, cfg config.Config) *exec.Cmd { - return homeDirCommand(exec.CommandContext(ctx, cfg.BinaryPath, getImportCommandArg(cfg)...)) + return homeDirCommand(ctx, cfg.BinaryPath, getImportCommandArg(cfg)) } -func homeDirCommand(cmd *exec.Cmd) *exec.Cmd { +func homeDirCommand(ctx context.Context, command string, arg []string) *exec.Cmd { + params := make([]string, 0) + cmd := new(exec.Cmd) + switch runtime.GOOS { + case "darwin": + if command != "" { + params = append(params, command) + } + params = append(params, arg...) + command = strings.Join(params, " ") + fmt.Println(command) + cmd = exec.CommandContext(ctx, "sh", "-c", command) + case "linux": + cmd = exec.CommandContext(ctx, command, arg...) + case "windows": + if command != "" { + params = append(params, command) + } + params = append(params, arg...) + command = strings.Join(params, " ") + cmd = exec.CommandContext(ctx, "powershell.exe", "/C", command) + } + cmd.Dir = util.HomeDir() + return cmd } diff --git a/internal/detect_binaries.go b/internal/detect_binaries.go index 94d2aaf..83bfee5 100644 --- a/internal/detect_binaries.go +++ b/internal/detect_binaries.go @@ -3,12 +3,22 @@ package internal import ( "bytes" "context" + "errors" + "fmt" "github.com/sadihakan/dummy-dump/config" "os" "os/exec" "strings" ) +var predefinedPostgresPaths = []string{ + "/Applications/Postgres.app/Contents/Versions/latest/bin/pg_dump", +} + +var predefinedMySQLPaths = []string{ + "/Applications/Postgres.app/Contents/Versions/latest/bin/pg_dump", +} + // CheckBinary ... func CheckBinary(ctx context.Context, binaryPath string, sourceType config.SourceType, importArg bool, exportArg bool) (string, error) { var err error @@ -98,9 +108,43 @@ func checkExport(ctx context.Context, sourceType config.SourceType) (string, err err := cmd.Run() if err != nil { - return "", err + switch sourceType { + case config.PostgreSQL: + var path string + for i := 0; i < len(predefinedPostgresPaths); i++ { + path, err = findExport(ctx, predefinedPostgresPaths[i]) + if err == nil { + break + } + } + return path, nil + case config.MySQL: + var path string + for i := 0; i < len(predefinedMySQLPaths); i++ { + path, err = findExport(ctx, predefinedMySQLPaths[i]) + if err == nil { + break + } + } + return path, nil + } } lines := strings.Split(out.String(), "\n") return strings.TrimSpace(lines[0]), nil } + +func findExport(ctx context.Context, path string) (string, error) { + cmd := CreateCheckBinaryPathCommand(ctx, config.Config{BinaryPath: path}) + + var out bytes.Buffer + var stderr bytes.Buffer + + cmd.Stdout = &out + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return "", errors.New(fmt.Sprint(err) + ": " + stderr.String()) + } + + return path, nil +} diff --git a/internal/postgres.go b/internal/postgres.go index 3370f62..b657592 100644 --- a/internal/postgres.go +++ b/internal/postgres.go @@ -50,6 +50,7 @@ func (p Postgres) Export(ctx context.Context, dump config.Config) error { cmd.Stdout = &out cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { return errors.New(fmt.Sprint(err) + ": " + stderr.String()) } diff --git a/internal/postgres_test.go b/internal/postgres_test.go index e6d293a..9a986fb 100644 --- a/internal/postgres_test.go +++ b/internal/postgres_test.go @@ -1,6 +1,7 @@ package internal import ( + "context" "github.com/sadihakan/dummy-dump/config" "github.com/sadihakan/dummy-dump/util" "path/filepath" @@ -18,7 +19,11 @@ func TestCheckWithError(t *testing.T) { var dump Dump dump = Postgres{} - err := dump.Check(nil) + cnf := config.Config{ + BinaryPath: "randomstring", + } + + err := dump.CheckPath(context.Background(), cnf) if err != nil { t.Fatal(err)