diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2023-12-30 23:04:22 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2023-12-30 23:04:22 -0500 |
| commit | 6a414432a2cc90523eced1dccb3f5060b5252a08 (patch) | |
| tree | 4c9c06a1cd73ccdad31dbb8f2f7c6b7465631e9b /internal/commands/list.go | |
| parent | Tweak migrations dir error handling (diff) | |
| download | cli-6a414432a2cc90523eced1dccb3f5060b5252a08.zip | |
Add working create, list, and migrate commands
Diffstat (limited to 'internal/commands/list.go')
| -rw-r--r-- | internal/commands/list.go | 58 |
1 files changed, 58 insertions, 0 deletions
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 +} |