/
root.go
111 lines (91 loc) · 3.33 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Defines the root command and global flags
package cmd
import (
"fmt"
"log"
"os"
"text/tabwriter"
"github.com/grpc-ecosystem/grpcdebug/cmd/config"
"github.com/grpc-ecosystem/grpcdebug/cmd/transport"
"github.com/grpc-ecosystem/grpcdebug/cmd/verbose"
"github.com/spf13/cobra"
)
var verboseFlag, timestampFlag bool
var address, security, credFile, serverNameOverride string
// The table formater
var w = tabwriter.NewWriter(os.Stdout, 10, 0, 3, ' ', 0)
var rootUsageTemplate = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
grpcdebug <target address> [flags] {{ .CommandPath | ChildCommandPath }} <command>{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "grpcdebug <target address> {{ .CommandPath | ChildCommandPath }} [command] --help" for more information about a command.{{end}}
`
var rootCmd = &cobra.Command{
Use: "grpcdebug",
Short: "grpcdebug is an gRPC service admin CLI",
}
func initConfig() {
if verboseFlag {
verbose.EnableDebugOutput()
}
c := config.GetServerConfig(address)
if credFile != "" {
c.CredentialFile = credFile
}
if serverNameOverride != "" {
c.ServerNameOverride = serverNameOverride
}
if security == "tls" {
c.Security = config.TypeTLS
if c.CredentialFile == "" {
rootCmd.Usage()
log.Fatalf("Please specify credential file under [tls] mode.")
}
} else if security != "insecure" {
rootCmd.Usage()
log.Fatalf("Unrecognized security mode: %v", security)
}
transport.Connect(c)
}
// ChildCommandPath used in template
func ChildCommandPath(path string) string {
if len(path) <= 10 {
return ""
}
return path[10:]
}
func init() {
cobra.AddTemplateFunc("ChildCommandPath", ChildCommandPath)
cobra.OnInitialize(initConfig)
rootCmd.SetUsageTemplate(rootUsageTemplate)
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Print verbose information for debugging")
rootCmd.PersistentFlags().BoolVarP(×tampFlag, "timestamp", "t", false, "Print timestamp as RFC3339 instead of human readable strings")
rootCmd.PersistentFlags().StringVar(&security, "security", "insecure", "Defines the type of credentials to use [tls, google-default, insecure]")
rootCmd.PersistentFlags().StringVar(&credFile, "credential_file", "", "Sets the path of the credential file; used in [tls] mode")
rootCmd.PersistentFlags().StringVar(&serverNameOverride, "server_name_override", "", "Overrides the peer server name if non empty; used in [tls] mode")
}
// Execute executes the root command.
func Execute() {
if len(os.Args) > 1 {
address = os.Args[1]
os.Args = os.Args[1:]
} else {
rootCmd.Usage()
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}