()
| 31 | } |
| 32 | |
| 33 | func (r *RootCmd) featuresList() *serpent.Command { |
| 34 | var ( |
| 35 | featureColumns = []string{"name", "entitlement", "enabled", "limit", "actual"} |
| 36 | columns []string |
| 37 | outputFormat string |
| 38 | ) |
| 39 | |
| 40 | cmd := &serpent.Command{ |
| 41 | Use: "list", |
| 42 | Aliases: []string{"ls"}, |
| 43 | Middleware: serpent.Chain(), |
| 44 | Handler: func(inv *serpent.Invocation) error { |
| 45 | client, err := r.InitClient(inv) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | entitlements, err := client.Entitlements(inv.Context()) |
| 50 | var apiError *codersdk.Error |
| 51 | if errors.As(err, &apiError) && apiError.StatusCode() == http.StatusNotFound { |
| 52 | return xerrors.New("You are on the AGPL licensed version of Coder that does not have Enterprise functionality!") |
| 53 | } |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | // This uses custom formatting as the JSON output outputs an object |
| 59 | // as opposed to a list from the table output. |
| 60 | out := "" |
| 61 | switch outputFormat { |
| 62 | case "table", "": |
| 63 | out, err = displayFeatures(columns, entitlements.Features) |
| 64 | if err != nil { |
| 65 | return xerrors.Errorf("render table: %w", err) |
| 66 | } |
| 67 | case "json": |
| 68 | buf := new(bytes.Buffer) |
| 69 | enc := json.NewEncoder(buf) |
| 70 | enc.SetIndent("", " ") |
| 71 | err = enc.Encode(entitlements) |
| 72 | if err != nil { |
| 73 | return xerrors.Errorf("marshal features to JSON: %w", err) |
| 74 | } |
| 75 | out = buf.String() |
| 76 | default: |
| 77 | return xerrors.Errorf(`unknown output format %q, only "table" and "json" are supported`, outputFormat) |
| 78 | } |
| 79 | |
| 80 | _, err = fmt.Fprintln(inv.Stdout, out) |
| 81 | return err |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | cmd.Options = serpent.OptionSet{ |
| 86 | { |
| 87 | Flag: "column", |
| 88 | FlagShorthand: "c", |
| 89 | Description: "Specify columns to filter in the table.", |
| 90 | Default: strings.Join(featureColumns, ","), |
no test coverage detected