- Notifications
You must be signed in to change notification settings - Fork1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
af0508e
feat(cli): add --json output to version cmd
johnstcnc8c610e
fixup! feat(cli): add --json output to version cmd
johnstcnd3d5a69
fixup! feat(cli): add --json output to version cmd
johnstcnb9ada46
fixup! feat(cli): add --json output to version cmd
johnstcncaf2b46
fixup! feat(cli): add --json output to version cmd
johnstcnf218bcf
feat(cliui): add TextFormat
johnstcn1f403a2
use --output specifier instead
johnstcna7ec68d
fixup! use --output specifier instead
johnstcn747e63f
gen
johnstcnb099e5c
strings.ReplaceAll
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletionscli/cliui/output.go
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
3 changes: 3 additions & 0 deletionscli/cliui/output_test.go
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
32 changes: 1 addition & 31 deletionscli/root.go
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
6 changes: 5 additions & 1 deletioncli/testdata/coder_version_--help.golden
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
Usage: coder version [flags] | ||
Show coder version | ||
[1mOptions[0m | ||
-o, --output string (default: text) | ||
Output format. Available formats: text, json. | ||
--- | ||
Run `coder --help` for a list of global options. |
84 changes: 84 additions & 0 deletionscli/version.go
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,84 @@ | ||
package cli | ||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
"github.com/coder/coder/buildinfo" | ||
"github.com/coder/coder/cli/clibase" | ||
"github.com/coder/coder/cli/cliui" | ||
) | ||
// versionInfo wraps the stuff we get from buildinfo so that it's | ||
// easier to emit in different formats. | ||
type versionInfo struct { | ||
Version string `json:"version"` | ||
BuildTime time.Time `json:"build_time"` | ||
ExternalURL string `json:"external_url"` | ||
Slim bool `json:"slim"` | ||
AGPL bool `json:"agpl"` | ||
} | ||
// String() implements Stringer | ||
func (vi versionInfo) String() string { | ||
var str strings.Builder | ||
_, _ = str.WriteString("Coder ") | ||
if vi.AGPL { | ||
_, _ = str.WriteString("(AGPL) ") | ||
} | ||
_, _ = str.WriteString(vi.Version) | ||
if !vi.BuildTime.IsZero() { | ||
_, _ = str.WriteString(" " + vi.BuildTime.Format(time.UnixDate)) | ||
} | ||
_, _ = str.WriteString("\r\n" + vi.ExternalURL + "\r\n\r\n") | ||
if vi.Slim { | ||
_, _ = str.WriteString(fmt.Sprintf("Slim build of Coder, does not support the %s subcommand.", cliui.Styles.Code.Render("server"))) | ||
} else { | ||
_, _ = str.WriteString(fmt.Sprintf("Full build of Coder, supports the %s subcommand.", cliui.Styles.Code.Render("server"))) | ||
} | ||
return str.String() | ||
} | ||
func defaultVersionInfo() *versionInfo { | ||
buildTime, _ := buildinfo.Time() | ||
return &versionInfo{ | ||
Version: buildinfo.Version(), | ||
BuildTime: buildTime, | ||
ExternalURL: buildinfo.ExternalURL(), | ||
Slim: buildinfo.IsSlim(), | ||
AGPL: buildinfo.IsAGPL(), | ||
} | ||
} | ||
// version prints the coder version | ||
func (*RootCmd) version(versionInfo func() *versionInfo) *clibase.Cmd { | ||
var ( | ||
formatter = cliui.NewOutputFormatter( | ||
cliui.TextFormat(), | ||
cliui.JSONFormat(), | ||
) | ||
vi = versionInfo() | ||
) | ||
cmd := &clibase.Cmd{ | ||
Use: "version", | ||
Short: "Show coder version", | ||
Options: clibase.OptionSet{}, | ||
Handler: func(inv *clibase.Invocation) error { | ||
out, err := formatter.Format(inv.Context(), vi) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprintln(inv.Stdout, out) | ||
return err | ||
}, | ||
} | ||
formatter.AttachOptions(&cmd.Options) | ||
return cmd | ||
} |
66 changes: 66 additions & 0 deletionscli/version_test.go
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,66 @@ | ||
package cli_test | ||
import ( | ||
"bytes" | ||
"context" | ||
"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() | ||
expectedText := `Coder v0.0.0-devel | ||
https://github.com/coder/coder | ||
Full build of Coder, supports the [;mserver[0m subcommand. | ||
` | ||
expectedJSON := `{ | ||
"version": "v0.0.0-devel", | ||
"build_time": "0001-01-01T00:00:00Z", | ||
"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: expectedText, | ||
}, | ||
{ | ||
Name: "JSON output", | ||
Args: []string{"version", "--output=json"}, | ||
Expected: expectedJSON, | ||
}, | ||
{ | ||
Name: "Text output", | ||
Args: []string{"version", "--output=text"}, | ||
Expected: expectedText, | ||
}, | ||
} { | ||
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 := buf.String() | ||
actual = strings.ReplaceAll(actual, "\r\n", "\n") | ||
require.Equal(t, tt.Expected, actual) | ||
}) | ||
} | ||
} |
13 changes: 12 additions & 1 deletiondocs/cli/version.md
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
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.