aboutsummaryrefslogtreecommitdiffstats
path: root/internal/commands/new.go
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-13 18:23:48 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-13 18:23:48 -0500
commit00aa327eb204d504f1e03dbe5a7cdec4af4ec913 (patch)
tree571a5bc5ce52a11e0039f9763c89b9316e7bf9d4 /internal/commands/new.go
parentTweak readme (diff)
downloadcli-00aa327eb204d504f1e03dbe5a7cdec4af4ec913.zip
Rework docs, add more output, and tweak tests
Diffstat (limited to 'internal/commands/new.go')
-rw-r--r--internal/commands/new.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/commands/new.go b/internal/commands/new.go
new file mode 100644
index 0000000..be1f49c
--- /dev/null
+++ b/internal/commands/new.go
@@ -0,0 +1,81 @@
+package commands
+
+import (
+ "context"
+ "flag"
+ "time"
+
+ "github.com/eugenetriguba/bolt/internal/configloader"
+ "github.com/eugenetriguba/bolt/internal/models"
+ "github.com/eugenetriguba/bolt/internal/output"
+ "github.com/eugenetriguba/bolt/internal/repositories"
+ "github.com/eugenetriguba/bolt/internal/services"
+ "github.com/google/subcommands"
+)
+
+type NewCmd struct {
+ message string
+}
+
+func (*NewCmd) Name() string {
+ return "new"
+}
+
+func (*NewCmd) Synopsis() string {
+ return "create a new database migration"
+}
+
+func (*NewCmd) Usage() string {
+ return `create [-message|-m]:
+ Create a new database migration
+ `
+}
+
+func (cmd *NewCmd) SetFlags(f *flag.FlagSet) {
+ f.StringVar(
+ &cmd.message,
+ "message",
+ "autogenerated",
+ "Message to use for the migration",
+ )
+ f.StringVar(&cmd.message, "m", cmd.message, "alias for -message")
+}
+
+func (cmd *NewCmd) Execute(
+ _ context.Context,
+ f *flag.FlagSet,
+ _ ...interface{},
+) subcommands.ExitStatus {
+ consoleOutputter := output.ConsoleOutputter{}
+
+ cfg, err := configloader.NewConfig()
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ migrationFsRepo, err := repositories.NewMigrationFsRepo(cfg.MigrationsDir)
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ migrationService := services.NewMigrationService(
+ nil,
+ migrationFsRepo,
+ consoleOutputter,
+ )
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ migration := models.NewMigration(time.Now(), cmd.message)
+ err = migrationService.CreateMigration(migration)
+ if err != nil {
+ consoleOutputter.Error(err.Error())
+ return subcommands.ExitFailure
+ }
+
+ return subcommands.ExitSuccess
+}