NewLicenseFormatter returns a new license formatter. The formatter will return a table and JSON output.
()
| 15 | // NewLicenseFormatter returns a new license formatter. |
| 16 | // The formatter will return a table and JSON output. |
| 17 | func NewLicenseFormatter() *cliui.OutputFormatter { |
| 18 | type tableLicense struct { |
| 19 | ID int32 `table:"id,default_sort"` |
| 20 | UUID uuid.UUID `table:"uuid" format:"uuid"` |
| 21 | UploadedAt time.Time `table:"uploaded at" format:"date-time"` |
| 22 | // Features is the formatted string for the license claims. |
| 23 | // Used for the table view. |
| 24 | Features string `table:"features"` |
| 25 | ExpiresAt time.Time `table:"expires at" format:"date-time"` |
| 26 | Trial bool `table:"trial"` |
| 27 | } |
| 28 | |
| 29 | return cliui.NewOutputFormatter( |
| 30 | cliui.ChangeFormatterData( |
| 31 | cliui.TableFormat([]tableLicense{}, []string{"ID", "UUID", "Expires At", "Uploaded At", "Features"}), |
| 32 | func(data any) (any, error) { |
| 33 | list, ok := data.([]codersdk.License) |
| 34 | if !ok { |
| 35 | return nil, xerrors.Errorf("invalid data type %T", data) |
| 36 | } |
| 37 | out := make([]tableLicense, 0, len(list)) |
| 38 | for _, lic := range list { |
| 39 | var formattedFeatures string |
| 40 | features, err := lic.FeaturesClaims() |
| 41 | if err != nil { |
| 42 | formattedFeatures = xerrors.Errorf("invalid license: %w", err).Error() |
| 43 | } else { |
| 44 | var strs []string |
| 45 | if lic.AllFeaturesClaim() { |
| 46 | // If all features are enabled, just include that |
| 47 | strs = append(strs, "all features") |
| 48 | } else { |
| 49 | for k, v := range features { |
| 50 | if v > 0 { |
| 51 | // Only include claims > 0 |
| 52 | strs = append(strs, fmt.Sprintf("%s=%v", k, v)) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | formattedFeatures = strings.Join(strs, ", ") |
| 57 | } |
| 58 | // If this returns an error, a zero time is returned. |
| 59 | exp, _ := lic.ExpiresAt() |
| 60 | |
| 61 | out = append(out, tableLicense{ |
| 62 | ID: lic.ID, |
| 63 | UUID: lic.UUID, |
| 64 | UploadedAt: lic.UploadedAt, |
| 65 | Features: formattedFeatures, |
| 66 | ExpiresAt: exp, |
| 67 | Trial: lic.Trial(), |
| 68 | }) |
| 69 | } |
| 70 | return out, nil |
| 71 | }), |
| 72 | cliui.ChangeFormatterData(cliui.JSONFormat(), func(data any) (any, error) { |
| 73 | list, ok := data.([]codersdk.License) |
| 74 | if !ok { |
no test coverage detected