From 1a56834173abad5858f577573a81d2ab5c83d591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 26 Dec 2024 12:28:08 +0000 Subject: [PATCH] use the latest testscript to drop func() int And don't fail when requesting help, which is what Go's flag package has been moving towards. --- go.mod | 2 +- go.sum | 4 ++-- main.go | 21 ++++++++------------- main_test.go | 8 ++++---- testdata/script/help.txtar | 8 ++++---- 5 files changed, 19 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index b66e3498..c427c54b 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/bluekeyes/go-gitdiff v0.8.0 github.com/go-quicktest/qt v1.101.0 github.com/google/go-cmp v0.6.0 - github.com/rogpeppe/go-internal v1.13.1 + github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a golang.org/x/mod v0.22.0 golang.org/x/tools v0.27.0 ) diff --git a/go.sum b/go.sum index 34f422d6..a482f435 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= -github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGKbLGBPtR/8/oO74W6hmz0qE5q0z9aqSAewaaM= +github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= diff --git a/main.go b/main.go index 81adefac..9a203ced 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ import ( "mvdan.cc/garble/internal/literals" ) -var flagSet = flag.NewFlagSet("garble", flag.ContinueOnError) +var flagSet = flag.NewFlagSet("garble", flag.ExitOnError) var ( flagLiterals bool @@ -146,8 +146,6 @@ For more information, see https://github.com/burrowers/garble. `[1:]) } -func main() { os.Exit(main1()) } - var ( // Presumably OK to share fset across packages. fset = token.NewFileSet() @@ -220,7 +218,7 @@ func debugSince(start time.Time) time.Duration { return time.Since(start).Truncate(10 * time.Microsecond) } -func main1() int { +func main() { if dir := os.Getenv("GARBLE_WRITE_CPUPROFILES"); dir != "" { f, err := os.CreateTemp(dir, "garble-cpu-*.pprof") if err != nil { @@ -256,9 +254,7 @@ func main1() int { fmt.Fprintf(os.Stderr, "garble allocs: %d\n", memStats.Mallocs) } }() - if err := flagSet.Parse(os.Args[1:]); err != nil { - return 2 - } + flagSet.Parse(os.Args[1:]) log.SetPrefix("[garble] ") log.SetFlags(0) // no timestamps, as they aren't very useful if flagDebug { @@ -270,7 +266,7 @@ func main1() int { args := flagSet.Args() if len(args) < 1 { usage() - return 2 + os.Exit(2) } // If a random seed was used, the user won't be able to reproduce the @@ -283,12 +279,11 @@ func main1() int { } if err := mainErr(args); err != nil { if code, ok := err.(errJustExit); ok { - return int(code) + os.Exit(int(code)) } fmt.Fprintln(os.Stderr, err) - return 1 + os.Exit(1) } - return 0 } type errJustExit int @@ -358,13 +353,13 @@ func mainErr(args []string) error { case "help": if hasHelpFlag(args) || len(args) > 1 { fmt.Fprintf(os.Stderr, "usage: garble help [command]\n") - return errJustExit(2) + return errJustExit(0) } if len(args) == 1 { return mainErr([]string{args[0], "-h"}) } usage() - return errJustExit(2) + return errJustExit(0) case "version": if hasHelpFlag(args) || len(args) > 0 { fmt.Fprintf(os.Stderr, "usage: garble version\n") diff --git a/main_test.go b/main_test.go index 0ea0a349..396365ed 100644 --- a/main_test.go +++ b/main_test.go @@ -41,11 +41,11 @@ func TestMain(m *testing.M) { os.Setenv("GORACE", "atexit_sleep_ms=10") } if os.Getenv("RUN_GARBLE_MAIN") == "true" { - os.Exit(main1()) + main() } - os.Exit(testscript.RunMain(garbleMain{m}, map[string]func() int{ - "garble": main1, - })) + testscript.Main(garbleMain{m}, map[string]func(){ + "garble": main, + }) } type garbleMain struct { diff --git a/testdata/script/help.txtar b/testdata/script/help.txtar index 3f324a82..22b6b3e0 100644 --- a/testdata/script/help.txtar +++ b/testdata/script/help.txtar @@ -4,22 +4,22 @@ stderr 'garble \[garble flags\] command' ! stderr 'usage: go build' ! stdout . -! exec garble -h +exec garble -h stderr 'Garble obfuscates Go code' stderr 'garble \[garble flags\] command' ! stdout . -! exec garble help +exec garble help stderr 'Garble obfuscates Go code' stderr 'garble \[garble flags\] command' ! stdout . -! exec garble help foo bar +exec garble help foo bar stderr 'usage: garble help' ! stderr 'Garble obfuscates Go code' ! stdout . -! exec garble help -h +exec garble help -h stderr 'usage: garble help' ! stdout .