-
Notifications
You must be signed in to change notification settings - Fork 888
coder features list CLI command #3533
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1c536ef
AGPL Entitlements API
spikecurtis 9785907
Generate typesGenerated.ts
spikecurtis 1f07ceb
AllFeatures -> FeatureNames
spikecurtis 57095bd
Features CLI command
spikecurtis 711addc
Validate columns
spikecurtis 23f4341
Tests for features list CLI command
spikecurtis 82bb3f9
Merge branch 'main' of github.com:coder/coder into spike/3278_entitle…
spikecurtis 393ba09
Drop empty EntitlementsRequest
spikecurtis ead4b6c
Fix dump.sql generation
spikecurtis fe531e2
Merge branch 'main' of github.com:coder/coder into spike/3278_entitle…
spikecurtis 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 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,112 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
var featureColumns = []string{"Name", "Entitlement", "Enabled", "Limit", "Actual"} | ||
|
||
func features() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Short: "List features", | ||
Use: "features", | ||
Aliases: []string{"feature"}, | ||
} | ||
cmd.AddCommand( | ||
featuresList(), | ||
) | ||
return cmd | ||
} | ||
|
||
func featuresList() *cobra.Command { | ||
var ( | ||
columns []string | ||
outputFormat string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := cliui.ValidateColumns(featureColumns, columns) | ||
if err != nil { | ||
return err | ||
} | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
entitlements, err := client.Entitlements(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out := "" | ||
switch outputFormat { | ||
case "table", "": | ||
out = displayFeatures(columns, entitlements.Features) | ||
case "json": | ||
outBytes, err := json.Marshal(entitlements) | ||
if err != nil { | ||
return xerrors.Errorf("marshal users to JSON: %w", err) | ||
} | ||
|
||
out = string(outBytes) | ||
default: | ||
return xerrors.Errorf(`unknown output format %q, only "table" and "json" are supported`, outputFormat) | ||
} | ||
|
||
_, err = fmt.Fprintln(cmd.OutOrStdout(), out) | ||
return err | ||
}, | ||
} | ||
|
||
cmd.Flags().StringArrayVarP(&columns, "column", "c", featureColumns, | ||
fmt.Sprintf("Specify a column to filter in the table. Available columns are: %s", | ||
strings.Join(featureColumns, ", "))) | ||
cmd.Flags().StringVarP(&outputFormat, "output", "o", "table", "Output format. Available formats are: table, json.") | ||
return cmd | ||
} | ||
|
||
// displayFeatures will return a table displaying all features passed in. | ||
// filterColumns must be a subset of the feature fields and will determine which | ||
// columns to display | ||
func displayFeatures(filterColumns []string, features map[string]codersdk.Feature) string { | ||
tableWriter := cliui.Table() | ||
header := table.Row{} | ||
for _, h := range featureColumns { | ||
header = append(header, h) | ||
} | ||
tableWriter.AppendHeader(header) | ||
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns)) | ||
tableWriter.SortBy([]table.SortBy{{ | ||
Name: "username", | ||
}}) | ||
for name, feat := range features { | ||
tableWriter.AppendRow(table.Row{ | ||
name, | ||
feat.Entitlement, | ||
feat.Enabled, | ||
intOrNil(feat.Limit), | ||
intOrNil(feat.Actual), | ||
}) | ||
} | ||
return tableWriter.Render() | ||
} | ||
|
||
func intOrNil(i *int64) string { | ||
if i == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("%d", *i) | ||
} |
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" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/pty/ptytest" | ||
) | ||
|
||
func TestFeaturesList(t *testing.T) { | ||
t.Parallel() | ||
t.Run("Table", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
coderdtest.CreateFirstUser(t, client) | ||
cmd, root := clitest.New(t, "features", "list") | ||
clitest.SetupConfig(t, client, root) | ||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
errC := make(chan error) | ||
go func() { | ||
errC <- cmd.Execute() | ||
}() | ||
require.NoError(t, <-errC) | ||
pty.ExpectMatch("user_limit") | ||
pty.ExpectMatch("not_entitled") | ||
}) | ||
t.Run("JSON", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, nil) | ||
coderdtest.CreateFirstUser(t, client) | ||
cmd, root := clitest.New(t, "features", "list", "-o", "json") | ||
clitest.SetupConfig(t, client, root) | ||
doneChan := make(chan struct{}) | ||
|
||
buf := bytes.NewBuffer(nil) | ||
cmd.SetOut(buf) | ||
go func() { | ||
defer close(doneChan) | ||
err := cmd.Execute() | ||
assert.NoError(t, err) | ||
}() | ||
|
||
<-doneChan | ||
|
||
var entitlements codersdk.Entitlements | ||
err := json.Unmarshal(buf.Bytes(), &entitlements) | ||
require.NoError(t, err, "unmarshal JSON output") | ||
assert.Len(t, entitlements.Features, 2) | ||
assert.Empty(t, entitlements.Warnings) | ||
assert.Equal(t, codersdk.EntitlementNotEntitled, | ||
entitlements.Features[codersdk.FeatureUserLimit].Entitlement) | ||
assert.Equal(t, codersdk.EntitlementNotEntitled, | ||
entitlements.Features[codersdk.FeatureAuditLog].Entitlement) | ||
assert.False(t, entitlements.HasLicense) | ||
}) | ||
} |
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
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.
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.
Possibly slightly weird is that when you do
-o json
you get the whole Entitlements struct, but when you do-o table
(or default output), you only get the features in the table.What is missing in the table output are the warnings, but our plan is to print license warnings after every CLI command, so when that is done you will still get them in the output.