Skip to content

Commit

Permalink
Merge pull request #15 from sadihakan/development
Browse files Browse the repository at this point in the history
Fix: Command check
  • Loading branch information
sadihakan authored Mar 30, 2021
2 parents 8ae6169 + 9d7c9d6 commit 26de929
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
21 changes: 12 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import "github.com/sadihakan/dummy-dump/errors"
import (
"github.com/sadihakan/dummy-dump/errors"
)

type Config struct {
Source SourceType
Expand All @@ -13,7 +15,7 @@ type Config struct {
BinaryPath string
}

func (c Config) checkAll() error {
func (c *Config) checkAll() error {
if c.Source == "" {
return errors.New(errors.ConfigSourceNil)
}
Expand All @@ -22,7 +24,7 @@ func (c Config) checkAll() error {
return errors.New(errors.ConfigUserNil)
}

if c.Import==true && c.Path == "" {
if c.Import == true && c.Path == "" {
return errors.New(errors.ConfigPathNotExist)
}

Expand All @@ -33,13 +35,14 @@ func (c Config) checkAll() error {
if c.Import == c.Export {
return errors.New(errors.ConfigMethodError)
}

if c.Export && c.Path == "" {
c.Path = "."
}
return nil
}

func (c Config) CheckConfigPostgreSQL() error {
if c.Import {
c.DB = "postgres"
}
func (c *Config) CheckConfigPostgreSQL() error {

if err := c.checkAll(); err != nil {
return err
Expand All @@ -52,7 +55,7 @@ func (c Config) CheckConfigPostgreSQL() error {
return nil
}

func (c Config) CheckConfigMySQL() error {
func (c *Config) CheckConfigMySQL() error {
if err := c.checkAll(); err != nil {
return err
}
Expand All @@ -64,7 +67,7 @@ func (c Config) CheckConfigMySQL() error {
return nil
}

func (c Config) CheckConfigMsSQL() error {
func (c *Config) CheckConfigMsSQL() error {
if err := c.checkAll(); err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

const (
//pgDatabase = "--dbname=postgres"
pgFlagDatabase = "-d"
pgFlagFileName = "-f"
pgFlagCreate = "--create"
pgFlatFormat = "--format=c"
pgFlagDatabase = "-d"
pgFlagFileName = "-f"
pgFlagCreateDatabase = "-C"
pgFlagCreate = "--create"
pgFlatFormat = "--format=c"

//pgRestore="pg_restore"
//pgDump="pg_dump"
Expand Down Expand Up @@ -43,8 +44,8 @@ func CreateExportBinaryCommand(sourceType config.SourceType) *exec.Cmd {
}

// CreateExportCommand ...
func CreateExportCommand(binaryPath string, sourceType config.SourceType, user string, database string) *exec.Cmd {
return exec.Command(binaryPath, getExportCommandArg(sourceType, user, database)...)
func CreateExportCommand(binaryPath string, sourceType config.SourceType, user string, database string, path string) *exec.Cmd {
return exec.Command(binaryPath, getExportCommandArg(sourceType, user, database, path)...)
}

// CreateImportCommand ...
Expand All @@ -56,7 +57,7 @@ func CreateImportCommand(binaryPath string, sourceType config.SourceType, user s
func getImportCommandArg(sourceType config.SourceType, user string, database, path string) (arg []string) {
switch sourceType {
case config.PostgreSQL:
arg = []string{user, pgFlagDatabase, database, pgFlagCreate, path}
arg = []string{user, pgFlagCreateDatabase, pgFlagDatabase, database, pgFlagCreate, path}
case config.MySQL:
arg = []string{mysqlFlagUser, user, mysqlFlagPassword, database, mysqlFlagExecute, "source " + path}

Expand All @@ -65,15 +66,14 @@ func getImportCommandArg(sourceType config.SourceType, user string, database, pa
}

// getExportCommandArg ...
func getExportCommandArg(sourceType config.SourceType, user string, database string) (arg []string) {
func getExportCommandArg(sourceType config.SourceType, user string, database string, path string) (arg []string) {
today := time.Now().UTC().UnixNano()
filename := fmt.Sprintf("%d.backup", today)
filename := fmt.Sprintf("%s/%d.backup", path, today)
switch sourceType {
case config.PostgreSQL:
arg = []string{user, database, pgFlagFileName, filename, pgFlagCreate, pgFlatFormat}
case config.MySQL:
arg = []string{mysqlFlagUser, user, mysqlFlagPassword, database}

}
return arg
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (m MySQL) Check() error {

func (m MySQL) Export(dump config.Config) error {
filename := fmt.Sprintf("%d.backup", time.Now().UTC().UnixNano())
cmd := CreateExportCommand(dump.BinaryPath, config.MySQL, dump.User, dump.DB)
cmd := CreateExportCommand(dump.BinaryPath, config.MySQL, dump.User, dump.DB, dump.Path)
var outb bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
Expand Down
13 changes: 8 additions & 5 deletions internal/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"bytes"
"errors"
"fmt"
"github.com/sadihakan/dummy-dump/config"
"os"
Expand All @@ -19,7 +20,7 @@ func (p Postgres) Check() error {
err := cmd.Run()
if err != nil {
_, _ = os.Stderr.WriteString(err.Error())
return err
return errors.New(errBuf.String())
}
return nil
}
Expand All @@ -30,13 +31,15 @@ func (p Postgres) Export(dump config.Config) error {
user := fmt.Sprintf("--username=%s", dump.User)
database := fmt.Sprintf("--dbname=%s", dump.DB)

cmd := CreateExportCommand(dump.BinaryPath, config.PostgreSQL, user, database)
fmt.Println(dump.Path)
cmd := CreateExportCommand(dump.BinaryPath, config.PostgreSQL, user, database, dump.Path)
fmt.Println(cmd)
cmd.Stdout = &out
cmd.Stderr = &errBuf
err := cmd.Run()

if err != nil {
return err
return errors.New(errBuf.String())
}

return nil
Expand All @@ -46,14 +49,14 @@ func (p Postgres) Import(dump config.Config) error {
var out, errBuf bytes.Buffer

user := fmt.Sprintf("--username=%s", dump.User)

cmd := CreateImportCommand(dump.BinaryPath, config.PostgreSQL, user, dump.DB, dump.Path)
fmt.Println(cmd)
cmd.Stdout = &out
cmd.Stderr = &errBuf
err := cmd.Run()

if err != nil {
return err
return errors.New(errBuf.String())
}

return nil
Expand Down

0 comments on commit 26de929

Please sign in to comment.