-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathversion_command.go
68 lines (52 loc) · 1.25 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
package cmd
import (
"encoding/json"
"flag"
"fmt"
"strings"
"github.com/mitchellh/cli"
)
type VersionOutput struct {
Version string `json:"version"`
}
type VersionCommand struct {
Ui cli.Ui
Version string
jsonOutput bool
}
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
}
if c.jsonOutput {
output := VersionOutput{
Version: c.Version,
}
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
}
c.Ui.Output(string(c.Version))
return 0
}
func (c *VersionCommand) Help() string {
helpText := `
Usage: terraform-ls version [-json]
` + c.Synopsis() + "\n\n" + helpForFlags(c.flags())
return strings.TrimSpace(helpText)
}
func (c *VersionCommand) Synopsis() string {
return "Displays the version of the language server"
}