-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget.ts
More file actions
93 lines (78 loc) · 2.61 KB
/
get.ts
File metadata and controls
93 lines (78 loc) · 2.61 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
import { getOption } from '../../utils/options.js';
import { getLoginMethod } from '../../auth/s3-client.js';
import { getAuthClient } from '../../auth/client.js';
import { getSelectedOrganization } from '../../auth/storage.js';
import { getTigrisConfig } from '../../auth/config.js';
import { getAccessKey } from '@tigrisdata/iam';
import {
printStart,
printSuccess,
printFailure,
msg,
} from '../../utils/messages.js';
import { exitWithError } from '../../utils/exit.js';
const context = msg('access-keys', 'get');
export default async function get(options: Record<string, unknown>) {
printStart(context);
const json = getOption<boolean>(options, ['json']);
const format = json
? 'json'
: getOption<string>(options, ['format', 'f', 'F'], 'table');
const id = getOption<string>(options, ['id']);
if (!id) {
printFailure(context, 'Access key ID is required');
exitWithError('Access key ID is required', context);
}
const loginMethod = await getLoginMethod();
if (loginMethod !== 'oauth') {
printFailure(
context,
'Access keys can only be retrieved when logged in via OAuth.\nRun "tigris login oauth" first.'
);
exitWithError(
'Access keys can only be retrieved when logged in via OAuth.\nRun "tigris login oauth" first.',
context
);
}
const authClient = getAuthClient();
const isAuthenticated = await authClient.isAuthenticated();
if (!isAuthenticated) {
printFailure(context, 'Not authenticated. Run "tigris login oauth" first.');
exitWithError(
'Not authenticated. Run "tigris login oauth" first.',
context
);
}
const accessToken = await authClient.getAccessToken();
const selectedOrg = getSelectedOrganization();
const tigrisConfig = getTigrisConfig();
const { data, error } = await getAccessKey(id, {
config: {
sessionToken: accessToken,
organizationId: selectedOrg ?? undefined,
iamEndpoint: tigrisConfig.iamEndpoint,
},
});
if (error) {
printFailure(context, error.message);
exitWithError(error, context);
}
if (format === 'json') {
console.log(JSON.stringify(data));
} else {
console.log(` Name: ${data.name}`);
console.log(` ID: ${data.id}`);
console.log(` Status: ${data.status}`);
console.log(` Created: ${data.createdAt}`);
console.log(` Organization: ${data.organizationId}`);
if (data.roles && data.roles.length > 0) {
console.log(` Roles:`);
for (const role of data.roles) {
console.log(` - ${role.bucket}: ${role.role}`);
}
} else {
console.log(` Roles: None`);
}
}
printSuccess(context);
}