-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcommand.tsx
More file actions
131 lines (118 loc) · 4.34 KB
/
command.tsx
File metadata and controls
131 lines (118 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { ConfigIO } from '../../../lib';
import { getErrorMessage } from '../../errors';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import { RemoveAllScreen, RemoveFlow } from '../../tui/screens/remove';
import type { RemoveAllOptions, RemoveResult } from './types';
import { validateRemoveAllOptions } from './validate';
import type { Command } from '@commander-js/extra-typings';
import { Text, render } from 'ink';
import React from 'react';
async function handleRemoveAll(_options: RemoveAllOptions): Promise<RemoveResult> {
try {
const configIO = new ConfigIO();
// Get current project name to preserve it
let projectName = 'Project';
try {
const current = await configIO.readProjectSpec();
projectName = current.name;
} catch {
// Use default if can't read
}
// Reset agentcore.json (keep project name, clear all resources including gateways)
await configIO.writeProjectSpec({
name: projectName,
version: 1,
agents: [],
memories: [],
credentials: [],
evaluators: [],
onlineEvalConfigs: [],
agentCoreGateways: [],
policyEngines: [],
});
// Preserve aws-targets.json and deployed-state.json so that
// a subsequent `agentcore deploy` can tear down existing stacks.
return {
success: true,
message: 'All schemas reset to empty state',
note: 'Your source code has not been modified. Run `agentcore deploy` to apply changes to AWS.',
};
} catch (err) {
return { success: false, error: getErrorMessage(err) };
}
}
async function handleRemoveAllCLI(options: RemoveAllOptions): Promise<void> {
validateRemoveAllOptions(options);
const result = await handleRemoveAll(options);
console.log(JSON.stringify(result));
process.exit(result.success ? 0 : 1);
}
export const registerRemove = (program: Command): Command => {
const removeCommand = program.command('remove').description(COMMAND_DESCRIPTIONS.remove);
// 'remove all' is a special command, not a primitive
removeCommand
.command('all')
.description('Reset all agentcore schemas to empty state')
.option('-y, --yes', 'Skip confirmation prompts [non-interactive]')
.option('--force', 'Skip confirmation prompts (alias for --yes) [non-interactive]')
.option('--dry-run', 'Show what would be reset without actually resetting [non-interactive]')
.option('--json', 'Output as JSON [non-interactive]')
.action(async (cliOptions: { yes?: boolean; force?: boolean; dryRun?: boolean; json?: boolean }) => {
try {
const skipConfirm = cliOptions.yes ?? cliOptions.force;
// Any flag triggers non-interactive CLI mode
if (skipConfirm || cliOptions.dryRun || cliOptions.json) {
await handleRemoveAllCLI({
force: skipConfirm,
dryRun: cliOptions.dryRun,
json: cliOptions.json,
});
} else {
const { unmount } = render(
<RemoveAllScreen
isInteractive={false}
onExit={() => {
unmount();
process.exit(0);
}}
/>
);
}
} catch (error) {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: getErrorMessage(error) }));
} else {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
}
process.exit(1);
}
});
// Resource subcommands (agent, memory, credential, gateway, mcp-tool) are registered
// via primitive.registerCommands() in cli.ts
// Catch-all for TUI fallback when no subcommand is specified.
// Commander matches named subcommands first, so this is safe even though
// primitive subcommands are registered after this point.
removeCommand
.argument('[subcommand]')
.action((subcommand: string | undefined, _options, cmd) => {
if (subcommand) {
console.error(`error: '${subcommand}' is not a valid subcommand.`);
cmd.outputHelp();
process.exit(1);
}
requireProject();
const { clear, unmount } = render(
<RemoveFlow
isInteractive={false}
onExit={() => {
clear();
unmount();
}}
/>
);
})
.showHelpAfterError()
.showSuggestionAfterError();
return removeCommand;
};