github.com/v2fly/v2ray-core/v4@v4.45.2/infra/control/main/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/v2fly/v2ray-core/v4/common/log"
     9  	_ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/memconservative"
    10  	_ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
    11  	"github.com/v2fly/v2ray-core/v4/infra/control"
    12  )
    13  
    14  func getCommandName() string {
    15  	if len(os.Args) > 1 {
    16  		return os.Args[1]
    17  	}
    18  	return ""
    19  }
    20  
    21  func main() {
    22  	// let the v2ctl prints log at stderr
    23  	log.RegisterHandler(log.NewLogger(log.CreateStderrLogWriter()))
    24  	name := getCommandName()
    25  	cmd := control.GetCommand(name)
    26  	if cmd == nil {
    27  		fmt.Fprintln(os.Stderr, "Unknown command:", name)
    28  		fmt.Fprintln(os.Stderr)
    29  
    30  		fmt.Println("v2ctl <command>")
    31  		fmt.Println("Available commands:")
    32  		control.PrintUsage()
    33  		return
    34  	}
    35  
    36  	if err := cmd.Execute(os.Args[2:]); err != nil {
    37  		hasError := false
    38  		if err != flag.ErrHelp {
    39  			fmt.Fprintln(os.Stderr, err.Error())
    40  			fmt.Fprintln(os.Stderr)
    41  			hasError = true
    42  		}
    43  
    44  		for _, line := range cmd.Description().Usage {
    45  			fmt.Println(line)
    46  		}
    47  
    48  		if hasError {
    49  			os.Exit(-1)
    50  		}
    51  	}
    52  }