Files
coder/enterprise/cli/features.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.4 KiB
Go
Raw Normal View History

2022-08-17 11:26:16 -07:00
package cli
import (
"bytes"
2022-08-17 11:26:16 -07:00
"encoding/json"
"errors"
2022-08-17 11:26:16 -07:00
"fmt"
"net/http"
2022-08-17 11:26:16 -07:00
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
2022-08-17 11:26:16 -07:00
)
2024-03-17 09:45:26 -05:00
func (r *RootCmd) features() *serpent.Command {
cmd := &serpent.Command{
Short: "List Enterprise features",
2022-08-17 11:26:16 -07:00
Use: "features",
Aliases: []string{"feature"},
Handler: func(inv *serpent.Invocation) error {
2023-03-23 17:42:20 -05:00
return inv.Command.HelpHandler(inv)
},
2024-03-17 09:45:26 -05:00
Children: []*serpent.Command{
2023-03-23 17:42:20 -05:00
r.featuresList(),
2022-10-06 16:15:28 -04:00
},
2022-08-17 11:26:16 -07:00
}
return cmd
}
2024-03-17 09:45:26 -05:00
func (r *RootCmd) featuresList() *serpent.Command {
2022-08-17 11:26:16 -07:00
var (
2024-08-20 14:47:46 +10:00
featureColumns = []string{"name", "entitlement", "enabled", "limit", "actual"}
2022-08-31 20:09:44 -05:00
columns []string
outputFormat string
2022-08-17 11:26:16 -07:00
)
2023-03-23 17:42:20 -05:00
client := new(codersdk.Client)
2022-08-17 11:26:16 -07:00
2024-03-17 09:45:26 -05:00
cmd := &serpent.Command{
2022-08-17 11:26:16 -07:00
Use: "list",
Aliases: []string{"ls"},
Middleware: serpent.Chain(
2023-03-23 17:42:20 -05:00
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
2023-03-23 17:42:20 -05:00
entitlements, err := client.Entitlements(inv.Context())
var apiError *codersdk.Error
if errors.As(err, &apiError) && apiError.StatusCode() == http.StatusNotFound {
return xerrors.New("You are on the AGPL licensed version of Coder that does not have Enterprise functionality!")
}
2022-08-17 11:26:16 -07:00
if err != nil {
return err
}
// This uses custom formatting as the JSON output outputs an object
// as opposed to a list from the table output.
2022-08-17 11:26:16 -07:00
out := ""
switch outputFormat {
case "table", "":
2022-08-19 02:41:00 +10:00
out, err = displayFeatures(columns, entitlements.Features)
if err != nil {
return xerrors.Errorf("render table: %w", err)
}
2022-08-17 11:26:16 -07:00
case "json":
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
err = enc.Encode(entitlements)
2022-08-17 11:26:16 -07:00
if err != nil {
2022-08-19 02:41:00 +10:00
return xerrors.Errorf("marshal features to JSON: %w", err)
2022-08-17 11:26:16 -07:00
}
out = buf.String()
2022-08-17 11:26:16 -07:00
default:
return xerrors.Errorf(`unknown output format %q, only "table" and "json" are supported`, outputFormat)
}
2023-03-23 17:42:20 -05:00
_, err = fmt.Fprintln(inv.Stdout, out)
2022-08-17 11:26:16 -07:00
return err
},
}
cmd.Options = serpent.OptionSet{
2023-03-23 17:42:20 -05:00
{
Flag: "column",
FlagShorthand: "c",
2024-08-20 14:47:46 +10:00
Description: "Specify columns to filter in the table.",
Default: strings.Join(featureColumns, ","),
Value: serpent.EnumArrayOf(&columns, featureColumns...),
2023-03-23 17:42:20 -05:00
},
{
Flag: "output",
FlagShorthand: "o",
2024-08-20 14:47:46 +10:00
Description: "Output format.",
2023-03-23 17:42:20 -05:00
Default: "table",
2024-08-20 14:47:46 +10:00
Value: serpent.EnumOf(&outputFormat, "table", "json"),
2023-03-23 17:42:20 -05:00
},
}
2022-08-17 11:26:16 -07:00
return cmd
}
2022-08-19 02:41:00 +10:00
type featureRow struct {
Name codersdk.FeatureName `table:"name,default_sort"`
Entitlement string `table:"entitlement"`
Enabled bool `table:"enabled"`
Limit *int64 `table:"limit"`
Actual *int64 `table:"actual"`
2022-08-19 02:41:00 +10:00
}
2022-08-17 11:26:16 -07:00
// 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[codersdk.FeatureName]codersdk.Feature) (string, error) {
2022-08-19 02:41:00 +10:00
rows := make([]featureRow, 0, len(features))
2022-08-17 11:26:16 -07:00
for name, feat := range features {
2022-08-19 02:41:00 +10:00
rows = append(rows, featureRow{
Name: name,
Entitlement: string(feat.Entitlement),
Enabled: feat.Enabled,
Limit: feat.Limit,
Actual: feat.Actual,
2022-08-17 11:26:16 -07:00
})
}
2022-08-19 02:41:00 +10:00
return cliui.DisplayTable(rows, "name", filterColumns)
2022-08-17 11:26:16 -07:00
}