-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Revision to version CLI output and add JSON support #8268
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this is an issue but there may be a tool/check in people's pipelines that parses the protocol from the 2nd line or
tail -n 1
or something where this may break. In which case we would suggest them use the json formatter from now on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably hold for v1.9 then. So merging to master and not backporting into release/1.8 is the way to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, thinking about it I am not sure whether we should consider this a breaking change. We provide no guarantees about the output.