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
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ program.hook('preAction', async (thisCommand, actionCommand) => {
const versionCmd = defineCommand({
name: 'version',
description: 'Print the elastic CLI version',
handler: () => ({ version: VERSION })
handler: () => ({ version: VERSION }),
formatOutput: () => VERSION + '\n',
})
program.addCommand(versionCmd)

Expand Down
19 changes: 19 additions & 0 deletions src/config/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ function fieldOptions (): Array<{ long: string; type: 'string'; description: str
]
}

/** Appends any warnings from a handler result as `Warning: <msg>` lines. */
function appendWarnings (base: string, result: JsonValue): string {
const warnings = (result as Record<string, JsonValue>).warnings
if (!Array.isArray(warnings)) return base
return base + warnings.map((w) => `Warning: ${w}\n`).join('')
}

function buildContextGroup (): OpaqueCommandHandle {
const addCmd = defineCommand({
name: 'add',
Expand All @@ -516,6 +523,7 @@ function buildContextGroup (): OpaqueCommandHandle {
...fieldOptions(),
],
handler: async (parsed) => handleContextAdd(parsed),
formatOutput: (result) => appendWarnings(`Context '${(result as unknown as CommandSummary).context}' added.\n`, result),
})

const removeCmd = defineCommand({
Expand All @@ -527,6 +535,7 @@ function buildContextGroup (): OpaqueCommandHandle {
{ long: 'force', type: 'boolean', description: 'allow removing the current context' },
],
handler: async (parsed) => handleContextRemove(parsed),
formatOutput: (result) => appendWarnings(`Context '${(result as unknown as CommandSummary).context}' removed.\n`, result),
})

const editCmd = defineCommand({
Expand All @@ -539,13 +548,19 @@ function buildContextGroup (): OpaqueCommandHandle {
...fieldOptions(),
],
handler: async (parsed) => handleContextEdit(parsed),
formatOutput: (result) => appendWarnings(`Context '${(result as unknown as CommandSummary).context}' updated.\n`, result),
})

const listCmd = defineCommand({
name: 'list',
description: 'List all contexts defined in the config file',
options: [CONFIG_FILE_OPT],
handler: async (parsed) => handleContextList(parsed.options),
formatOutput: (result) => {
const r = result as { contexts: Array<{ name: string; current: boolean }> }
if (r.contexts.length === 0) return 'No contexts configured.\n'
return r.contexts.map((c) => (c.current ? `* ${c.name}` : ` ${c.name}`)).join('\n') + '\n'
},
})

return defineGroup({ name: 'context', description: 'Manage contexts in the elastic config file' }, listCmd, addCmd, editCmd, removeCmd)
Expand All @@ -558,13 +573,17 @@ function buildCurrentContextGroup (): OpaqueCommandHandle {
positionalArg: { name: 'name', description: 'context name', required: true },
options: [CONFIG_FILE_OPT],
handler: async (parsed) => handleCurrentContextSet(parsed),
formatOutput: (result) => appendWarnings(
`Switched to context '${(result as { current: string }).current}'.\n`, result
),
})

const getCmd = defineCommand({
name: 'get',
description: 'Print the current context name',
options: [CONFIG_FILE_OPT],
handler: async (parsed) => handleCurrentContextGet(parsed.options),
formatOutput: (result) => (result as { current: string }).current + '\n',
})

return defineGroup(
Expand Down
6 changes: 6 additions & 0 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export function renderText(value: JsonValue): string {
}
}

if (isFlatObject(value)) {
return Object.entries(value)
.map(([k, v]) => `${k}: ${v ?? ''}`)
.join('\n') + '\n'
}

return JSON.stringify(value, null, 2) + '\n'
}

Expand Down
8 changes: 4 additions & 4 deletions test/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1888,14 +1888,14 @@ describe('defineCommand', () => {
assert.deepEqual(JSON.parse(out), { ok: true, count: 3 })
})

it('factory writes handler return value as pretty-printed JSON in text mode', async () => {
it('factory writes flat object handler result as key:value lines in text mode', async () => {
const cmd = defineCommand({
name: 'status',
description: 'Get status',
handler: () => ({ ok: true }),
})
const out = await invokeUnderRoot(cmd, [], [])
assert.equal(out, JSON.stringify({ ok: true }, null, 2) + '\n')
assert.equal(out, 'ok: true\n')
})

it('factory handles async handler return value', async () => {
Expand Down Expand Up @@ -2151,14 +2151,14 @@ describe('text output rendering', () => {
assert.match(out, /[─├┤┼]/)
})

it('falls back to pretty-printed JSON for a plain object', async () => {
it('renders a flat object as key:value lines', async () => {
const cmd = defineCommand({
name: 'status',
description: 'Status',
handler: () => ({ ok: true, count: 3 }),
})
const out = await invokeText(cmd)
assert.equal(out, JSON.stringify({ ok: true, count: 3 }, null, 2) + '\n')
assert.equal(out, 'ok: true\ncount: 3\n')
})

it('falls back to pretty-printed JSON for nested arrays', async () => {
Expand Down
21 changes: 15 additions & 6 deletions test/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,22 @@ describe('renderText', () => {
})
})

describe('flat objects — key: value pairs', () => {
it('renders a flat object as key: value lines', () => {
assert.equal(renderText({ status: 'ok', count: 3 }), 'status: ok\ncount: 3\n')
})

it('renders null values as empty string', () => {
assert.equal(renderText({ name: 'foo', value: null }), 'name: foo\nvalue: \n')
})

it('renders a single-key flat object', () => {
assert.equal(renderText({ version: '1.2.3' }), 'version: 1.2.3\n')
})
})

describe('complex types — fall back to pretty JSON', () => {
it('renders a plain object as pretty-printed JSON', () => {
it('renders a nested object as pretty-printed JSON', () => {
const val = { key: 'value', nested: { x: 1 } }
assert.equal(renderText(val), JSON.stringify(val, null, 2) + '\n')
})
Expand All @@ -140,11 +154,6 @@ describe('renderText', () => {
const val = ['hello', { key: 1 }]
assert.equal(renderText(val as never), JSON.stringify(val, null, 2) + '\n')
})

it('renders a flat object (not an array) as pretty-printed JSON', () => {
const val = { status: 'ok', count: 3 }
assert.equal(renderText(val), JSON.stringify(val, null, 2) + '\n')
})
})
})

Expand Down
Loading