github.com/ks888/tgo@v0.0.0-20190130135156-80bf89407292/cmd/tgo/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/ks888/tgo/log"
     9  	"github.com/ks888/tgo/service"
    10  )
    11  
    12  const (
    13  	traceOptionDesc      = "The tracing is enabled when this `function` is called and then disabled when returned."
    14  	tracelevelOptionDesc = "Functions are traced if the stack depth is within this `tracelevel`. The stack depth here is based on the point the tracing is enabled."
    15  	parselevelOptionDesc = "The trace log includes the function's args. The `parselevel` option determines how detailed these values should be."
    16  	verboseOptionDesc    = "Show the debug-level message"
    17  )
    18  
    19  func serverCmd(args []string) error {
    20  	commandLine := flag.NewFlagSet("", flag.ExitOnError)
    21  	commandLine.Usage = func() {
    22  		fmt.Fprintf(commandLine.Output(), `Usage:
    23  
    24    %s server [flags] [hostname:port]
    25  
    26  Flags:
    27  `, os.Args[0])
    28  		commandLine.PrintDefaults()
    29  	}
    30  	verbose := commandLine.Bool("verbose", false, verboseOptionDesc)
    31  
    32  	commandLine.Parse(args)
    33  	if commandLine.NArg() < 1 {
    34  		commandLine.Usage()
    35  		os.Exit(1)
    36  	}
    37  	log.EnableDebugLog = *verbose
    38  
    39  	return service.Serve(commandLine.Arg(0))
    40  }
    41  
    42  func main() {
    43  	commandLine := flag.NewFlagSet("", flag.ExitOnError)
    44  	commandLine.Usage = func() {
    45  		fmt.Fprintf(commandLine.Output(), `tgo is the function tracer for Go programs.
    46  
    47  Usage:
    48  
    49    %s <command> [arguments]
    50  
    51  Commands:
    52  
    53    server   launches the server which offers tracing service. See https://godoc.org/github.com/ks888/tgo/service for the detail.
    54  
    55  Use "tgo <command> --help" for more information about a command.
    56  `, os.Args[0])
    57  		commandLine.PrintDefaults()
    58  	}
    59  
    60  	if len(os.Args) < 2 {
    61  		commandLine.Usage()
    62  		os.Exit(1)
    63  	}
    64  
    65  	var err error
    66  	switch os.Args[1] {
    67  	case "server":
    68  		err = serverCmd(os.Args[2:])
    69  	default:
    70  		commandLine.Usage()
    71  		os.Exit(1)
    72  	}
    73  	if err != nil {
    74  		fmt.Fprintln(os.Stderr, err)
    75  		os.Exit(1)
    76  	}
    77  }