forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.ts
More file actions
81 lines (71 loc) · 2.1 KB
/
Copy pathaction.ts
File metadata and controls
81 lines (71 loc) · 2.1 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
import {
ConfigIO,
ConfigNotFoundError,
ConfigParseError,
ConfigReadError,
ConfigValidationError,
NoProjectError,
findConfigRoot,
} from '../../../lib';
export interface ValidateOptions {
directory?: string;
}
export interface ValidateResult {
success: boolean;
error?: string;
}
/**
* Validates all AgentCore schema files in the project.
* Returns a binary success/fail result with an error message if validation fails.
*/
export async function handleValidate(options: ValidateOptions): Promise<ValidateResult> {
const baseDir = options.directory ?? process.cwd();
// Check if project exists
const configRoot = findConfigRoot(baseDir);
if (!configRoot) {
return {
success: false,
error: new NoProjectError().message,
};
}
const configIO = new ConfigIO({ baseDir: configRoot });
// Validate project spec (agentcore.json)
try {
await configIO.readProjectSpec();
} catch (err) {
return { success: false, error: formatError(err, 'agentcore.json') };
}
// Validate AWS targets (aws-targets.json)
try {
await configIO.readAWSDeploymentTargets();
} catch (err) {
return { success: false, error: formatError(err, 'aws-targets.json') };
}
// Validate deployed state if it exists (.cli/state.json)
if (configIO.configExists('state')) {
try {
await configIO.readDeployedState();
} catch (err) {
return { success: false, error: formatError(err, '.cli/state.json') };
}
}
return { success: true };
}
function formatError(err: unknown, fileName: string): string {
if (err instanceof ConfigValidationError) {
return err.message;
}
if (err instanceof ConfigParseError) {
return `Invalid JSON in ${fileName}: ${err.cause instanceof Error ? err.cause.message : String(err.cause)}`;
}
if (err instanceof ConfigReadError) {
return `Failed to read ${fileName}: ${err.cause instanceof Error ? err.cause.message : String(err.cause)}`;
}
if (err instanceof ConfigNotFoundError) {
return `Required file not found: ${fileName}`;
}
if (err instanceof Error) {
return err.message;
}
return String(err);
}