diff --git a/README.md b/README.md index 4de698a..05c7936 100644 --- a/README.md +++ b/README.md @@ -84,4 +84,10 @@ func main() { os.Exit(1) } } -``` \ No newline at end of file +``` +### Migration generator +This library contains helper tool for transports. You can install it via go: +```sh +$ go get -u github.com/retailcrm/mg-transport-core/cmd/transport-core-tool +``` +Currently, it only can generate new migrations for your transport. \ No newline at end of file diff --git a/cmd/transport-core-tool/main.go b/cmd/transport-core-tool/main.go new file mode 100644 index 0000000..0c848a3 --- /dev/null +++ b/cmd/transport-core-tool/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "os" + + "github.com/jessevdk/go-flags" + "github.com/retailcrm/mg-transport-core/core" +) + +type Options struct{} + +var ( + options = Options{} + parser = flags.NewParser(&options, flags.Default) +) + +func init() { + _, err := parser.AddCommand("migration", + "Create new empty migration in specified directory.", + "Create new empty migration in specified directory.", + &core.NewMigrationCommand{}, + ) + + if err != nil { + panic(err.Error()) + } +} + +func main() { + if _, err := parser.Parse(); err != nil { + if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { + os.Exit(0) + } else { + os.Exit(1) + } + } +} diff --git a/core/migration_generator.go b/core/migration_generator.go new file mode 100644 index 0000000..a48651b --- /dev/null +++ b/core/migration_generator.go @@ -0,0 +1,84 @@ +package core + +import ( + "fmt" + "os" + "path" + "strconv" + "strings" + "time" +) + +var migrationTemplate = `package $package + +import ( + "github.com/jinzhu/gorm" + "github.com/retailcrm/mg-transport-core/core" + "gopkg.in/gormigrate.v1" +) + +func init() { + core.Migrations().Add(&gormigrate.Migration{ + ID: "$version", + Migrate: func(db *gorm.DB) error { + // Write your migration code here... + }, + Rollback: func(db *gorm.DB) error { + // Write your migration rollback code here... + }, + }) +} +` + +// NewMigrationCommand struct +type NewMigrationCommand struct { + Directory string `short:"d" long:"directory" default:"./migrations" description:"Directory where migration will be created"` +} + +func (x *NewMigrationCommand) FileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func (x *NewMigrationCommand) Execute(args []string) error { + version := strconv.FormatInt(time.Now().Unix(), 10) + directory := path.Clean(x.Directory) + packageName := "migrations" + + if _, err := os.Stat(directory); os.IsNotExist(err) { + return fmt.Errorf("err: specified directory doesn't exist") + } + + if base := path.Base(directory); base != "/" && base != "." { + packageName = base + } + + filePath := path.Join(directory, version+"_app.go") + if x.FileExists(filePath) { + return fmt.Errorf("\"%s\" already exists or it's a directory", filePath) + } + + migrationData := strings.Replace( + strings.Replace(migrationTemplate, "$version", version, 1), + "$package", + packageName, + 1, + ) + + file, err := os.Create(filePath) + if err != nil { + return err + } + + defer file.Close() + + if _, err := file.WriteString(migrationData); err != nil { + return err + } + + fmt.Println("Created new migration: " + filePath) + return nil +} diff --git a/go.mod b/go.mod index ed243d0..77bebc4 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/golang/protobuf v1.3.2 // indirect github.com/google/go-querystring v1.0.0 // indirect github.com/h2non/gock v1.0.10 + github.com/jessevdk/go-flags v1.4.0 // indirect github.com/jinzhu/gorm v1.9.10 github.com/json-iterator/go v1.1.7 // indirect github.com/lib/pq v1.2.0 // indirect diff --git a/go.sum b/go.sum index c4b4ba9..399e4f9 100644 --- a/go.sum +++ b/go.sum @@ -87,6 +87,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jinzhu/gorm v1.9.2 h1:lCvgEaqe/HVE+tjAR2mt4HbbHAZsQOv3XAZiEZV37iw= github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo= github.com/jinzhu/gorm v1.9.10 h1:HvrsqdhCW78xpJF67g1hMxS6eCToo9PZH4LDB8WKPac=