-
Notifications
You must be signed in to change notification settings - Fork 8
/
version_command.go
85 lines (67 loc) · 1.73 KB
/
version_command.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
package cmd
import (
"encoding/json"
"flag"
"fmt"
"runtime"
"strings"
"github.com/mitchellh/cli"
)
type VersionOutput struct {
Version string `json:"version"`
*BuildInfo
}
type VersionCommand struct {
Ui cli.Ui
Version string
jsonOutput bool
}
type BuildInfo struct {
GoVersion string `json:"go,omitempty"`
GoOS string `json:"os,omitempty"`
GoArch string `json:"arch,omitempty"`
Compiler string `json:"compiler,omitempty"`
}
func (c *VersionCommand) flags() *flag.FlagSet {
fs := defaultFlagSet("version")
fs.BoolVar(&c.jsonOutput, "json", false, "output the version information as a JSON object")
fs.Usage = func() { c.Ui.Error(c.Help()) }
return fs
}
func (c *VersionCommand) Run(args []string) int {
f := c.flags()
if err := f.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s", err))
return 1
}
output := VersionOutput{
Version: c.Version,
BuildInfo: &BuildInfo{
GoVersion: runtime.Version(),
GoOS: runtime.GOOS,
GoArch: runtime.GOARCH,
Compiler: runtime.Compiler,
},
}
if c.jsonOutput {
jsonOutput, err := json.MarshalIndent(output, "", " ")
if err != nil {
c.Ui.Error(fmt.Sprintf("\nError marshalling JSON: %s", err))
return 1
}
c.Ui.Output(string(jsonOutput))
return 0
}
ver := fmt.Sprintf("%s\nplatform: %s/%s\ngo: %s\ncompiler: %s", c.Version, output.GoOS, output.GoArch, output.GoVersion, output.Compiler)
c.Ui.Output(ver)
return 0
}
func (c *VersionCommand) Help() string {
helpText := `
Usage: azapi2azurerm version [-json]
` + c.Synopsis() + "\n\n" + helpForFlags(c.flags())
return strings.TrimSpace(helpText)
}
func (c *VersionCommand) Synopsis() string {
return "Displays the version of the migration tool"
}