Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Add NO_COLOR (-no-color) support (pressly#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman authored Oct 20, 2022
1 parent b6c15b9 commit 5a6c34e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
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

// 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

0 comments on commit 5a6c34e

Please sign in to comment.