-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlist.ts
More file actions
214 lines (189 loc) · 5.82 KB
/
list.ts
File metadata and controls
214 lines (189 loc) · 5.82 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {Flags} from '@oclif/core';
import {OdsCommand, TableRenderer, type ColumnDef} from '@salesforce/b2c-tooling-sdk/cli';
import {getApiErrorMessage, type OdsComponents} from '@salesforce/b2c-tooling-sdk';
import {t, withDocs} from '../../i18n/index.js';
type SandboxModel = OdsComponents['schemas']['SandboxModel'];
/**
* Response type for the list command.
*/
interface OdsListResponse {
count: number;
data: SandboxModel[];
}
export const COLUMNS: Record<string, ColumnDef<SandboxModel>> = {
realm: {
header: 'Realm',
get: (s) => s.realm || '-',
},
instance: {
header: 'Num',
get: (s) => s.instance || '-',
},
state: {
header: 'State',
get: (s) => s.state || '-',
},
profile: {
header: 'Profile',
get: (s) => s.resourceProfile || '-',
},
created: {
header: 'Created',
get: (s) => (s.createdAt ? new Date(s.createdAt).toISOString().slice(0, 10) : '-'),
},
eol: {
header: 'EOL',
get(s) {
if (!s.eol) return '-';
const d = new Date(s.eol);
const date = d.toISOString().slice(0, 10);
const msUntilEol = d.getTime() - Date.now();
if (msUntilEol <= 24 * 60 * 60 * 1000) {
const hh = String(d.getUTCHours()).padStart(2, '0');
const mm = String(d.getUTCMinutes()).padStart(2, '0');
return `${date} ${hh}:${mm}`;
}
return date;
},
},
id: {
header: 'ID',
get: (s) => s.id || '-',
},
hostname: {
header: 'Hostname',
get: (s) => s.hostName || '-',
extended: true,
},
createdBy: {
header: 'Created By',
get: (s) => s.createdBy || '-',
extended: true,
},
autoScheduled: {
header: 'Auto',
get: (s) => (s.autoScheduled ? 'Yes' : 'No'),
extended: true,
},
};
/** Default columns shown without --extended */
const DEFAULT_COLUMNS = ['realm', 'instance', 'state', 'profile', 'created', 'eol', 'id'];
const tableRenderer = new TableRenderer(COLUMNS);
/**
* Command to list all on-demand sandboxes.
*/
export default class SandboxList extends OdsCommand<typeof SandboxList> {
static aliases = ['ods:list'];
static description = withDocs(
t('commands.sandbox.list.description', 'List all on-demand sandboxes'),
'/cli/sandbox.html#b2c-sandbox-list',
);
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --realm abcd',
'<%= config.bin %> <%= command.id %> --filter-params "realm=abcd&state=started"',
'<%= config.bin %> <%= command.id %> --show-deleted',
'<%= config.bin %> <%= command.id %> --extended',
'<%= config.bin %> <%= command.id %> --columns realm,instance,state,hostname',
'<%= config.bin %> <%= command.id %> --json',
];
static flags = {
realm: Flags.string({
char: 'r',
description: 'Filter by realm ID (four-letter ID)',
}),
'filter-params': Flags.string({
description: 'Raw filter parameters (e.g., "realm=abcd&state=started&resourceProfile=medium")',
}),
'show-deleted': Flags.boolean({
description: 'Include deleted sandboxes in the list',
default: false,
}),
columns: Flags.string({
char: 'c',
description: `Columns to display (comma-separated). Available: ${Object.keys(COLUMNS).join(', ')}`,
}),
extended: Flags.boolean({
char: 'x',
description: 'Show all columns including extended fields',
default: false,
}),
};
async run(): Promise<OdsListResponse> {
const host = this.odsHost;
const includeDeleted = this.flags['show-deleted'];
const realm = this.flags.realm;
const rawFilterParams = this.flags['filter-params'];
// Build filter params string
let filterParams: string | undefined;
if (realm || rawFilterParams) {
const parts: string[] = [];
if (realm) {
parts.push(`realm=${realm}`);
}
if (rawFilterParams) {
parts.push(rawFilterParams);
}
filterParams = parts.join('&');
}
this.log(t('commands.sandbox.list.fetching', 'Fetching sandboxes from {{host}}...', {host}));
const result = await this.odsClient.GET('/sandboxes', {
params: {
query: {
include_deleted: includeDeleted,
filter_params: filterParams,
},
},
});
if (result.error) {
this.error(
t('commands.sandbox.list.error', 'Failed to fetch sandboxes: {{message}}', {
message: getApiErrorMessage(result.error, result.response),
}),
);
}
const sandboxes = result.data?.data ?? [];
const response: OdsListResponse = {
count: sandboxes.length,
data: sandboxes,
};
if (this.jsonEnabled()) {
return response;
}
if (sandboxes.length === 0) {
this.log(t('commands.sandbox.list.noSandboxes', 'No sandboxes found.'));
return response;
}
tableRenderer.render(sandboxes, this.getSelectedColumns());
return response;
}
/**
* Determines which columns to display based on flags.
*/
private getSelectedColumns(): string[] {
const columnsFlag = this.flags.columns;
const extended = this.flags.extended;
if (columnsFlag) {
// User specified explicit columns
const requested = columnsFlag.split(',').map((c) => c.trim());
const valid = tableRenderer.validateColumnKeys(requested);
if (valid.length === 0) {
this.warn(`No valid columns specified. Available: ${tableRenderer.getColumnKeys().join(', ')}`);
return DEFAULT_COLUMNS;
}
return valid;
}
if (extended) {
// Show all columns
return tableRenderer.getColumnKeys();
}
// Default columns (non-extended)
return DEFAULT_COLUMNS;
}
}