Skip to content

Commit

Permalink
feat: add goose env command (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jan 19, 2023
1 parent 436452d commit db8fedd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
41 changes: 16 additions & 25 deletions cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"text/template"

"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/internal/cfg"
)

var (
flags = flag.NewFlagSet("goose", flag.ExitOnError)
dir = flags.String("dir", defaultMigrationDir, "directory with migration files")
dir = flags.String("dir", cfg.DefaultMigrationDir, "directory with migration files")
table = flags.String("table", "goose_db_version", "migrations table name")
verbose = flags.Bool("v", false, "enable verbose mode")
help = flags.Bool("h", false, "print help")
Expand Down Expand Up @@ -66,8 +67,8 @@ func main() {

// The -dir option has not been set, check whether the env variable is set
// before defaulting to ".".
if *dir == defaultMigrationDir && os.Getenv(envGooseMigrationDir) != "" {
*dir = os.Getenv(envGooseMigrationDir)
if *dir == cfg.DefaultMigrationDir && cfg.GOOSEMIGRATIONDIR != "" {
*dir = cfg.GOOSEMIGRATIONDIR
}

switch args[0] {
Expand All @@ -86,6 +87,11 @@ func main() {
log.Fatalf("goose run: %v", err)
}
return
case "env":
for _, env := range cfg.List() {
fmt.Printf("%s=%q\n", env.Name, env.Value)
}
return
}

args = mergeArgs(args)
Expand Down Expand Up @@ -140,34 +146,19 @@ func main() {
}

func checkNoColorFromEnv() bool {
if s := os.Getenv(envNoColor); s != "" {
ok, _ := strconv.ParseBool(s)
return ok
}
return false
ok, _ := strconv.ParseBool(cfg.GOOSENOCOLOR)
return ok
}

const (
envGooseDriver = "GOOSE_DRIVER"
envGooseDBString = "GOOSE_DBSTRING"
envGooseMigrationDir = "GOOSE_MIGRATION_DIR"
// https://no-color.org/
envNoColor = "NO_COLOR"
)

const (
defaultMigrationDir = "."
)

func mergeArgs(args []string) []string {
if len(args) < 1 {
return args
}
if d := os.Getenv(envGooseDriver); d != "" {
args = append([]string{d}, args...)
if s := cfg.GOOSEDRIVER; s != "" {
args = append([]string{s}, args...)
}
if d := os.Getenv(envGooseDBString); d != "" {
args = append([]string{args[0], d}, args[1:]...)
if s := cfg.GOOSEDBSTRING; s != "" {
args = append([]string{args[0], s}, args[1:]...)
}
return args
}
Expand Down Expand Up @@ -268,7 +259,7 @@ SELECT 'down SQL query';

// initDir will create a directory with an empty SQL migration file.
func gooseInit(dir string) error {
if dir == "" || dir == defaultMigrationDir {
if dir == "" || dir == cfg.DefaultMigrationDir {
dir = "migrations"
}
_, err := os.Stat(dir)
Expand Down
39 changes: 39 additions & 0 deletions internal/cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cfg

import "os"

var (
GOOSEDRIVER = envOr("GOOSE_DRIVER", "")
GOOSEDBSTRING = envOr("GOOSE_DBSTRING", "")
GOOSEMIGRATIONDIR = envOr("GOOSE_MIGRATION_DIR", DefaultMigrationDir)
// https://no-color.org/
GOOSENOCOLOR = envOr("NO_COLOR", "false")
)

var (
DefaultMigrationDir = "."
)

// An EnvVar is an environment variable Name=Value.
type EnvVar struct {
Name string
Value string
}

func List() []EnvVar {
return []EnvVar{
{Name: "GOOSE_DRIVER", Value: GOOSEDRIVER},
{Name: "GOOSE_DBSTRING", Value: GOOSEDBSTRING},
{Name: "GOOSE_MIGRATION_DIR", Value: GOOSEMIGRATIONDIR},
{Name: "NO_COLOR", Value: GOOSENOCOLOR},
}
}

// envOr returns os.Getenv(key) if set, or else default.
func envOr(key, def string) string {
val := os.Getenv(key)
if val == "" {
val = def
}
return val
}

0 comments on commit db8fedd

Please sign in to comment.