-
Notifications
You must be signed in to change notification settings - Fork 41
feat: add config command with dot-notation support #1387
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
Open
Hweinstock
wants to merge
4
commits into
aws:main
Choose a base branch
from
Hweinstock:feat/config-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f7cc37
feat: instrument config command
Hweinstock d454788
fix: simplify set path by validing the partial instead of the merged
Hweinstock 6c18be6
refactor: unify parsing logic with telemetry for common abstraction
Hweinstock 4d3cef1
fix: adjust tests to include quotes for strings
Hweinstock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { spawnAndCollect } from '../src/test-utils/cli-runner.js'; | ||
| import { mkdtempSync, readFileSync } from 'node:fs'; | ||
| import { rm } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { afterAll, describe, expect, it } from 'vitest'; | ||
|
|
||
| const testConfigDir = mkdtempSync(join(tmpdir(), 'agentcore-config-integ-')); | ||
| const cliPath = join(__dirname, '..', 'dist', 'cli', 'index.mjs'); | ||
|
|
||
| function run(args: string[]) { | ||
| return spawnAndCollect('node', [cliPath, ...args], tmpdir(), { | ||
| AGENTCORE_SKIP_INSTALL: '1', | ||
| AGENTCORE_CONFIG_DIR: testConfigDir, | ||
| }); | ||
| } | ||
|
|
||
| function readConfig() { | ||
| return JSON.parse(readFileSync(join(testConfigDir, 'config.json'), 'utf-8')); | ||
| } | ||
|
|
||
| describe('config command', () => { | ||
| afterAll(() => rm(testConfigDir, { recursive: true, force: true })); | ||
|
|
||
| it('lists config with only installationId when fresh', async () => { | ||
| const result = await run(['config']); | ||
| expect(result.exitCode).toBe(0); | ||
| const parsed = JSON.parse(result.stdout); | ||
| expect(parsed.installationId).toBeDefined(); | ||
| }); | ||
|
|
||
| it('sets a string value', async () => { | ||
| const result = await run(['config', 'uvIndex', 'https://example.com']); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout).toContain('Set uvIndex = https://example.com'); | ||
| expect(readConfig().uvIndex).toBe('https://example.com'); | ||
| }); | ||
|
|
||
| it('gets a value', async () => { | ||
| const result = await run(['config', 'uvIndex']); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout.trim()).toBe('"https://example.com"'); | ||
| }); | ||
|
|
||
| it('sets a nested value with dot notation', async () => { | ||
| const result = await run(['config', 'telemetry.endpoint', 'https://metrics.example.com']); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(readConfig().telemetry.endpoint).toBe('https://metrics.example.com'); | ||
| }); | ||
|
|
||
| it('gets a nested value with dot notation', async () => { | ||
| const result = await run(['config', 'telemetry.endpoint']); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout.trim()).toBe('"https://metrics.example.com"'); | ||
| }); | ||
|
|
||
| it('gets an object value as JSON', async () => { | ||
| const result = await run(['config', 'telemetry']); | ||
| expect(result.exitCode).toBe(0); | ||
| const parsed = JSON.parse(result.stdout); | ||
| expect(parsed.endpoint).toBe('https://metrics.example.com'); | ||
| }); | ||
|
|
||
| it('sets a boolean value via JSON parsing', async () => { | ||
| const result = await run(['config', 'telemetry.enabled', 'true']); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(readConfig().telemetry.enabled).toBe(true); | ||
| }); | ||
|
|
||
| it('sets a numeric value via JSON parsing', async () => { | ||
| const result = await run(['config', 'transactionSearchIndexPercentage', '50']); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(readConfig().transactionSearchIndexPercentage).toBe(50); | ||
| }); | ||
|
|
||
| it('rejects invalid value for a typed key', async () => { | ||
| const result = await run(['config', 'telemetry.enabled', 'notabool']); | ||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stderr).toContain('Invalid value'); | ||
| }); | ||
|
|
||
| it('rejects unknown keys', async () => { | ||
| const result = await run(['config', 'foo.bar.baz', 'hello']); | ||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stderr).toContain('Invalid value'); | ||
| }); | ||
|
|
||
| it('returns error for unset key', async () => { | ||
| const result = await run(['config', 'disableTransactionSearch']); | ||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stderr).toContain('is not set'); | ||
| }); | ||
|
|
||
| it('lists all config after mutations', async () => { | ||
| const result = await run(['config']); | ||
| expect(result.exitCode).toBe(0); | ||
| const parsed = JSON.parse(result.stdout); | ||
| expect(parsed.uvIndex).toBe('https://example.com'); | ||
| expect(parsed.telemetry.endpoint).toBe('https://metrics.example.com'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { readGlobalConfig, updateGlobalConfig, validateGlobalConfig } from '../../../lib/schemas/io/global-config.js'; | ||
| import type { ConfigResult } from './types.js'; | ||
| import { ValidationError } from '@/lib/index.js'; | ||
|
|
||
| export async function handleConfigList(): Promise<ConfigResult> { | ||
| const config = await readGlobalConfig(); | ||
| return { success: true, message: JSON.stringify(config, null, 2) }; | ||
| } | ||
|
|
||
| export async function handleConfigGet(key: string): Promise<ConfigResult> { | ||
| const config = await readGlobalConfig(); | ||
| const value = getByPath(config, key); | ||
| if (value === undefined) { | ||
| return { success: false, error: new Error(`Key "${key}" is not set.`) }; | ||
| } | ||
| const message = JSON.stringify(value, null, 2); | ||
| return { success: true, message }; | ||
| } | ||
|
|
||
| export async function handleConfigSet(key: string, raw: string): Promise<ConfigResult> { | ||
| const value = parseValue(raw); | ||
| const partial = buildNestedObject(key, value); | ||
| const validation = validateGlobalConfig(partial); | ||
|
|
||
| if (!validation.success) { | ||
| return { success: false, error: new ValidationError(`Invalid value "${raw}" for key "${key}".`) }; | ||
| } | ||
|
|
||
| const ok = await updateGlobalConfig(partial); | ||
| if (!ok) { | ||
| return { success: false, error: new Error(`Could not write config.`) }; | ||
| } | ||
| return { success: true, message: `Set ${key} = ${raw}` }; | ||
| } | ||
|
|
||
| function parseValue(raw: string): unknown { | ||
| try { | ||
| return JSON.parse(raw); | ||
| } catch { | ||
| return raw; | ||
| } | ||
| } | ||
|
|
||
| function isRecord(val: unknown): val is Record<string, unknown> { | ||
| return typeof val === 'object' && val !== null && !Array.isArray(val); | ||
| } | ||
|
|
||
| function getByPath(obj: Record<string, unknown>, path: string): unknown { | ||
| let current: unknown = obj; | ||
| for (const part of path.split('.')) { | ||
| if (!isRecord(current)) return undefined; | ||
| current = current[part]; | ||
| } | ||
| return current; | ||
| } | ||
|
|
||
| function buildNestedObject(path: string, value: unknown): Record<string, unknown> { | ||
| const parts = path.split('.'); | ||
| const leaf = parts.pop(); | ||
| if (!leaf) return {}; | ||
| const result: Record<string, unknown> = {}; | ||
| const inner = parts.reduce<Record<string, unknown>>((acc, part) => { | ||
| const next: Record<string, unknown> = {}; | ||
| acc[part] = next; | ||
| return next; | ||
| }, result); | ||
| inner[leaf] = value; | ||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { COMMAND_DESCRIPTIONS } from '../../tui/copy.js'; | ||
| import { handleConfigGet, handleConfigList, handleConfigSet } from './actions.js'; | ||
| import type { ConfigResult } from './types.js'; | ||
| import type { Command } from '@commander-js/extra-typings'; | ||
|
|
||
| function resolveAction(key?: string, value?: string): () => Promise<ConfigResult> { | ||
| if (!key) return () => handleConfigList(); | ||
| if (value === undefined) return () => handleConfigGet(key); | ||
| return () => handleConfigSet(key, value); | ||
| } | ||
|
|
||
| function printResult(result: ConfigResult): void { | ||
| if (result.success) { | ||
| console.log(result.message); | ||
| } else { | ||
| console.error(result.error.message); | ||
| } | ||
| } | ||
|
|
||
| export function registerConfig(program: Command) { | ||
| program | ||
| .command('config') | ||
| .description(COMMAND_DESCRIPTIONS.config) | ||
| .argument('[key]', 'Config key in dot notation (e.g. telemetry.enabled)') | ||
| .argument('[value]', 'Value to set') | ||
| .action(async (key?: string, value?: string) => { | ||
| const result = await resolveAction(key, value)(); | ||
| printResult(result); | ||
| if (!result.success) process.exit(1); | ||
| }); | ||
|
Hweinstock marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { registerConfig } from './command.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import type { Result } from '../../../lib/result.js'; | ||
|
|
||
| export type ConfigResult = Result<{ message: string }>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,7 @@ export const COMMAND_DESCRIPTIONS = { | |
| validate: 'Validate agentcore/ config files.', | ||
| 'config-bundle': '[preview] Manage configuration bundle versions and diffs.', | ||
| archive: '[preview] Archive (delete) a batch evaluation or recommendation on the service and clear local history.', | ||
| config: 'Adjust global configuration settings such as telemetry opt-out status', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should move this to another file, IMO its not intuitive that descriptions are in |
||
| } as const; | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.