-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_status.go
65 lines (51 loc) · 1.69 KB
/
cmd_status.go
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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"github.com/google/subcommands"
"github.com/jacobbrewer1/goschema/pkg/migrations"
"github.com/pterm/pterm"
)
type statusCmd struct{}
func (c *statusCmd) Name() string {
return "status"
}
func (c *statusCmd) Synopsis() string {
return "Print the status of the database migrations."
}
func (c *statusCmd) Usage() string {
return `status:
Print the status of the database migrations.
`
}
func (c *statusCmd) SetFlags(f *flag.FlagSet) {}
func (c *statusCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if e := os.Getenv(migrations.DbEnvVar); e == "" {
slog.Error(fmt.Sprintf("Environment variable %s is not set", migrations.DbEnvVar))
return subcommands.ExitFailure
}
db, err := migrations.ConnectDB()
if err != nil {
slog.Error("Error connecting to the database", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
versions, err := migrations.NewVersioning(db, "", 0).GetStatus()
if err != nil {
slog.Error("Error getting the status", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
tableDataStr := make([][]string, 0)
tableDataStr = append(tableDataStr, []string{"Version", "Current", "Created At"})
for _, v := range versions {
tableDataStr = append(tableDataStr, []string{v.Version, fmt.Sprintf("%t", v.IsCurrent == 1), v.CreatedAt.String()})
}
var tableData pterm.TableData = tableDataStr
if err := pterm.DefaultTable.WithHasHeader().WithBoxed().WithData(tableData).Render(); err != nil {
slog.Error("Error rendering table", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}