forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.go
More file actions
114 lines (102 loc) · 3.07 KB
/
features.go
File metadata and controls
114 lines (102 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cli
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
agpl "github.com/coder/coder/cli"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
func features() *cobra.Command {
cmd := &cobra.Command{
Short: "List Enterprise features",
Use: "features",
Aliases: []string{"feature"},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.AddCommand(
featuresList(),
)
return cmd
}
func featuresList() *cobra.Command {
var (
featureColumns = []string{"Name", "Entitlement", "Enabled", "Limit", "Actual"}
columns []string
outputFormat string
)
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
client, err := agpl.CreateClient(cmd)
if err != nil {
return err
}
entitlements, err := client.Entitlements(cmd.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!")
}
if err != nil {
return err
}
out := ""
switch outputFormat {
case "table", "":
out, err = displayFeatures(columns, entitlements.Features)
if err != nil {
return xerrors.Errorf("render table: %w", err)
}
case "json":
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
err = enc.Encode(entitlements)
if err != nil {
return xerrors.Errorf("marshal features to JSON: %w", err)
}
out = buf.String()
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
}
type featureRow struct {
Name codersdk.FeatureName `table:"name"`
Entitlement string `table:"entitlement"`
Enabled bool `table:"enabled"`
Limit *int64 `table:"limit"`
Actual *int64 `table:"actual"`
}
// 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) {
rows := make([]featureRow, 0, len(features))
for name, feat := range features {
rows = append(rows, featureRow{
Name: name,
Entitlement: string(feat.Entitlement),
Enabled: feat.Enabled,
Limit: feat.Limit,
Actual: feat.Actual,
})
}
return cliui.DisplayTable(rows, "name", filterColumns)
}