From c6d56a651f84c2f60e76fe32b585929649837018 Mon Sep 17 00:00:00 2001 From: Alex Bogomolov Date: Wed, 17 Jan 2018 18:20:08 +0200 Subject: [PATCH] add option to choose timestamp format for create --- cli/commands.go | 2 +- cli/main.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 703896dc..1b7c5a6d 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -8,7 +8,7 @@ import ( "fmt" ) -func createCmd(dir string, timestamp int64, name string, ext string) { +func createCmd(dir string, timestamp interface{}, name string, ext string) { base := fmt.Sprintf("%v%v_%v.", dir, timestamp, name) os.MkdirAll(dir, os.ModePerm) createFile(base + "up" + ext) diff --git a/cli/main.go b/cli/main.go index 4c727a97..9d21ce60 100644 --- a/cli/main.go +++ b/cli/main.go @@ -42,8 +42,9 @@ Options: -help Print usage Commands: - create [-ext E] [-dir D] NAME + create [-ext E] [-dir D] [-timestamp unix] NAME Create a set of timestamped up/down migrations titled NAME, in directory D with extension E + -timestamp accepts "unix" for Unix timestamps (e.g. 1516205943) and "datetime" or "time" for datetime timestamps (e.g. 180117181903), default: "unix" goto V Migrate to version V up [N] Apply all or N up migrations down [N] Apply all or N down migrations @@ -110,6 +111,7 @@ Commands: 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)") + timestampFormatPtr := createFlagSet.String("timestamp", "unix", `Format of a timestamp ("unix" for Unix timestamp, "datetime" or "time" for datetime timestamp, default: "unix")`) createFlagSet.Parse(args) if createFlagSet.NArg() == 0 { @@ -124,7 +126,13 @@ Commands: *dirPtr = strings.Trim(*dirPtr, "/") + "/" } - timestamp := startTime.Unix() + var timestamp interface{} + + if *timestampFormatPtr == "datetime" || *timestampFormatPtr == "time" { + timestamp = startTime.Format("060102150405") + } else { + timestamp = startTime.Unix() + } createCmd(*dirPtr, timestamp, name, *extPtr)