1
1
package version
2
2
3
3
import (
4
+ "flag"
4
5
"fmt"
6
+ "strings"
5
7
6
8
"github.com/hashicorp/consul/agent/consul"
9
+ "github.com/hashicorp/consul/command/flags"
10
+ "github.com/hashicorp/consul/version"
7
11
"github.com/mitchellh/cli"
8
12
)
9
13
10
- func New (ui cli.Ui , version string ) * cmd {
11
- return & cmd {UI : ui , version : version }
14
+ func New (ui cli.Ui ) * cmd {
15
+ c := & cmd {UI : ui }
16
+ c .init ()
17
+ return c
12
18
}
13
19
14
20
type cmd struct {
15
- UI cli.Ui
16
- version string
21
+ UI cli.Ui
22
+ flags * flag.FlagSet
23
+ format string
24
+ help string
17
25
}
18
26
19
- func (c * cmd ) Run (_ []string ) int {
20
- c .UI .Output (fmt .Sprintf ("Consul %s" , c .version ))
27
+ func (c * cmd ) init () {
28
+ c .flags = flag .NewFlagSet ("" , flag .ContinueOnError )
29
+ c .flags .StringVar (
30
+ & c .format ,
31
+ "format" ,
32
+ PrettyFormat ,
33
+ fmt .Sprintf ("Output format {%s}" , strings .Join (GetSupportedFormats (), "|" )))
34
+ c .help = flags .Usage (help , c .flags )
21
35
22
- const rpcProtocol = consul .DefaultRPCProtocol
36
+ }
37
+
38
+ type RPCVersionInfo struct {
39
+ Default int
40
+ Min int
41
+ Max int
42
+ }
43
+
44
+ type VersionInfo struct {
45
+ HumanVersion string `json:"-"`
46
+ Version string
47
+ Revision string
48
+ Prerelease string
49
+ RPC RPCVersionInfo
50
+ }
23
51
24
- var supplement string
25
- if rpcProtocol < consul .ProtocolVersionMax {
26
- supplement = fmt .Sprintf (" (agent will automatically use protocol >%d when speaking to compatible agents)" ,
27
- rpcProtocol )
52
+ func (c * cmd ) Run (args []string ) int {
53
+ if err := c .flags .Parse (args ); err != nil {
54
+ return 1
28
55
}
29
- c .UI .Output (fmt .Sprintf ("Protocol %d spoken by default, understands %d to %d%s" ,
30
- rpcProtocol , consul .ProtocolVersionMin , consul .ProtocolVersionMax , supplement ))
31
56
57
+ formatter , err := NewFormatter (c .format )
58
+ if err != nil {
59
+ c .UI .Error (err .Error ())
60
+ return 1
61
+ }
62
+ out , err := formatter .Format (& VersionInfo {
63
+ HumanVersion : version .GetHumanVersion (),
64
+ Version : version .Version ,
65
+ Revision : version .GitCommit ,
66
+ Prerelease : version .VersionPrerelease ,
67
+ RPC : RPCVersionInfo {
68
+ Default : consul .DefaultRPCProtocol ,
69
+ Min : int (consul .ProtocolVersionMin ),
70
+ Max : consul .ProtocolVersionMax ,
71
+ },
72
+ })
73
+ if err != nil {
74
+ c .UI .Error (err .Error ())
75
+ return 1
76
+ }
77
+ c .UI .Output (out )
32
78
return 0
33
79
}
34
80
@@ -37,5 +83,10 @@ func (c *cmd) Synopsis() string {
37
83
}
38
84
39
85
func (c * cmd ) Help () string {
40
- return ""
86
+ return flags . Usage ( c . help , nil )
41
87
}
88
+
89
+ const synopsis = "Output Consul version information"
90
+ const help = `
91
+ Usage: consul version [options]
92
+ `
0 commit comments