Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consolidate cmd routing into main #272

Merged
merged 1 commit into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -115,24 +115,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 @@ -23,14 +25,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)
}
}