Skip to content

Commit

Permalink
use the latest testscript to drop func() int
Browse files Browse the repository at this point in the history
And don't fail when requesting help, which is what Go's flag package
has been moving towards.
  • Loading branch information
mvdan authored and lu4p committed Dec 26, 2024
1 parent 7678613 commit 76905ba
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 8 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions testdata/script/help.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

Expand Down

0 comments on commit 76905ba

Please sign in to comment.