github.com/slackhq/nebula@v1.9.0/cmd/nebula-cert/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "io" 7 "os" 8 ) 9 10 var Build string 11 12 type helpError struct { 13 s string 14 } 15 16 func (he *helpError) Error() string { 17 return he.s 18 } 19 20 func newHelpErrorf(s string, v ...interface{}) error { 21 return &helpError{s: fmt.Sprintf(s, v...)} 22 } 23 24 func main() { 25 flag.Usage = func() { 26 help("", os.Stderr) 27 os.Exit(1) 28 } 29 30 printVersion := flag.Bool("version", false, "Print version") 31 flagHelp := flag.Bool("help", false, "Print command line usage") 32 flagH := flag.Bool("h", false, "Print command line usage") 33 printUsage := false 34 35 flag.Parse() 36 37 if *flagH || *flagHelp { 38 printUsage = true 39 } 40 41 args := flag.Args() 42 43 if *printVersion { 44 fmt.Printf("Version: %v\n", Build) 45 os.Exit(0) 46 } 47 48 if len(args) < 1 { 49 if printUsage { 50 help("", os.Stderr) 51 os.Exit(0) 52 } 53 54 help("No mode was provided", os.Stderr) 55 os.Exit(1) 56 } else if printUsage { 57 handleError(args[0], &helpError{}, os.Stderr) 58 os.Exit(0) 59 } 60 61 var err error 62 63 switch args[0] { 64 case "ca": 65 err = ca(args[1:], os.Stdout, os.Stderr, StdinPasswordReader{}) 66 case "keygen": 67 err = keygen(args[1:], os.Stdout, os.Stderr) 68 case "sign": 69 err = signCert(args[1:], os.Stdout, os.Stderr, StdinPasswordReader{}) 70 case "print": 71 err = printCert(args[1:], os.Stdout, os.Stderr) 72 case "verify": 73 err = verify(args[1:], os.Stdout, os.Stderr) 74 default: 75 err = fmt.Errorf("unknown mode: %s", args[0]) 76 } 77 78 if err != nil { 79 os.Exit(handleError(args[0], err, os.Stderr)) 80 } 81 } 82 83 func handleError(mode string, e error, out io.Writer) int { 84 code := 1 85 86 // Handle -help, -h flags properly 87 if e == flag.ErrHelp { 88 code = 0 89 e = &helpError{} 90 } else if e != nil && e.Error() != "" { 91 fmt.Fprintln(out, "Error:", e) 92 } 93 94 switch e.(type) { 95 case *helpError: 96 switch mode { 97 case "ca": 98 caHelp(out) 99 case "keygen": 100 keygenHelp(out) 101 case "sign": 102 signHelp(out) 103 case "print": 104 printHelp(out) 105 case "verify": 106 verifyHelp(out) 107 } 108 } 109 110 return code 111 } 112 113 func help(err string, out io.Writer) { 114 if err != "" { 115 fmt.Fprintln(out, "Error:", err) 116 fmt.Fprintln(out, "") 117 } 118 119 fmt.Fprintf(out, "Usage of %s <global flags> <mode>:\n", os.Args[0]) 120 fmt.Fprintln(out, " Global flags:") 121 fmt.Fprintln(out, " -version: Prints the version") 122 fmt.Fprintln(out, " -h, -help: Prints this help message") 123 fmt.Fprintln(out, "") 124 fmt.Fprintln(out, " Modes:") 125 fmt.Fprintln(out, " "+caSummary()) 126 fmt.Fprintln(out, " "+keygenSummary()) 127 fmt.Fprintln(out, " "+signSummary()) 128 fmt.Fprintln(out, " "+printSummary()) 129 fmt.Fprintln(out, " "+verifySummary()) 130 fmt.Fprintln(out, "") 131 fmt.Fprintf(out, " To see usage for a given mode, use %s <mode> -h\n", os.Args[0]) 132 } 133 134 func mustFlagString(name string, val *string) error { 135 if *val == "" { 136 return newHelpErrorf("-%s is required", name) 137 } 138 return nil 139 }