- Notifications
You must be signed in to change notification settings - Fork928
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
1c536ef
9785907
1f07ceb
57095bd
711addc
23f4341
82bb3f9
393ba09
ead4b6c
fe531e2
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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) | ||
} |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Possibly slightly weird is that when you do What is missing in the table output are the warnings, but our plan is to print license warnings afterevery CLI command, so when that is done you will still get them in the output. | ||
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) | ||
}) | ||
} |