This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
/
commands.go
99 lines (81 loc) · 2.72 KB
/
commands.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
package commands
import (
"fmt"
"io"
"os"
"github.com/spf13/cobra"
)
type Cmd struct {
root *cobra.Command
Err io.Writer
Out io.Writer
consul *consul
Template string
}
func Init(name, version string) *Cmd {
c := Cmd{
Err: os.Stderr,
Out: os.Stdout,
consul: &consul{
auth: new(auth),
},
}
c.root = &cobra.Command{
Use: "consul-cli",
Short: "Command line interface for Consul HTTP API",
Long: "Command line interface for Consul HTTP API",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
},
RunE: func(cmd *cobra.Command, args []string) error {
c.root.Help()
return nil
},
}
c.root.PersistentFlags().StringVar(&c.consul.address, "consul", "127.0.0.1:8500", "Consul address:port")
c.root.PersistentFlags().BoolVar(&c.consul.sslEnabled, "ssl", false, "Use HTTPS when talking to Consul")
c.root.PersistentFlags().BoolVar(&c.consul.sslVerify, "ssl-verify", true, "Verify certificates when connecting via SSL")
c.root.PersistentFlags().StringVar(&c.consul.sslCert, "ssl-cert", "", "Path to an SSL client certificate for authentication")
c.root.PersistentFlags().StringVar(&c.consul.sslKey, "ssl-key", "", "Path to an SSL client certificate key for authentication")
c.root.PersistentFlags().StringVar(&c.consul.sslCaCert, "ssl-ca-cert", "", "Path to a CA certificate file to validate the Consul server")
c.root.PersistentFlags().Var((*auth)(c.consul.auth), "auth", "The HTTP basic authentication username (and optional password) separated by a colon")
c.root.PersistentFlags().StringVar(&c.consul.token, "token", "", "The Consul ACL token")
c.root.PersistentFlags().StringVar(&c.consul.tokenFile, "token-file", "", "Path to file containing Consul ACL token")
c.initAcl()
c.initAgent()
c.initCatalog()
c.initCheck()
c.initCoordinate()
c.initHealth()
c.initKv()
c.initService()
c.initSession()
c.initStatus()
versionCmd := &cobra.Command{
Use: "version",
Short: "Print version information",
Long: "Print version information",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("%s %s\n", name, version)
return nil
},
}
c.root.AddCommand(versionCmd)
return &c
}
func (c *Cmd) Execute() error {
return c.root.Execute()
}
func (c *Cmd) Help() error {
return c.root.Help()
}
func (c *Cmd) AddCommand(cmd *cobra.Command) {
c.root.AddCommand(cmd)
}
func (c *Cmd) AddTemplateOption(cmd *cobra.Command) {
cmd.Flags().StringVar(&c.Template, "template", "", "Output template. Use @filename to read template from a file")
}
type funcVar func(s string) error
func (f funcVar) Set(s string) error { return f(s) }
func (f funcVar) String() string { return "" }
func (f funcVar) IsBoolFlag() bool { return false }
func (f funcVar) Type() string { return "funcVar" }