Skip to content

Commit

Permalink
properly break up funcrtionality into smaller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dallincoons committed May 4, 2020
1 parent 767aecf commit a3a996d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 27 deletions.
88 changes: 64 additions & 24 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"log"
"os"
"os/exec"
"strings"
)

// cloneCmd represents the clone command
Expand All @@ -38,52 +39,68 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
to, _ := cmd.Flags().GetString("to")
name, _ := cmd.Flags().GetString("name")
importDatabase, _ := cmd.Flags().GetString("database")
dump_dir, _ := cmd.Flags().GetString("dump_dir")
dump_name, _ := cmd.Flags().GetString("dump_name")
databaseToCloneName, _ := cmd.Flags().GetString("from")
importDatabaseName, _ := cmd.Flags().GetString("to")

file, ferr := os.OpenFile(fmt.Sprintf("%s/%s.sql", to, name), os.O_RDWR|os.O_CREATE, 0644)
dump_name = strings.TrimRight(dump_name, ".sql")

file, ferr := os.OpenFile(fmt.Sprintf("%s/%s.sql", dump_dir, dump_name), os.O_RDWR|os.O_CREATE, 0644)

if (ferr != nil) {
log.Fatalf("error opening file: %s", ferr)
}

Clone(file, importDatabase, to, name)
Clone(file, databaseToCloneName, importDatabaseName, dump_dir, dump_name)

file.Close()
},
}

func Clone(file *os.File, importDatabase string, to string, name string) {
func Clone(file *os.File, databaseToCloneName string, importDatabaseName string, dump_dir string, dump_name string) {
fmt.Println("Cloning database")
dumpDatabase(file, databaseToCloneName)

importDatabase(importDatabaseName, dump_dir, dump_name, file)

fmt.Println("Cleaning up")
removeDumpFiles(dump_dir, dump_name)

fmt.Println(fmt.Sprintf("%s successfully cloned from %s", importDatabaseName, databaseToCloneName))
}

func removeDumpFiles(dump_dir string, dump_name string) {
cleanup := exec.Command("rm", fmt.Sprintf("%s/%s.sql", dump_dir, dump_name), fmt.Sprintf("%s/%s.sql.bak", dump_dir, dump_name))

cleanup.Run()
}

func dumpDatabase(file *os.File, databaseToCloneName string) {
dump := exec.Command(
"mysqldump",
fmt.Sprintf("%s", viper.GetString("database.database")),
fmt.Sprintf("%s", databaseToCloneName),
fmt.Sprintf("-h%s", viper.GetString("database.host")),
fmt.Sprintf("-u%s", viper.GetString("database.username")),
fmt.Sprintf("-p%s", viper.GetString("database.password")),
fmt.Sprintf("-P%s", viper.GetString("database.port")),
"--databases",
)

fmt.Println(dump.String())
if Verbose {
fmt.Println(dump.String())
}

dump.Stdout = file

err := dump.Run()
if err != nil {
log.Fatalln(err.Error())
}
}

replace := exec.Command(
"sed",
"-i.bak",
fmt.Sprintf("s/%s/%s/g", viper.GetString("database.database"), importDatabase),
fmt.Sprintf("%s/%s.sql", to, name),
)

fmt.Println(replace.String())

replace.Run()
func importDatabase(importDatabase string, to string, name string, file *os.File) {
replaceDatabaseName(importDatabase, to, name)

importCmd := exec.Command(
"mysql",
Expand All @@ -93,6 +110,16 @@ func Clone(file *os.File, importDatabase string, to string, name string) {
fmt.Sprintf("-P%s", viper.GetString("database.port")),
)

addDumpToStdin(importCmd, file)

importCmd.Wait()

if (Verbose) {
fmt.Println(importCmd.String())
}
}

func addDumpToStdin(importCmd *exec.Cmd, file *os.File) {
stdin, err := importCmd.StdinPipe()
if err != nil {
log.Fatal(err)
Expand All @@ -103,23 +130,36 @@ func Clone(file *os.File, importDatabase string, to string, name string) {
}
bytes, _ := ioutil.ReadFile(file.Name())
_, err = io.WriteString(stdin, string(bytes))

if err != nil {
log.Fatal(err)
}

fmt.Println(importCmd.String())
stdin.Close()
}

func replaceDatabaseName(importDatabase string, to string, name string) {
replace := exec.Command(
"sed",
"-i.bak",
fmt.Sprintf("s/%s/%s/g", viper.GetString("database.database"), importDatabase),
fmt.Sprintf("%s/%s.sql", to, name),
)

output, _ := importCmd.Output()
if Verbose {
fmt.Println(replace.String())
}

fmt.Println(string(output))
replace.Run()
}

func init() {
rootCmd.AddCommand(cloneCmd)

home, _ := os.UserHomeDir()

cloneCmd.Flags().String("to", home, "Specify a directory to output the dump file")
cloneCmd.Flags().String("name", "dump", "Specify the name of the dump file")
cloneCmd.Flags().String("database", "cloned", "Specify a directory to output the dump file")
cloneCmd.Flags().String("dump_dir", home, "Specify a directory to output the dump file")
cloneCmd.Flags().String("dump_name", "dump", "Specify the name of the dump file")
cloneCmd.Flags().String("to", "cloned", "Specify name of new database")
cloneCmd.Flags().String("from", viper.GetString("database.database"), "Specify name of database to clone")
}
22 changes: 19 additions & 3 deletions cmd/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ func TestCopyExistingDatabase(t *testing.T) {
}

_, err = db.Exec("drop table if exists test;")
_, err = db.Exec("drop table if exists new_db;")
_, err = db.Exec("create table test (column1 int not null, column2 int not null);")
_, err = db.Exec("drop database if exists new_db;")
_, err = db.Exec("create table test (column1 int not 1null, column2 int not null);")
_, err = db.Exec("insert into test (column1, column2) VALUE (1,2);")

db.Close()

file, _ := os.OpenFile("./test_dump.sql", os.O_RDWR|os.O_CREATE, 0644)

viper.Set("database.database", "original")
Expand All @@ -29,7 +31,9 @@ func TestCopyExistingDatabase(t *testing.T) {
viper.Set("database.password", "secret")
viper.Set("database.port", "3310")

Clone(file, "new_db", ".", "test_dump")
Clone(file, "original", "new_db", ".", "test_dump")

db, _ = sql.Open("mysql", "root:secret@tcp(localhost:3310)/")

rows, _ := db.Query("Show databases")

Expand All @@ -47,4 +51,16 @@ func TestCopyExistingDatabase(t *testing.T) {
if (!ok) {
log.Fatalln("Expected to find new_db in list of databases")
}

_, err = os.Stat("./test_dump.sql")

if (err == nil) {
log.Fatalln("dump file was not removed")
}

_, err = os.Stat("./test_dump.sql.bak")

if (err == nil) {
log.Fatalln("dump backup file was not removed")
}
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

var cfgFile string
var Verbose bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -58,6 +59,7 @@ func init() {
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.surgio-tools.yaml)")
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down

0 comments on commit a3a996d

Please sign in to comment.