aboutsummaryrefslogtreecommitdiffstats
path: root/internal/commands/new.go
blob: 54dac55e46b9c3936b155eb09172939ae7bf3c32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package commands

import (
	"context"
	"flag"
	"fmt"

	"git.eugenetriguba.com/bolt/cli/internal/configloader"
	"git.eugenetriguba.com/bolt/cli/internal/output"
	"git.eugenetriguba.com/bolt/cli/internal/repositories"
	"git.eugenetriguba.com/bolt/cli/internal/services"
	"git.eugenetriguba.com/subcmd"
)

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 `new [-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{},
) subcmd.ExitStatus {
	consoleOutputter := output.NewConsoleOutputter()

	cfg, err := configloader.NewConfig()
	if err != nil {
		consoleOutputter.Error(fmt.Errorf("unable to retrieve configuration: %w", err))
		return subcmd.ExitFailure
	}

	migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations)
	if err != nil {
		consoleOutputter.Error(
			fmt.Errorf("unable to setup local migrations directory: %w", err),
		)
		return subcmd.ExitFailure
	}

	migrationService := services.NewMigrationService(
		nil,
		migrationFsRepo,
		consoleOutputter,
	)

	_, err = migrationService.CreateMigration(cmd.message)
	if err != nil {
		consoleOutputter.Error(fmt.Errorf("unable to create new migration: %w", err))
		return subcmd.ExitFailure
	}

	return subcmd.ExitSuccess
}