-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8268 from hashicorp/feature/improved-version-output
Add Revision to version CLI output and add JSON support
- Loading branch information
Showing
9 changed files
with
239 additions
and
19 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
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,69 @@ | ||
package version | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
PrettyFormat string = "pretty" | ||
JSONFormat string = "json" | ||
) | ||
|
||
type Formatter interface { | ||
Format(info *VersionInfo) (string, error) | ||
} | ||
|
||
func GetSupportedFormats() []string { | ||
return []string{PrettyFormat, JSONFormat} | ||
} | ||
|
||
func NewFormatter(format string) (Formatter, error) { | ||
switch format { | ||
case PrettyFormat: | ||
return newPrettyFormatter(), nil | ||
case JSONFormat: | ||
return newJSONFormatter(), nil | ||
default: | ||
return nil, fmt.Errorf("Unknown format: %s", format) | ||
} | ||
} | ||
|
||
type prettyFormatter struct{} | ||
|
||
func newPrettyFormatter() Formatter { | ||
return &prettyFormatter{} | ||
} | ||
|
||
func (_ *prettyFormatter) Format(info *VersionInfo) (string, error) { | ||
var buffer bytes.Buffer | ||
buffer.WriteString(fmt.Sprintf("Consul %s\n", info.HumanVersion)) | ||
if info.Revision != "" { | ||
buffer.WriteString(fmt.Sprintf("Revision %s\n", info.Revision)) | ||
} | ||
|
||
var supplement string | ||
if info.RPC.Default < info.RPC.Max { | ||
supplement = fmt.Sprintf(" (agent will automatically use protocol >%d when speaking to compatible agents)", | ||
info.RPC.Default) | ||
} | ||
buffer.WriteString(fmt.Sprintf("Protocol %d spoken by default, understands %d to %d%s\n", | ||
info.RPC.Default, info.RPC.Min, info.RPC.Max, supplement)) | ||
|
||
return buffer.String(), nil | ||
} | ||
|
||
type jsonFormatter struct{} | ||
|
||
func newJSONFormatter() Formatter { | ||
return &jsonFormatter{} | ||
} | ||
|
||
func (_ *jsonFormatter) Format(info *VersionInfo) (string, error) { | ||
b, err := json.MarshalIndent(info, "", " ") | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to marshal version info: %v", err) | ||
} | ||
return string(b), nil | ||
} |
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,63 @@ | ||
package version | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// update allows golden files to be updated based on the current output. | ||
var update = flag.Bool("update", false, "update golden files") | ||
|
||
// golden reads and optionally writes the expected data to the golden file, | ||
// returning the contents as a string. | ||
func golden(t *testing.T, name, got string) string { | ||
t.Helper() | ||
|
||
golden := filepath.Join("testdata", name+".golden") | ||
if *update && got != "" { | ||
err := ioutil.WriteFile(golden, []byte(got), 0644) | ||
require.NoError(t, err) | ||
} | ||
|
||
expected, err := ioutil.ReadFile(golden) | ||
require.NoError(t, err) | ||
|
||
return string(expected) | ||
} | ||
|
||
func TestFormat(t *testing.T) { | ||
info := VersionInfo{ | ||
HumanVersion: "1.99.3-beta1", | ||
Version: "1.99.3", | ||
Prerelease: "beta1", | ||
Revision: "5e5dbedd47a5f875b60e241c5555a9caab595246", | ||
RPC: RPCVersionInfo{ | ||
Default: 2, | ||
Min: 1, | ||
Max: 3, | ||
}, | ||
} | ||
|
||
formatters := map[string]Formatter{ | ||
"pretty": newPrettyFormatter(), | ||
// the JSON formatter ignores the showMeta | ||
"json": newJSONFormatter(), | ||
} | ||
|
||
for fmtName, formatter := range formatters { | ||
t.Run(fmtName, func(t *testing.T) { | ||
actual, err := formatter.Format(&info) | ||
require.NoError(t, err) | ||
|
||
gName := fmt.Sprintf("%s", fmtName) | ||
|
||
expected := golden(t, gName, actual) | ||
require.Equal(t, expected, actual) | ||
}) | ||
} | ||
} |
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,10 @@ | ||
{ | ||
"Version": "1.99.3", | ||
"Revision": "5e5dbedd47a5f875b60e241c5555a9caab595246", | ||
"Prerelease": "beta1", | ||
"RPC": { | ||
"Default": 2, | ||
"Min": 1, | ||
"Max": 3 | ||
} | ||
} |
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,3 @@ | ||
Consul 1.99.3-beta1 | ||
Revision 5e5dbedd47a5f875b60e241c5555a9caab595246 | ||
Protocol 2 spoken by default, understands 1 to 3 (agent will automatically use protocol >2 when speaking to compatible agents) |
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
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
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
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