Skip to content

Commit

Permalink
Merge pull request #125 from pressly/fix_goose_panic
Browse files Browse the repository at this point in the history
clean up arg parsing a bit
  • Loading branch information
VojtechVitek committed Nov 13, 2018
2 parents da4711c + 428691c commit 82b1074
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
18 changes: 7 additions & 11 deletions cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,28 @@ func main() {
flags.Parse(os.Args[1:])

args := flags.Args()
if len(args) == 0 || args[0] == "-h" || args[0] == "--help" {
flags.Usage()
return
}

if len(args) > 1 && args[0] == "create" {
switch args[0] {
case "create":
if err := goose.Run("create", nil, *dir, args[1:]...); err != nil {
log.Fatalf("goose run: %v", err)
}
return
}

// TODO clean up arg/flag parsing flow
if args[0] == "fix" {
case "fix":
if err := goose.Run("fix", nil, *dir); err != nil {
log.Fatalf("goose run: %v", err)
}
return
}

if len(args) < 3 {
flags.Usage()
return
}

if args[0] == "-h" || args[0] == "--help" {
flags.Usage()
return
}

driver, dbstring, command := args[0], args[1], args[2]

if err := goose.SetDialect(driver); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions examples/go-migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ func main() {
flags.Parse(os.Args[1:])

args := flags.Args()
if len(args) == 0 || args[0] == "-h" || args[0] == "--help" {
flags.Usage()
return
}

if len(args) > 1 && args[0] == "create" {
switch args[0] {
case "create":
if err := goose.Run("create", nil, *dir, args[1:]...); err != nil {
log.Fatalf("goose run: %v", err)
}
return
}

// TODO clean up arg/flag parsing flow
if args[0] == "fix" {
case "fix":
if err := goose.Run("fix", nil, *dir); err != nil {
log.Fatalf("goose run: %v", err)
}
return
}

if len(args) < 3 {
Expand Down
9 changes: 8 additions & 1 deletion goose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ func TestDefaultBinary(t *testing.T) {
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db version",
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db down",
"./bin/goose -dir=examples/sql-migrations sqlite3 sql.db status",
"./bin/goose",
}

for _, cmd := range commands {
args := strings.Split(cmd, " ")
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
command := args[0]
var params []string
if len(args) > 1 {
params = args[1:]
}

out, err := exec.Command(command, params...).CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
Expand Down

0 comments on commit 82b1074

Please sign in to comment.