From 6a414432a2cc90523eced1dccb3f5060b5252a08 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 30 Dec 2023 23:04:22 -0500 Subject: Add working create, list, and migrate commands --- internal/commands/list.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 internal/commands/list.go (limited to 'internal/commands/list.go') diff --git a/internal/commands/list.go b/internal/commands/list.go new file mode 100644 index 0000000..ba7958d --- /dev/null +++ b/internal/commands/list.go @@ -0,0 +1,58 @@ +package commands + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/eugenetriguba/bolt/internal/config" + "github.com/eugenetriguba/bolt/internal/db" + "github.com/eugenetriguba/bolt/internal/repositories" + "github.com/google/subcommands" +) + +type ListCmd struct{} + +func (*ListCmd) Name() string { return "list" } +func (*ListCmd) Synopsis() string { return "List the database migrations and their statuses." } +func (*ListCmd) Usage() string { + return `list: + List the database migrations and their statuses. + ` +} + +func (m *ListCmd) SetFlags(f *flag.FlagSet) {} + +func (m *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + db, err := db.Connect() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + defer db.Close() + + c, err := config.NewConfig() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + + migrationRepo, err := repositories.NewMigrationRepo(db, c) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + + migrations, err := migrationRepo.List() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + + for _, migration := range migrations { + fmt.Println(migration) + } + + return subcommands.ExitSuccess +} -- cgit v1.2.3