Skip to content

Commit

Permalink
Merge pull request #272 from dnephin/trim-cmd
Browse files Browse the repository at this point in the history
consolidate cmd routing into main
  • Loading branch information
dnephin committed Sep 3, 2022
2 parents e5943e1 + e440ac6 commit 338c8c8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 69 deletions.
13 changes: 0 additions & 13 deletions cmd/cmd.go

This file was deleted.

24 changes: 13 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,26 @@ func usage(out io.Writer, name string, flags *pflag.FlagSet) {
%[1]s [flags] [--] [go test flags]
%[1]s [command]
See https://pkg.go.dev/gotest.tools/gotestsum#section-readme for detailed documentation.
Flags:
`, name)
flags.SetOutput(out)
flags.PrintDefaults()
fmt.Fprint(out, `
fmt.Fprintf(out, `
Formats:
dots print a character for each test
dots-v2 experimental dots format, one package per line
pkgname print a line for each package
pkgname-and-test-fails print a line for each package and failed test output
testname print a line for each test and package
standard-quiet standard go test format
standard-verbose standard go test -v format
dots print a character for each test
dots-v2 experimental dots format, one package per line
pkgname print a line for each package
pkgname-and-test-fails print a line for each package and failed test output
testname print a line for each test and package
standard-quiet standard go test format
standard-verbose standard go test -v format
Commands:
tool tools for working with test2json output
help print this help next
`)
%[1]s tool slowest find or skip the slowest tests
%[1]s help print this help next
`, name)
}

func lookEnvWithDefault(key, defValue string) string {
Expand Down
20 changes: 11 additions & 9 deletions cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Usage:
gotestsum [flags] [--] [go test flags]
gotestsum [command]

See https://pkg.go.dev/gotest.tools/gotestsum#section-readme for detailed documentation.

Flags:
--debug enabled debug logging
-f, --format string print format of test input (default "short")
Expand All @@ -24,14 +26,14 @@ Flags:
--watch watch go files, and run tests when a file is modified

Formats:
dots print a character for each test
dots-v2 experimental dots format, one package per line
pkgname print a line for each package
pkgname-and-test-fails print a line for each package and failed test output
testname print a line for each test and package
standard-quiet standard go test format
standard-verbose standard go test -v format
dots print a character for each test
dots-v2 experimental dots format, one package per line
pkgname print a line for each package
pkgname-and-test-fails print a line for each package and failed test output
testname print a line for each test and package
standard-quiet standard go test format
standard-verbose standard go test -v format

Commands:
tool tools for working with test2json output
help print this help next
gotestsum tool slowest find or skip the slowest tests
gotestsum help print this help next
33 changes: 0 additions & 33 deletions cmd/tool/cmd.go

This file was deleted.

43 changes: 40 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"fmt"
"os"

"gotest.tools/gotestsum/cmd"
"gotest.tools/gotestsum/cmd/tool"
"gotest.tools/gotestsum/cmd/tool/slowest"
"gotest.tools/gotestsum/internal/log"
)

Expand All @@ -25,13 +26,49 @@ func main() {

func route(args []string) error {
name := args[0]
next, rest := cmd.Next(args[1:])
next, rest := nextArg(args[1:])
switch next {
case "help", "?":
return cmd.Run(name, []string{"--help"})
case "tool":
return tool.Run(name+" "+next, rest)
return toolRun(name+" "+next, rest)
default:
return cmd.Run(name, args[1:])
}
}

// nextArg splits args into the next positional argument and any remaining args.
func nextArg(args []string) (string, []string) {
switch len(args) {
case 0:
return "", nil
case 1:
return args[0], nil
default:
return args[0], args[1:]
}
}

func toolRun(name string, args []string) error {
usage := func(name string) string {
return fmt.Sprintf(`Usage: %[1]s COMMAND [flags]
Commands:
%[1]s slowest find or skip the slowest tests
Use '%[1]s COMMAND --help' for command specific help.
`, name)
}

next, rest := nextArg(args)
switch next {
case "", "help", "?":
fmt.Println(usage(name))
return nil
case "slowest":
return slowest.Run(name+" "+next, rest)
default:
fmt.Fprintln(os.Stderr, usage(name))
return fmt.Errorf("invalid command: %v %v", name, next)
}
}

0 comments on commit 338c8c8

Please sign in to comment.