From 3239b1867142f89f607f92f1f3f9b0c251b6881d Mon Sep 17 00:00:00 2001 From: Dale Hui Date: Mon, 18 Jun 2018 00:06:17 -0700 Subject: [PATCH] Fix time format flag to be backwards compatible https://github.com/golang-migrate/migrate/pull/17#issuecomment-397960867 --- cli/commands.go | 20 +++++++++++--------- cli/main.go | 8 ++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 205cbe2..de35cf0 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -49,9 +49,9 @@ func nextSeq(matches []string, dir string, seqDigits int) (string, error) { return nextSeqStr, nil } -func createCmd(dir string, timestamp int64, format string, name string, ext string, seq bool, seqDigits int) { +func createCmd(dir string, startTime time.Time, format string, name string, ext string, seq bool, seqDigits int) { var base string - if seq && len(format) > 0 { + if seq && format != defaultTimeFormat { log.fatalErr(errors.New("The seq and format options are mutually exclusive")) } if seq { @@ -68,13 +68,15 @@ func createCmd(dir string, timestamp int64, format string, name string, ext stri } base = fmt.Sprintf("%v%v_%v.", dir, nextSeqStr, name) } else { - if len(format) > 0 { - t := time.Unix(timestamp, 0) - version := t.Format(format) - base = fmt.Sprintf("%v%v_%v.", dir, version, name) - } else { - log.Println("Time format not specified") - base = fmt.Sprintf("%v%v_%v.", dir, timestamp, name) + switch format { + case "": + log.fatal("Time format may not be empty") + case "unix": + base = fmt.Sprintf("%v%v_%v.", dir, startTime.Unix(), name) + case "unixNano": + base = fmt.Sprintf("%v%v_%v.", dir, startTime.UnixNano(), name) + default: + base = fmt.Sprintf("%v%v_%v.", dir, startTime.Format(format), name) } } diff --git a/cli/main.go b/cli/main.go index d68da95..f867fdc 100644 --- a/cli/main.go +++ b/cli/main.go @@ -15,6 +15,8 @@ import ( "github.com/golang-migrate/migrate/source" ) +const defaultTimeFormat = "20060102150405" + // set main log var log = &Log{} @@ -118,7 +120,7 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n") createFlagSet := flag.NewFlagSet("create", flag.ExitOnError) extPtr := createFlagSet.String("ext", "", "File extension") dirPtr := createFlagSet.String("dir", "", "Directory to place file in (default: current working directory)") - formatPtr := createFlagSet.String("format", "", "Specify the format of the version using a Go time format string. For yyyymmddhhmmss use format of 20060102150405. If not specified the version will be the unix timestamp.") + formatPtr := createFlagSet.String("format", defaultTimeFormat, `The Go time format string to use. If the string "unix" or "unixNano" is specified, then the seconds or nanoseconds since January 1, 1970 UTC respectively will be used. Caution, due to the behavior of time.Time.Format(), invalid format strings will not error`) createFlagSet.BoolVar(&seq, "seq", seq, "Use sequential numbers instead of timestamps (default: false)") createFlagSet.IntVar(&seqDigits, "digits", seqDigits, "The number of digits to use in sequences (default: 6)") createFlagSet.Parse(args) @@ -137,9 +139,7 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n") *dirPtr = strings.Trim(*dirPtr, "/") + "/" } - timestamp := startTime.Unix() - - createCmd(*dirPtr, timestamp, *formatPtr, name, *extPtr, seq, seqDigits) + createCmd(*dirPtr, startTime, *formatPtr, name, *extPtr, seq, seqDigits) case "goto": if migraterErr != nil {