gotest.tools/gotestsum@v1.11.0/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"gotest.tools/gotestsum/cmd"
     8  	"gotest.tools/gotestsum/cmd/tool/matrix"
     9  	"gotest.tools/gotestsum/cmd/tool/slowest"
    10  	"gotest.tools/gotestsum/internal/log"
    11  )
    12  
    13  func main() {
    14  	err := route(os.Args)
    15  	switch {
    16  	case err == nil:
    17  		return
    18  	case cmd.IsExitCoder(err):
    19  		// go test should already report the error to stderr, exit with
    20  		// the same status code
    21  		os.Exit(cmd.ExitCodeWithDefault(err))
    22  	default:
    23  		log.Error(err.Error())
    24  		os.Exit(3)
    25  	}
    26  }
    27  
    28  func route(args []string) error {
    29  	name := args[0]
    30  	next, rest := nextArg(args[1:])
    31  	switch next {
    32  	case "help", "?":
    33  		return cmd.Run(name, []string{"--help"})
    34  	case "tool":
    35  		return toolRun(name+" "+next, rest)
    36  	default:
    37  		return cmd.Run(name, args[1:])
    38  	}
    39  }
    40  
    41  // nextArg splits args into the next positional argument and any remaining args.
    42  func nextArg(args []string) (string, []string) {
    43  	switch len(args) {
    44  	case 0:
    45  		return "", nil
    46  	case 1:
    47  		return args[0], nil
    48  	default:
    49  		return args[0], args[1:]
    50  	}
    51  }
    52  
    53  func toolRun(name string, args []string) error {
    54  	usage := func(name string) string {
    55  		return fmt.Sprintf(`Usage: %[1]s COMMAND [flags]
    56  
    57  Commands:
    58      %[1]s slowest      find or skip the slowest tests
    59      %[1]s ci-matrix    use previous test runtime to place packages into optimal buckets
    60  
    61  Use '%[1]s COMMAND --help' for command specific help.
    62  `, name)
    63  	}
    64  
    65  	next, rest := nextArg(args)
    66  	switch next {
    67  	case "", "help", "?":
    68  		fmt.Println(usage(name))
    69  		return nil
    70  	case "slowest":
    71  		return slowest.Run(name+" "+next, rest)
    72  	case "ci-matrix":
    73  		return matrix.Run(name+" "+next, rest)
    74  	default:
    75  		fmt.Fprintln(os.Stderr, usage(name))
    76  		return fmt.Errorf("invalid command: %v %v", name, next)
    77  	}
    78  }