Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NO_COLOR (-no-color) support #409

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
*.test

# Files output by tests
/bin
/bin

# Local testing
.envrc
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"runtime/debug"
"strconv"
"text/template"

"github.com/pressly/goose/v3"
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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())
}
Expand All @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
maxVersion = int64((1 << 63) - 1)
timestampFormat = "20060102150405"
verbose = false
noColor = false
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't wait to get rid of these globals.


// base fs to lookup migrations
baseFS fs.FS = osFS{}
Expand Down
6 changes: 5 additions & 1 deletion migration_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down