Skip to content

Commit

Permalink
Merge pull request #37 from sadihakan/development
Browse files Browse the repository at this point in the history
Add: Predefined paths
  • Loading branch information
sadihakan authored Aug 23, 2022
2 parents 30515f7 + 6658b70 commit f586f35
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
40 changes: 31 additions & 9 deletions internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
46 changes: 45 additions & 1 deletion internal/detect_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
7 changes: 6 additions & 1 deletion internal/postgres_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"github.com/sadihakan/dummy-dump/config"
"github.com/sadihakan/dummy-dump/util"
"path/filepath"
Expand All @@ -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)
Expand Down

0 comments on commit f586f35

Please sign in to comment.