Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edfcd5b

Browse files
rodrimaiaammario
authored andcommitted
feat(cli): show license_expires as rfc3339 date instead of unix time (coder#7687)
* feat(licenses): show license_expires time as rfc3339 date * fix review comments
1 parent b99e081 commit edfcd5b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

enterprise/cli/licenses.go

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"regexp"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"golang.org/x/xerrors"
1314

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

158+
for i, license := range licenses {
159+
newClaims, err := convertLicenseExpireTime(license.Claims)
160+
if err != nil {
161+
return err
162+
}
163+
licenses[i].Claims = newClaims
164+
}
165+
157166
enc := json.NewEncoder(inv.Stdout)
158167
enc.SetIndent("", " ")
159168
return enc.Encode(licenses)
@@ -187,3 +196,29 @@ func (r *RootCmd) licenseDelete() *clibase.Cmd {
187196
}
188197
return cmd
189198
}
199+
200+
func convertLicenseExpireTime(licenseClaims map[string]interface{}) (map[string]interface{}, error) {
201+
if licenseClaims["license_expires"] != nil {
202+
licenseExpiresNumber, ok := licenseClaims["license_expires"].(json.Number)
203+
if !ok {
204+
return licenseClaims, xerrors.Errorf("could not convert license_expires to json.Number")
205+
}
206+
207+
licenseExpires, err := licenseExpiresNumber.Int64()
208+
if err != nil {
209+
return licenseClaims, xerrors.Errorf("could not convert license_expires to int64: %w", err)
210+
}
211+
212+
t := time.Unix(licenseExpires, 0)
213+
rfc3339Format := t.Format(time.RFC3339)
214+
215+
claimsCopy := make(map[string]interface{}, len(licenseClaims))
216+
for k, v := range licenseClaims {
217+
claimsCopy[k] = v
218+
}
219+
220+
claimsCopy["license_expires"] = rfc3339Format
221+
return claimsCopy, nil
222+
}
223+
return licenseClaims, nil
224+
}

enterprise/cli/licenses_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func TestLicensesListFake(t *testing.T) {
158158
assert.Equal(t, "claim1", licenses[0].Claims["h1"])
159159
assert.Equal(t, int32(5), licenses[1].ID)
160160
assert.Equal(t, "claim2", licenses[1].Claims["h2"])
161+
assert.Equal(t, "2024-04-06T16:53:35Z", licenses[0].Claims["license_expires"])
161162
})
162163
}
163164

@@ -294,7 +295,8 @@ func (s *fakeLicenseAPI) licenses(rw http.ResponseWriter, _ *http.Request) {
294295
ID: 1,
295296
UploadedAt: time.Now(),
296297
Claims: map[string]interface{}{
297-
"h1": "claim1",
298+
"license_expires": 1712422415,
299+
"h1": "claim1",
298300
"features": map[string]int64{
299301
"f1": 1,
300302
"f2": 2,

0 commit comments

Comments
 (0)