forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: Introduce version JSON output (hashicorp#386)
- Loading branch information
1 parent
19fcbf2
commit 1176437
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters