forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlifecycle-utils.ts
More file actions
50 lines (46 loc) · 1.71 KB
/
Copy pathlifecycle-utils.ts
File metadata and controls
50 lines (46 loc) · 1.71 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
import { LIFECYCLE_TIMEOUT_MAX, LIFECYCLE_TIMEOUT_MIN } from '../../../schema';
export interface LifecycleOptions {
idleTimeout?: number | string;
maxLifetime?: number | string;
}
export interface LifecycleValidationResult {
valid: boolean;
error?: string;
idleTimeout?: number;
maxLifetime?: number;
}
/**
* Parse and validate lifecycle CLI options.
* Coerces string values to numbers and validates range constraints.
* Returns the parsed numeric values in the result — does NOT mutate the input.
*/
export function parseAndValidateLifecycleOptions(options: LifecycleOptions): LifecycleValidationResult {
let idleTimeout: number | undefined;
let maxLifetime: number | undefined;
if (options.idleTimeout !== undefined) {
const val = Number(options.idleTimeout);
if (isNaN(val) || !Number.isInteger(val) || val < LIFECYCLE_TIMEOUT_MIN || val > LIFECYCLE_TIMEOUT_MAX) {
return {
valid: false,
error: `--idle-timeout must be an integer between ${LIFECYCLE_TIMEOUT_MIN} and ${LIFECYCLE_TIMEOUT_MAX} seconds`,
};
}
idleTimeout = val;
}
if (options.maxLifetime !== undefined) {
const val = Number(options.maxLifetime);
if (isNaN(val) || !Number.isInteger(val) || val < LIFECYCLE_TIMEOUT_MIN || val > LIFECYCLE_TIMEOUT_MAX) {
return {
valid: false,
error: `--max-lifetime must be an integer between ${LIFECYCLE_TIMEOUT_MIN} and ${LIFECYCLE_TIMEOUT_MAX} seconds`,
};
}
maxLifetime = val;
}
if (idleTimeout !== undefined && maxLifetime !== undefined) {
if (idleTimeout > maxLifetime) {
return { valid: false, error: '--idle-timeout must be <= --max-lifetime' };
}
}
return { valid: true, idleTimeout, maxLifetime };
}