forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.tsx
More file actions
96 lines (90 loc) · 3.21 KB
/
Copy pathcommand.tsx
File metadata and controls
96 lines (90 loc) · 3.21 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
import { getErrorMessage } from '../../errors';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import { handleFetchAccess } from './action';
import type { FetchAccessResult } from './action';
import type { FetchAccessOptions } from './types';
import type { Command } from '@commander-js/extra-typings';
import { Box, Text, render } from 'ink';
export const registerFetch = (program: Command) => {
const fetchCmd = program.command('fetch').description(COMMAND_DESCRIPTIONS.fetch);
fetchCmd
.command('access')
.description('Fetch access info (URL, token, auth guidance) for a deployed gateway or agent.')
.option('--name <resource>', 'Gateway or agent name [non-interactive]')
.option('--type <type>', 'Resource type: gateway (default) or agent [non-interactive]', 'gateway')
.option('--target <target>', 'Deployment target [non-interactive]')
.option('--identity-name <name>', 'Identity credential name for token fetch [non-interactive]')
.option('--json', 'Output as JSON [non-interactive]')
.action(async (cliOptions: Record<string, unknown>) => {
const options = cliOptions as unknown as FetchAccessOptions;
requireProject();
let result: FetchAccessResult;
try {
result = await handleFetchAccess(options);
} catch (error) {
if (options.json) {
console.log(JSON.stringify({ success: false, error: getErrorMessage(error) }));
} else {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
}
process.exit(1);
return;
}
if (!result.success) {
if (options.json) {
console.log(
JSON.stringify({
success: false,
error: result.error,
...(result.availableGateways && { availableGateways: result.availableGateways }),
})
);
} else if (!result.availableGateways) {
render(<Text color="red">{result.error}</Text>);
} else {
render(
<Box flexDirection="column">
<Text color="red">{result.error}</Text>
<Text>Available gateways:</Text>
{result.availableGateways.map(gw => (
<Text key={gw.name}>
{' '}
{gw.name} [{gw.authType}]
</Text>
))}
</Box>
);
}
process.exit(1);
return;
}
if (options.json) {
console.log(JSON.stringify({ success: true, ...result.result }, null, 2));
return;
}
const r = result.result!;
render(
<Box flexDirection="column">
<Text>
<Text bold>URL:</Text>
<Text color="green"> {r.url}</Text>
</Text>
<Text>
<Text bold>Auth:</Text> {r.authType}
</Text>
{r.message && <Text>{r.message}</Text>}
{r.token && (
<Text>
<Text bold>Token:</Text> {r.token}
</Text>
)}
{r.expiresIn !== undefined && (
<Text>
<Text bold>Expires in:</Text> {r.expiresIn}s
</Text>
)}
</Box>
);
});
};