first draft for cli

This commit is contained in:
mattes 2014-08-11 04:59:51 +02:00
parent 34e959f3b5
commit 470b2c7a7e
1 changed files with 78 additions and 19 deletions

97
main.go
View File

@ -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=<path>] [-url=<url>] <command> [<args>]
Commands:
create <name> 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 <n> Apply migrations -n|+n
help Show this help
`)
}