Skip to content

feat(licenses): show license_expires time as rfc3339 date #7687

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions enterprise/cli/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"golang.org/x/xerrors"

Expand Down Expand Up @@ -154,6 +155,14 @@ func (r *RootCmd) licensesList() *clibase.Cmd {
licenses = make([]codersdk.License, 0)
}

for i, license := range licenses {
newClaims, err := convertLicenseExpireTime(license.Claims)
if err != nil {
return err
}
licenses[i].Claims = newClaims
}

enc := json.NewEncoder(inv.Stdout)
enc.SetIndent("", " ")
return enc.Encode(licenses)
Expand Down Expand Up @@ -187,3 +196,29 @@ func (r *RootCmd) licenseDelete() *clibase.Cmd {
}
return cmd
}

func convertLicenseExpireTime(licenseClaims map[string]interface{}) (map[string]interface{}, error) {
if licenseClaims["license_expires"] != nil {
licenseExpiresNumber, ok := licenseClaims["license_expires"].(json.Number)
if !ok {
return licenseClaims, xerrors.Errorf("could not convert license_expires to json.Number")
}

licenseExpires, err := licenseExpiresNumber.Int64()
if err != nil {
return licenseClaims, xerrors.Errorf("could not convert license_expires to int64: %w", err)
}

t := time.Unix(licenseExpires, 0)
rfc3339Format := t.Format(time.RFC3339)

claimsCopy := make(map[string]interface{}, len(licenseClaims))
for k, v := range licenseClaims {
claimsCopy[k] = v
}

claimsCopy["license_expires"] = rfc3339Format
return claimsCopy, nil
}
return licenseClaims, nil
}
4 changes: 3 additions & 1 deletion enterprise/cli/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestLicensesListFake(t *testing.T) {
assert.Equal(t, "claim1", licenses[0].Claims["h1"])
assert.Equal(t, int32(5), licenses[1].ID)
assert.Equal(t, "claim2", licenses[1].Claims["h2"])
assert.Equal(t, "2024-04-06T16:53:35Z", licenses[0].Claims["license_expires"])
})
}

Expand Down Expand Up @@ -294,7 +295,8 @@ func (s *fakeLicenseAPI) licenses(rw http.ResponseWriter, _ *http.Request) {
ID: 1,
UploadedAt: time.Now(),
Claims: map[string]interface{}{
"h1": "claim1",
"license_expires": 1712422415,
"h1": "claim1",
"features": map[string]int64{
"f1": 1,
"f2": 2,
Expand Down