diff --git a/.gitignore b/.gitignore index 530931a..84f1690 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ *.test # Files output by tests -/bin \ No newline at end of file +/bin + +# Local testing +.envrc diff --git a/Makefile b/Makefile index 78f602b..e401f06 100644 --- a/Makefile +++ b/Makefile @@ -24,3 +24,12 @@ test-clickhouse: docker-cleanup: docker stop -t=0 $$(docker ps --filter="label=goose_test" -aq) + +start-postgres: + docker run --rm -d \ + -e POSTGRES_USER=${GOOSE_POSTGRES_DB_USER} \ + -e POSTGRES_PASSWORD=${GOOSE_POSTGRES_PASSWORD} \ + -e POSTGRES_DB=${GOOSE_POSTGRES_DBNAME} \ + -p ${GOOSE_POSTGRES_PORT}:5432 \ + -l goose_test \ + postgres:14-alpine diff --git a/cmd/goose/main.go b/cmd/goose/main.go index a0d2d84..ed0cece 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "runtime/debug" + "strconv" "text/template" "github.com/pressly/goose/v3" @@ -26,6 +27,7 @@ var ( sslcert = flags.String("ssl-cert", "", "file path to SSL certificates in pem format (only support on mysql)") sslkey = flags.String("ssl-key", "", "file path to SSL key in pem format (only support on mysql)") noVersioning = flags.Bool("no-versioning", false, "apply migration commands with no versioning, in file order, from directory pointed to") + noColor = flags.Bool("no-color", false, "disable color output (NO_COLOR env variable supported)") ) var ( gooseVersion = "" @@ -116,8 +118,10 @@ func main() { if len(args) > 3 { arguments = append(arguments, args[3:]...) } - options := []goose.OptionsFunc{} + if *noColor || checkNoColorFromEnv() { + options = append(options, goose.WithNoColor(true)) + } if *allowMissing { options = append(options, goose.WithAllowMissing()) } @@ -135,10 +139,20 @@ func main() { } } +func checkNoColorFromEnv() bool { + if s := os.Getenv(envNoColor); s != "" { + ok, _ := strconv.ParseBool(s) + return ok + } + return false +} + const ( envGooseDriver = "GOOSE_DRIVER" envGooseDBString = "GOOSE_DBSTRING" envGooseMigrationDir = "GOOSE_MIGRATION_DIR" + // https://no-color.org/ + envNoColor = "NO_COLOR" ) const ( diff --git a/goose.go b/goose.go index bcf1df4..0dbfd67 100644 --- a/goose.go +++ b/goose.go @@ -15,6 +15,7 @@ var ( maxVersion = int64((1 << 63) - 1) timestampFormat = "20060102150405" verbose = false + noColor = false // base fs to lookup migrations baseFS fs.FS = osFS{} diff --git a/migration_sql.go b/migration_sql.go index 359ebf6..49968b3 100644 --- a/migration_sql.go +++ b/migration_sql.go @@ -113,7 +113,11 @@ const ( func verboseInfo(s string, args ...interface{}) { if verbose { - log.Printf(grayColor+s+resetColor, args...) + if noColor { + log.Printf(s, args...) + } else { + log.Printf(grayColor+s+resetColor, args...) + } } } diff --git a/up.go b/up.go index d8d19cf..1d668e3 100644 --- a/up.go +++ b/up.go @@ -24,6 +24,10 @@ func WithNoVersioning() OptionsFunc { return func(o *options) { o.noVersioning = true } } +func WithNoColor(b bool) OptionsFunc { + return func(o *options) { noColor = b } +} + func withApplyUpByOne() OptionsFunc { return func(o *options) { o.applyUpByOne = true } }