refactor cli

This commit is contained in:
mattes 2014-08-14 00:22:56 +02:00
parent 0067abbe92
commit fa983e8eb7
2 changed files with 57 additions and 78 deletions

133
main.go
View File

@ -16,13 +16,17 @@ import (
"time"
)
var url = flag.String("url", "", "Driver connection URL, like schema://url")
var migrationsPath = flag.String("path", "", "Path to migrations")
var url = flag.String("url", "", "")
var migrationsPath = flag.String("path", "", "")
func main() {
flag.Parse()
command := flag.Arg(0)
if *migrationsPath == "" {
*migrationsPath, _ = os.Getwd()
}
switch command {
case "create":
verifyMigrationsPath(*migrationsPath)
@ -31,7 +35,16 @@ func main() {
fmt.Println("Please specify name.")
os.Exit(1)
}
createCmd(*url, *migrationsPath, name)
migrationFile, err := migrate.Create(*url, *migrationsPath, name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Version %v migration files created in %v:\n", migrationFile.Version, migrationsPath)
fmt.Println(migrationFile.UpFile.FileName)
fmt.Println(migrationFile.DownFile.FileName)
case "migrate":
verifyMigrationsPath(*migrationsPath)
@ -41,27 +54,52 @@ func main() {
fmt.Println("Unable to parse parse param <n>.")
os.Exit(1)
}
migrateCmd(*url, *migrationsPath, relativeNInt)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, *url, *migrationsPath, relativeNInt)
writePipe(pipe)
printTimer()
case "up":
verifyMigrationsPath(*migrationsPath)
upCmd(*url, *migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Up(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
case "down":
verifyMigrationsPath(*migrationsPath)
downCmd(*url, *migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Down(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
case "redo":
verifyMigrationsPath(*migrationsPath)
redoCmd(*url, *migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Redo(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
case "reset":
verifyMigrationsPath(*migrationsPath)
resetCmd(*url, *migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Reset(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
case "version":
verifyMigrationsPath(*migrationsPath)
versionCmd(*url, *migrationsPath)
version, err := migrate.Version(*url, *migrationsPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(version)
case "help":
helpCmd()
@ -71,13 +109,6 @@ func main() {
}
}
func verifyMigrationsPath(path string) {
if path == "" {
fmt.Println("Please specify path")
os.Exit(1)
}
}
func writePipe(pipe chan interface{}) {
if pipe != nil {
for {
@ -115,6 +146,13 @@ func writePipe(pipe chan interface{}) {
}
}
func verifyMigrationsPath(path string) {
if path == "" {
fmt.Println("Please specify path")
os.Exit(1)
}
}
var timerStart time.Time
func printTimer() {
@ -126,67 +164,6 @@ func printTimer() {
}
}
func createCmd(url, migrationsPath, name string) {
migrationFile, err := migrate.Create(url, migrationsPath, name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Version %v migration files created in %v:\n", migrationFile.Version, migrationsPath)
fmt.Println(migrationFile.UpFile.FileName)
fmt.Println(migrationFile.DownFile.FileName)
}
func upCmd(url, migrationsPath string) {
timerStart = time.Now()
pipe := pipep.New()
go migrate.Up(pipe, url, migrationsPath)
writePipe(pipe)
printTimer()
}
func downCmd(url, migrationsPath string) {
timerStart = time.Now()
pipe := pipep.New()
go migrate.Down(pipe, url, migrationsPath)
writePipe(pipe)
printTimer()
}
func redoCmd(url, migrationsPath string) {
timerStart = time.Now()
pipe := pipep.New()
go migrate.Redo(pipe, url, migrationsPath)
writePipe(pipe)
printTimer()
}
func resetCmd(url, migrationsPath string) {
timerStart = time.Now()
pipe := pipep.New()
go migrate.Reset(pipe, url, migrationsPath)
writePipe(pipe)
printTimer()
}
func migrateCmd(url, migrationsPath string, relativeN int) {
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, url, migrationsPath, relativeN)
writePipe(pipe)
printTimer()
}
func versionCmd(url, migrationsPath string) {
version, err := migrate.Version(url, migrationsPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(version)
}
func helpCmd() {
os.Stderr.WriteString(
`usage: migrate [-path=<path>] [-url=<url>] <command> [<args>]
@ -200,5 +177,7 @@ Commands:
version Show current migration version
migrate <n> Apply migrations -n|+n
help Show this help
'-path' defaults to current working directory.
`)
}

View File

@ -35,7 +35,7 @@ func WaitAndRedirect(pipe, redirectPipe chan interface{}, interrupt chan os.Sign
os.Exit(5)
} else {
// add white space at beginning for ^C splitting
redirectPipe <- " Aborting after this migration ..."
redirectPipe <- " Aborting after this migration ... Hit again to force quit."
}
case item, ok := <-pipe: