From 470b2c7a7ea7444b6c7dcfb4559802398fe757a0 Mon Sep 17 00:00:00 2001 From: mattes Date: Mon, 11 Aug 2014 04:59:51 +0200 Subject: [PATCH] first draft for cli --- main.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 058d531..3984dae 100644 --- a/main.go +++ b/main.go @@ -8,73 +8,116 @@ import ( "strconv" ) -var url = flag.String("url", "schema://url", "Driver connection URL") -var path = flag.String("path", "", "Path to migrations") +var url = flag.String("url", "", "Driver connection URL, like schema://url") +var migrationsPath = flag.String("path", "", "Path to migrations") func main() { flag.Parse() - command := flag.Arg(0) - if *path == "" { - fmt.Println("Please specify path") - os.Exit(1) - } - switch command { case "create": - createCmd(*url, *path, flag.Arg(1)) + verifyMigrationsPath(*migrationsPath) + createCmd(*url, *migrationsPath, flag.Arg(1)) case "migrate": + verifyMigrationsPath(*migrationsPath) + relativeN := flag.Arg(1) relativeNInt, err := strconv.Atoi(relativeN) if err != nil { fmt.Println(err) os.Exit(1) } - migrateCmd(*url, *path, relativeNInt) + migrateCmd(*url, *migrationsPath, relativeNInt) + + case "up": + verifyMigrationsPath(*migrationsPath) + upCmd(*url, *migrationsPath) + + case "down": + verifyMigrationsPath(*migrationsPath) + downCmd(*url, *migrationsPath) + + case "redo": + verifyMigrationsPath(*migrationsPath) + redoCmd(*url, *migrationsPath) + + case "reset": + verifyMigrationsPath(*migrationsPath) + resetCmd(*url, *migrationsPath) + + case "version": + verifyMigrationsPath(*migrationsPath) + versionCmd(*url, *migrationsPath) case "help": helpCmd() + default: + helpCmd() } - - // fmt.Println(*url) - } -func helpCmd() { - fmt.Fprint(os.Stderr, "Usage of migrate:\n") - flag.PrintDefaults() +func verifyMigrationsPath(path string) { + if path == "" { + fmt.Println("Please specify path") + os.Exit(1) + } } func createCmd(url, migrationsPath, name string) { - files, err := migrate.Create(url, migrationsPath, name) + migrationFile, err := migrate.Create(url, migrationsPath, name) if err != nil { fmt.Println(err) os.Exit(1) } - fmt.Println(files) + + 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) { + if err := migrate.Up(url, migrationsPath); err != nil { + fmt.Println(err) + os.Exit(1) + } } func downCmd(url, migrationsPath string) { + if err := migrate.Down(url, migrationsPath); err != nil { + fmt.Println(err) + os.Exit(1) + } } func redoCmd(url, migrationsPath string) { + if err := migrate.Redo(url, migrationsPath); err != nil { + fmt.Println(err) + os.Exit(1) + } } func resetCmd(url, migrationsPath string) { + if err := migrate.Reset(url, migrationsPath); err != nil { + fmt.Println(err) + os.Exit(1) + } } func versionCmd(url, migrationsPath string) { - + version, err := migrate.Version(url, migrationsPath) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println(version) } func migrateCmd(url, migrationsPath string, relativeN int) { @@ -83,3 +126,19 @@ func migrateCmd(url, migrationsPath string, relativeN int) { os.Exit(1) } } + +func helpCmd() { + os.Stderr.WriteString( + `usage: migrate [-path=] [-url=] [] + +Commands: + create Create a new migration + up Apply all -up- migrations + down Apply all -down- migrations + reset Down followed by Up + redo Roll back most recent migration, then apply it again + version Show current migration version + migrate Apply migrations -n|+n + help Show this help +`) +}