Skip to content

Commit

Permalink
add env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Apr 8, 2024
1 parent a3e575b commit 3ec35b0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 5 additions & 3 deletions internal/cli/cmd_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ type cmdStatus struct {
// flags
dir string
dbstring string
useJSON bool
tablename string
useJSON bool
}

func newStatusCommand(state *state) *ff.Command {
c := cmdStatus{
state: state,
fs: ff.NewFlagSet("status"),
}
// Mandatory flags
_, _ = c.fs.AddFlag(newDirFlag(&c.dir))
_, _ = c.fs.AddFlag(newDBStringFlag(&c.dbstring))
// Optional flags
c.fs.BoolConfig(newJSONFlag(&c.useJSON))
c.fs.StringConfig(newDirFlag(&c.dir), "")
c.fs.StringConfig(newDBStringFlag(&c.dbstring), "")
c.fs.StringConfig(newTablenameFlag(&c.tablename), goose.DefaultTablename)

return &ff.Command{
Expand Down
6 changes: 5 additions & 1 deletion internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func run(ctx context.Context, state *state, args []string) error {
root.Subcommands = append(root.Subcommands, newStatusCommand(state))

// Parse the flags and return help if requested.
if err := root.Parse(args); err != nil {
err := root.Parse(
args,
ff.WithEnvVarPrefix("GOOSE"), // Support environment variables for all flags
)
if err != nil {
if errors.Is(err, ff.ErrHelp) {
fmt.Fprintf(state.stderr, "\n%s\n", createHelp(root))
return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func (s *state) initProvider(
options ...goose.ProviderOption,
) (*goose.Provider, error) {
if dir == "" {
return nil, fmt.Errorf("dir is required")
return nil, fmt.Errorf("migrations directory is required, set with --dir or GOOSE_DIR")
}
if dbstring == "" {
return nil, errors.New("dbstring is required")
return nil, errors.New("database connection string is required, set with --dbstring or GOOSE_DBSTRING")
}
db, dialect, err := openConnection(dbstring)
if err != nil {
Expand Down

0 comments on commit 3ec35b0

Please sign in to comment.