Skip to content
Open
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
13 changes: 12 additions & 1 deletion cmd/api_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c APIKeysCmd) List(ctx context.Context, in APIKeysListInput) error {

// Only surface Deleted At when the filter can actually return deleted keys.
showDeletedAt := in.IncludeDeleted || in.Status == "deleted" || in.Status == "all"
header := []string{"ID", "Name", "Scope", "Project", "Masked Key", "Expires At", "Created At"}
header := []string{"ID", "Name", "Scope", "Project", "Masked Key", "Expires At", "Last Used At", "Created At"}
if showDeletedAt {
header = append(header, "Deleted At")
}
Expand All @@ -201,6 +201,7 @@ func (c APIKeysCmd) List(ctx context.Context, in APIKeysListInput) error {
formatAPIKeyProject(key),
key.MaskedKey,
formatAPIKeyExpiresAt(key),
formatAPIKeyLastUsedAt(key),
util.FormatLocal(key.CreatedAt),
}
if showDeletedAt {
Expand Down Expand Up @@ -356,6 +357,7 @@ func renderAPIKeyDetails(key *kernel.APIKey) {
{"Masked Key", key.MaskedKey},
{"Created By", formatAPIKeyCreator(*key)},
{"Expires At", formatAPIKeyExpiresAt(*key)},
{"Last Used At", formatAPIKeyLastUsedAt(*key)},
{"Created At", util.FormatLocal(key.CreatedAt)},
}
PrintTableNoPad(rows, true)
Expand Down Expand Up @@ -395,6 +397,13 @@ func formatAPIKeyExpiresAt(key kernel.APIKey) string {
return util.FormatLocal(key.ExpiresAt)
}

func formatAPIKeyLastUsedAt(key kernel.APIKey) string {
if !key.JSON.LastUsedAt.Valid() {
return "-"
}
return util.FormatLocal(key.LastUsedAt)
}

func getAPIKeysHandler(cmd *cobra.Command) APIKeysCmd {
client := getKernelClient(cmd)
return APIKeysCmd{apiKeys: &client.APIKeys}
Expand Down Expand Up @@ -497,13 +506,15 @@ var apiKeysCreateCmd = &cobra.Command{
var apiKeysListCmd = &cobra.Command{
Use: "list",
Short: "List API keys",
Long: "List API keys. Last-used timestamps are accurate to within about 20 minutes; '-' means no use has been recorded since tracking began.",
Args: cobra.NoArgs,
RunE: runAPIKeysList,
}

var apiKeysGetCmd = &cobra.Command{
Use: "get <id>",
Short: "Get an API key",
Long: "Get an API key. The last-used timestamp is accurate to within about 20 minutes; '-' means no use has been recorded since tracking began.",
Args: cobra.ExactArgs(1),
RunE: runAPIKeysGet,
}
Expand Down
17 changes: 14 additions & 3 deletions cmd/api_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"testing"

"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
Expand Down Expand Up @@ -183,7 +184,7 @@ func TestAPIKeysRejectInvalidOutputBeforeCallingAPI(t *testing.T) {

func TestAPIKeysListPassesPaginationAndRendersRows(t *testing.T) {
buf := capturePtermOutput(t)
key := *apiKeyFromJSON(`{"id":"key_123","name":"ci","masked_key":"sk_...123","created_at":"2026-05-27T12:00:00Z","created_by":{"id":"user_123","email":"dev@example.com","name":"Dev"},"expires_at":null,"project_id":null,"project_name":null}`)
key := *apiKeyFromJSON(`{"id":"key_123","name":"ci","masked_key":"sk_...123","created_at":"2026-05-27T12:00:00Z","created_by":{"id":"user_123","email":"dev@example.com","name":"Dev"},"expires_at":null,"last_used_at":"2026-05-28T13:30:00Z","project_id":null,"project_name":null}`)
fake := &FakeAPIKeysService{
ListFunc: func(ctx context.Context, query kernel.APIKeyListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.APIKey], error) {
assert.True(t, query.Limit.Valid())
Expand All @@ -202,6 +203,13 @@ func TestAPIKeysListPassesPaginationAndRendersRows(t *testing.T) {
assert.Contains(t, out, "key_123")
assert.Contains(t, out, "ci")
assert.Contains(t, out, "Never")
assert.Contains(t, out, "Last Used At")
assert.Contains(t, out, util.FormatLocal(key.LastUsedAt))
}

func TestFormatAPIKeyLastUsedAtHandlesUnrecordedUse(t *testing.T) {
key := apiKeyFromJSON(`{"last_used_at":null}`)
assert.Equal(t, "-", formatAPIKeyLastUsedAt(*key))
}

func TestAPIKeysUpdateRequiresName(t *testing.T) {
Expand Down Expand Up @@ -338,17 +346,20 @@ func TestAPIKeysRotateOmitsUnsetParams(t *testing.T) {
}

func TestAPIKeysGetPassesIncludeDeleted(t *testing.T) {
capturePtermOutput(t)
buf := capturePtermOutput(t)
key := apiKeyFromJSON(`{"id":"key_123","name":"default","masked_key":"sk_...test","created_at":"2026-05-27T12:00:00Z","created_by":{"id":"user_123","email":"dev@example.com","name":"Dev"},"expires_at":null,"last_used_at":"2026-05-28T13:30:00Z","project_id":null,"project_name":null}`)
var captured kernel.APIKeyGetParams
fake := &FakeAPIKeysService{
GetFunc: func(ctx context.Context, id string, query kernel.APIKeyGetParams, opts ...option.RequestOption) (*kernel.APIKey, error) {
captured = query
return apiKeyFromJSON(`{"id":"` + id + `","name":"default","masked_key":"sk_...test","created_at":"2026-05-27T12:00:00Z","created_by":{"id":"user_123","email":"dev@example.com","name":"Dev"},"expires_at":null,"project_id":null,"project_name":null}`), nil
return key, nil
},
}
c := APIKeysCmd{apiKeys: fake}

require.NoError(t, c.Get(context.Background(), APIKeysGetInput{ID: "key_123", IncludeDeleted: true}))
assert.True(t, captured.IncludeDeleted.Valid())
assert.True(t, captured.IncludeDeleted.Value)
assert.Contains(t, buf.String(), "Last Used At")
assert.Contains(t, buf.String(), util.FormatLocal(key.LastUsedAt))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/joho/godotenv v1.5.1
github.com/kernel/kernel-go-sdk v0.131.0
github.com/kernel/kernel-go-sdk v0.132.0
github.com/klauspost/compress v1.18.5
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pterm/pterm v0.12.80
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kernel/kernel-go-sdk v0.131.0 h1:9aYi3O5N8g/cNwAIevmL+lOy8yy6daz5EXciCPipLco=
github.com/kernel/kernel-go-sdk v0.131.0/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
github.com/kernel/kernel-go-sdk v0.132.0 h1:LWwIuUqQ2fYu0oLUE9a1G7Yi0Id1Yb32bVIFBdIqQV0=
github.com/kernel/kernel-go-sdk v0.132.0/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE=
github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
Loading