Skip to content
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
16 changes: 12 additions & 4 deletions src/commands/me.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import type { Command } from 'commander'
import { ApiClient } from '../lib/api'
import { printJson, runAction } from '../lib/output'
import type { WhoAmI } from '../lib/types'

// Renders the human-readable identity summary. Prefers the GitHub login over the
// bare numeric user id when the server resolved a gh_user, falling back to the id.
export function renderMe(me: WhoAmI): void {
console.log(`customer: ${me.customer_login} (${me.customer_id})`)
if (me.gh_user) console.log(`user: ${me.gh_user.login} (${me.user_id})`)
else if (me.user_id) console.log(`user: ${me.user_id}`)
if (me.api_key_id) console.log(`api key: ${me.api_key_id}`)
if (me.sandbox_id) console.log(`sandbox: ${me.sandbox_id}`)
}

export function registerMe(program: Command): void {
program
Expand All @@ -14,10 +25,7 @@ export function registerMe(program: Command): void {
printJson(me)
return
}
console.log(`customer: ${me.customer_login} (${me.customer_id})`)
if (me.user_id) console.log(`user: ${me.user_id}`)
if (me.api_key_id) console.log(`api key: ${me.api_key_id}`)
if (me.sandbox_id) console.log(`sandbox: ${me.sandbox_id}`)
renderMe(me)
})
})
}
10 changes: 10 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@

// ------------------------------- identity -------------------------------

// The GitHub user behind a user_id, when we have it cached. Loosely typed: the
// CLI only reads `login`; the rest of the GithubUser fields are passed through.
export interface GhUser {
id: number
login: string
name: string | null
[key: string]: unknown
}

export interface WhoAmI {
customer_id: string
customer_login: string
user_id: string | null
gh_user: GhUser | null
api_key_id: string | null
sandbox_id: string | null
}
Expand Down
47 changes: 47 additions & 0 deletions test/me.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { renderMe } from '../src/commands/me'
import type { WhoAmI } from '../src/lib/types'

function base(overrides: Partial<WhoAmI> = {}): WhoAmI {
return {
customer_id: 'cust_1',
customer_login: 'ellipsis-dev',
user_id: null,
gh_user: null,
api_key_id: null,
sandbox_id: null,
...overrides,
}
}

describe('renderMe', () => {
let lines: string[]
beforeEach(() => {
lines = []
vi.spyOn(console, 'log').mockImplementation((msg?: unknown) => {
lines.push(String(msg))
})
})
afterEach(() => vi.restoreAllMocks())

it('shows the gh_user login alongside the id when resolved', () => {
renderMe(
base({
user_id: '24214708',
gh_user: { id: 24214708, login: 'hbrooks', name: 'Hunter' },
}),
)
expect(lines).toContain('user: hbrooks (24214708)')
})

it('falls back to the bare user id when gh_user is null', () => {
renderMe(base({ user_id: '24214708', gh_user: null }))
expect(lines).toContain('user: 24214708')
})

it('omits the user line entirely for api-key principals', () => {
renderMe(base({ api_key_id: 'eak_123' }))
expect(lines.some((l) => l.startsWith('user:'))).toBe(false)
expect(lines).toContain('api key: eak_123')
})
})
Loading