From 5193dcbfd4963b739254fc8f90bfbf9e69953236 Mon Sep 17 00:00:00 2001 From: Michael Fridman Date: Tue, 3 Sep 2024 08:05:36 -0400 Subject: [PATCH] feat: add ability to parse flags to the end (#814) --- CHANGELOG.md | 6 ++++++ cmd/goose/main.go | 4 +++- go.mod | 3 ++- go.sum | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f006d5fac..28057819c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Minimum Go version is now 1.21 - Add Unwrap to PartialError (#815) +- Allow flags anywhere on the CLI (#814) + +`goose` uses the default Go `flag` parsing library, which means flags **must** be defined before the +first positional argument. We've updated this behavior to allow flags to be defined anywhere. For +more details, see [blog post](https://mfridman.com/blog/2024/allowing-flags-anywhere-on-the-cli/). + - Update `WithDisableGlobalRegistry` behavior (#783). When set, this will ignore globally-registered migrationse entirely instead of the previous behavior of raising an error. Specifically, the following check is removed: diff --git a/cmd/goose/main.go b/cmd/goose/main.go index 82fca3c10..8a8f86f92 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -17,6 +17,7 @@ import ( "text/tabwriter" "text/template" + "github.com/mfridman/xflag" "github.com/pressly/goose/v3" "github.com/pressly/goose/v3/internal/cfg" "github.com/pressly/goose/v3/internal/migrationstats" @@ -45,7 +46,8 @@ func main() { ctx := context.Background() flags.Usage = usage - if err := flags.Parse(os.Args[1:]); err != nil { + + if err := xflag.ParseToEnd(flags, os.Args[1:]); err != nil { log.Fatalf("failed to parse args: %v", err) return } diff --git a/go.mod b/go.mod index 5fce935f7..df9ed17df 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,13 @@ module github.com/pressly/goose/v3 -go 1.21 +go 1.21.0 require ( github.com/ClickHouse/clickhouse-go/v2 v2.27.1 github.com/go-sql-driver/mysql v1.8.1 github.com/jackc/pgx/v5 v5.6.0 github.com/mfridman/interpolate v0.0.2 + github.com/mfridman/xflag v0.0.0-20240825232106-efb77353e578 github.com/microsoft/go-mssqldb v1.7.2 github.com/sethvargo/go-retry v0.3.0 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index 34ce2a774..daf0d5c96 100644 --- a/go.sum +++ b/go.sum @@ -151,6 +151,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/mfridman/xflag v0.0.0-20240825232106-efb77353e578 h1:CRrqlUmLebb/QjzRDWE0E66+YyN/v95+w6WyH9ju8/Y= +github.com/mfridman/xflag v0.0.0-20240825232106-efb77353e578/go.mod h1:/483ywM5ZO5SuMVjrIGquYNE5CzLrj5Ux/LxWWnjRaE= github.com/microsoft/go-mssqldb v1.7.2 h1:CHkFJiObW7ItKTJfHo1QX7QBBD1iV+mn1eOyRP3b/PA= github.com/microsoft/go-mssqldb v1.7.2/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=