-
Notifications
You must be signed in to change notification settings - Fork 894
feat(cli): add --output={text,json} to version cmd #7010
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af0508e
feat(cli): add --json output to version cmd
johnstcn c8c610e
fixup! feat(cli): add --json output to version cmd
johnstcn d3d5a69
fixup! feat(cli): add --json output to version cmd
johnstcn b9ada46
fixup! feat(cli): add --json output to version cmd
johnstcn caf2b46
fixup! feat(cli): add --json output to version cmd
johnstcn f218bcf
feat(cliui): add TextFormat
johnstcn 1f403a2
use --output specifier instead
johnstcn a7ec68d
fixup! use --output specifier instead
johnstcn 747e63f
gen
johnstcn b099e5c
strings.ReplaceAll
johnstcn 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
Next
Next commit
feat(cli): add --json output to version cmd
- Loading branch information
commit af0508e511032a4c173154676cb68a87b9037b2f
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,79 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/coder/coder/buildinfo" | ||
"github.com/coder/coder/cli/clibase" | ||
"github.com/coder/coder/cli/cliui" | ||
) | ||
|
||
// version prints the coder version | ||
func (*RootCmd) version() *clibase.Cmd { | ||
handleHuman := func(inv *clibase.Invocation) error { | ||
var str strings.Builder | ||
_, _ = str.WriteString("Coder ") | ||
if buildinfo.IsAGPL() { | ||
_, _ = str.WriteString("(AGPL) ") | ||
} | ||
_, _ = str.WriteString(buildinfo.Version()) | ||
buildTime, valid := buildinfo.Time() | ||
if valid { | ||
_, _ = str.WriteString(" " + buildTime.Format(time.UnixDate)) | ||
} | ||
_, _ = str.WriteString("\r\n" + buildinfo.ExternalURL() + "\r\n\r\n") | ||
|
||
if buildinfo.IsSlim() { | ||
_, _ = str.WriteString(fmt.Sprintf("Slim build of Coder, does not support the %s subcommand.\n", cliui.Styles.Code.Render("server"))) | ||
} else { | ||
_, _ = str.WriteString(fmt.Sprintf("Full build of Coder, supports the %s subcommand.\n", cliui.Styles.Code.Render("server"))) | ||
} | ||
|
||
_, _ = fmt.Fprint(inv.Stdout, str.String()) | ||
return nil | ||
} | ||
|
||
handleJSON := func(inv *clibase.Invocation) error { | ||
buildTime, _ := buildinfo.Time() | ||
versionInfo := struct { | ||
Version string `json:"version"` | ||
BuildTime string `json:"build_time"` | ||
ExternalURL string `json:"external_url"` | ||
Slim bool `json:"slim"` | ||
AGPL bool `json:"agpl"` | ||
}{ | ||
Version: buildinfo.Version(), | ||
BuildTime: buildTime.Format(time.UnixDate), | ||
ExternalURL: buildinfo.ExternalURL(), | ||
Slim: buildinfo.IsSlim(), | ||
AGPL: buildinfo.IsAGPL(), | ||
} | ||
|
||
enc := json.NewEncoder(inv.Stdout) | ||
enc.SetIndent("", " ") | ||
return enc.Encode(versionInfo) | ||
} | ||
|
||
var outputJSON bool | ||
|
||
return &clibase.Cmd{ | ||
Use: "version", | ||
Short: "Show coder version", | ||
Options: clibase.OptionSet{ | ||
{ | ||
Flag: "json", | ||
Description: "Emit version information in machine-readable JSON format.", | ||
Value: clibase.BoolOf(&outputJSON), | ||
}, | ||
}, | ||
Handler: func(inv *clibase.Invocation) error { | ||
if outputJSON { | ||
return handleJSON(inv) | ||
} | ||
return handleHuman(inv) | ||
}, | ||
} | ||
} |
This file contains hidden or 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,67 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/testutil" | ||
) | ||
|
||
func TestVersion(t *testing.T) { | ||
t.Parallel() | ||
ansiExpr := regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))") | ||
clean := func(s string) string { | ||
s = ansiExpr.ReplaceAllString(s, "") | ||
s = strings.Replace(s, "\r\n", "\n", -1) | ||
return s | ||
} | ||
expectedHuman := `Coder v0.0.0-devel | ||
https://github.com/coder/coder | ||
|
||
Full build of Coder, supports the server subcommand. | ||
` | ||
expectedJSON := `{ | ||
"version": "v0.0.0-devel", | ||
"build_time": "Mon Jan 1 00:00:00 UTC 0001", | ||
"external_url": "https://github.com/coder/coder", | ||
"slim": false, | ||
"agpl": false | ||
} | ||
` | ||
for _, tt := range []struct { | ||
Name string | ||
Args []string | ||
Expected string | ||
}{ | ||
{ | ||
Name: "Defaults to human-readable output", | ||
Args: []string{"version"}, | ||
Expected: expectedHuman, | ||
}, | ||
{ | ||
Name: "JSON output", | ||
Args: []string{"version", "--json"}, | ||
Expected: expectedJSON, | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.Name, func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) | ||
t.Cleanup(cancel) | ||
inv, _ := clitest.New(t, tt.Args...) | ||
buf := new(bytes.Buffer) | ||
inv.Stdout = buf | ||
err := inv.WithContext(ctx).Run() | ||
require.NoError(t, err) | ||
actual := clean(buf.String()) | ||
require.Equal(t, tt.Expected, actual) | ||
}) | ||
} | ||
} |
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.
nit: do you need a string builder if there is no way to break execution in the middle of processing?
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.
Not sure, but this is just the previous code I copy-pasta'ed.