bdy 1.25.32-dev → 1.25.34-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.callResultText = exports.toolArgumentRows = exports.isDestructiveTool = exports.validateToolArgs = exports.parseToolArgs = exports.mcpToolsOf = exports.parseMcpOutput = exports.assertMcpSucceeded = exports.runMcp = exports.finishMcp = exports.startMcp = exports.integrationRef = exports.integrationExecOptions = void 0;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_path_1 = require("node:path");
9
+ const utils_1 = require("../../utils");
10
+ const texts_1 = require("../../texts");
11
+ const cliError_1 = __importDefault(require("../../cliError"));
12
+ const input_1 = __importDefault(require("../../input"));
13
+ const execLog_1 = require("../../execLog");
14
+ // The options locating the integration, as on `integration get`.
15
+ const integrationExecOptions = (command) => {
16
+ command.option('-w, --workspace <domain>', texts_1.OPTION_REST_API_WORKSPACE);
17
+ command.option('-p, --project <name>', texts_1.OPTION_REST_API_PROJECT);
18
+ command.option('--scope <scope>', texts_1.OPT_COMMAND_SCOPE_ENVIRONMENT);
19
+ command.option('--environment <identifier>', texts_1.OPTION_INTEGRATION_ENVIRONMENT);
20
+ command.argument('<integration>', texts_1.OPTION_INTEGRATION_IDENTIFIER);
21
+ return command;
22
+ };
23
+ exports.integrationExecOptions = integrationExecOptions;
24
+ // The reference to an integration in printed hints has to resolve again from
25
+ // another shell or directory, so the line it was resolved through goes along.
26
+ const integrationRef = (identifier, options) => {
27
+ let ref = identifier;
28
+ const project = input_1.default.restApiProject(options.project, true);
29
+ if (project)
30
+ ref += ` -p ${project}`;
31
+ if (options.environment)
32
+ ref += ` --environment ${options.environment}`;
33
+ if (options.scope)
34
+ ref += ` --scope ${options.scope}`;
35
+ return ref;
36
+ };
37
+ exports.integrationRef = integrationRef;
38
+ const startMcp = async (client, workspace, hashId, mcp) => await client.executeIntegration(workspace, hashId, { type: 'MCP', mcp });
39
+ exports.startMcp = startMcp;
40
+ // Waits for the execution to leave INPROGRESS and reads its whole log.
41
+ const finishMcp = async (client, workspace, hashId, exec) => {
42
+ const execId = exec.execution_id;
43
+ while (exec.status === utils_1.TARGET_EXEC_STATUS.INPROGRESS) {
44
+ exec = await client.getIntegrationExec(workspace, hashId, execId, execLog_1.EXEC_WAIT_MS);
45
+ }
46
+ const lines = [];
47
+ await (0, execLog_1.forwardExecLog)((offset, limit) => client.getIntegrationExecLog(workspace, hashId, execId, offset, limit), 0, (page) => lines.push(...page));
48
+ return { exec, output: lines.join('\n') };
49
+ };
50
+ exports.finishMcp = finishMcp;
51
+ // Runs an operation and returns the parsed result, throwing the server's
52
+ // message when the execution did not succeed.
53
+ const runMcp = async (client, workspace, hashId, mcp, ref) => {
54
+ const started = await (0, exports.startMcp)(client, workspace, hashId, mcp);
55
+ const { exec, output } = await (0, exports.finishMcp)(client, workspace, hashId, started);
56
+ (0, exports.assertMcpSucceeded)(exec, output, ref);
57
+ return (0, exports.parseMcpOutput)(output);
58
+ };
59
+ exports.runMcp = runMcp;
60
+ const assertMcpSucceeded = (exec, output, ref) => {
61
+ if (exec.status === utils_1.TARGET_EXEC_STATUS.SUCCESSFUL)
62
+ return;
63
+ if (exec.status === utils_1.TARGET_EXEC_STATUS.CANCELLED) {
64
+ throw new cliError_1.default('command_failed', (0, texts_1.ERR_INTEGRATION_EXEC_CANCELLED)(ref, exec.execution_id));
65
+ }
66
+ throw new cliError_1.default('command_failed', (0, texts_1.ERR_INTEGRATION_EXEC_FAILED)(ref, exec.execution_id, exec.error_message || output.trim() || exec.failure_reason));
67
+ };
68
+ exports.assertMcpSucceeded = assertMcpSucceeded;
69
+ const parseMcpOutput = (output) => {
70
+ try {
71
+ return JSON.parse(output);
72
+ }
73
+ catch {
74
+ throw new cliError_1.default('command_failed', texts_1.ERR_INTEGRATION_EXEC_OUTPUT);
75
+ }
76
+ };
77
+ exports.parseMcpOutput = parseMcpOutput;
78
+ // LIST_TOOLS answers with an array; a server answering with the MCP
79
+ // tools/list envelope ({tools: [...]}) is read the same way.
80
+ const mcpToolsOf = (result) => Array.isArray(result) ? result : result?.tools || [];
81
+ exports.mcpToolsOf = mcpToolsOf;
82
+ // --args as JSON, @file or - (stdin); the tool arguments are an object.
83
+ const parseToolArgs = (args) => {
84
+ if (args === undefined)
85
+ return {};
86
+ let text = args;
87
+ if (args === '-') {
88
+ text = node_fs_1.default.readFileSync(0, 'utf8');
89
+ }
90
+ else if (args.startsWith('@')) {
91
+ const p = (0, node_path_1.resolve)(args.slice(1));
92
+ if (!node_fs_1.default.existsSync(p)) {
93
+ throw new cliError_1.default('file_not_found', (0, texts_1.ERR_INTEGRATION_EXEC_ARGS_FILE)(args.slice(1)));
94
+ }
95
+ text = node_fs_1.default.readFileSync(p, 'utf8');
96
+ }
97
+ let parsed;
98
+ try {
99
+ parsed = JSON.parse(text);
100
+ }
101
+ catch (err) {
102
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_INTEGRATION_EXEC_ARGS_JSON)(err.message));
103
+ }
104
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
105
+ throw new cliError_1.default('usage_error', (0, texts_1.ERR_INTEGRATION_EXEC_ARGS_JSON)(`got ${Array.isArray(parsed) ? 'an array' : parsed === null ? 'null' : typeof parsed}`));
106
+ }
107
+ return parsed;
108
+ };
109
+ exports.parseToolArgs = parseToolArgs;
110
+ let ajvInstance;
111
+ const getAjv = () => {
112
+ if (ajvInstance)
113
+ return ajvInstance;
114
+ const Ajv = require('ajv/dist/2020').default;
115
+ const addFormats = require('ajv-formats').default;
116
+ // strict off: servers write schemas by hand, unknown keywords included
117
+ const ajv = new Ajv({ allErrors: true, strict: false });
118
+ addFormats(ajv);
119
+ ajvInstance = ajv;
120
+ return ajv;
121
+ };
122
+ // Messages for every way the arguments break the tool's inputSchema, or
123
+ // null when the schema cannot be checked here - the server validates anyway,
124
+ // so a schema Ajv cannot compile (an older draft's tuple items, a remote
125
+ // $ref) must not block the call. $schema is dropped because servers declare
126
+ // draft-07 as often as 2020-12 and the keywords that matter are the same.
127
+ const validateToolArgs = (tool, args) => {
128
+ if (!tool.inputSchema || typeof tool.inputSchema !== 'object')
129
+ return null;
130
+ // $id too: Ajv keeps compiled schemas by it, and tools reuse ids
131
+ const schema = { ...tool.inputSchema };
132
+ delete schema.$schema;
133
+ delete schema.$id;
134
+ let validate;
135
+ try {
136
+ validate = getAjv().compile(schema);
137
+ }
138
+ catch {
139
+ return null;
140
+ }
141
+ if (validate(args))
142
+ return [];
143
+ return (validate.errors || []).map((error) => {
144
+ const path = error.instancePath
145
+ ? error.instancePath.slice(1).replace(/\//g, '.')
146
+ : '';
147
+ return path ? `${path}: ${error.message}` : `${error.message}`;
148
+ });
149
+ };
150
+ exports.validateToolArgs = validateToolArgs;
151
+ // Only an explicit destructiveHint counts. The MCP default for an unannotated
152
+ // tool that is not read-only is "destructive", which would put nearly every
153
+ // tool behind a prompt and teach callers to pass -f by reflex.
154
+ const isDestructiveTool = (tool) => tool.annotations?.destructiveHint === true &&
155
+ tool.annotations?.readOnlyHint !== true;
156
+ exports.isDestructiveTool = isDestructiveTool;
157
+ const schemaTypeOf = (prop) => {
158
+ if (!prop || typeof prop !== 'object')
159
+ return '-';
160
+ if (prop.const !== undefined)
161
+ return `const ${JSON.stringify(prop.const)}`;
162
+ if (Array.isArray(prop.enum))
163
+ return prop.enum.map(String).join(' | ');
164
+ if (prop.$ref)
165
+ return String(prop.$ref).split('/').pop();
166
+ const variants = prop.oneOf || prop.anyOf;
167
+ if (Array.isArray(variants))
168
+ return variants.map(schemaTypeOf).join(' | ');
169
+ const type = Array.isArray(prop.type) ? prop.type.join(' | ') : prop.type;
170
+ if (type === 'array')
171
+ return `array of ${schemaTypeOf(prop.items)}`;
172
+ if (prop.format)
173
+ return `${type || 'string'} (${prop.format})`;
174
+ return type || '-';
175
+ };
176
+ // One row per top-level argument; nested objects and arrays are summed up by
177
+ // type only - the whole schema is what --format json is for.
178
+ const toolArgumentRows = (tool) => {
179
+ const schema = tool.inputSchema || {};
180
+ const required = Array.isArray(schema.required)
181
+ ? schema.required
182
+ : [];
183
+ return Object.entries(schema.properties || {}).map(([name, prop]) => {
184
+ let description = (prop?.description || prop?.title || '-').split('\n')[0];
185
+ if (prop?.default !== undefined) {
186
+ description += ` (default: ${JSON.stringify(prop.default)})`;
187
+ }
188
+ return [
189
+ name,
190
+ schemaTypeOf(prop),
191
+ required.includes(name) ? 'yes' : 'no',
192
+ description,
193
+ ];
194
+ });
195
+ };
196
+ exports.toolArgumentRows = toolArgumentRows;
197
+ // A CallToolResult as text: text blocks as they are, other blocks described
198
+ // by what they are, structuredContent only when there is no content at all.
199
+ const callResultText = (result) => {
200
+ const content = Array.isArray(result?.content) ? result.content : null;
201
+ if (!content || !content.length) {
202
+ const data = result?.structuredContent ?? result;
203
+ return typeof data === 'string' ? data : JSON.stringify(data, null, 2);
204
+ }
205
+ return content
206
+ .map((block) => {
207
+ if (block?.type === 'text')
208
+ return block.text ?? '';
209
+ if (block?.type === 'resource') {
210
+ return block.resource?.text ?? `[resource ${block.resource?.uri ?? ''}]`;
211
+ }
212
+ if (block?.type === 'resource_link')
213
+ return `[resource ${block.uri ?? ''}]`;
214
+ if (block?.type === 'image' || block?.type === 'audio') {
215
+ return `[${block.type} ${block.mimeType ?? ''}]`.replace(' ]', ']');
216
+ }
217
+ return JSON.stringify(block);
218
+ })
219
+ .join('\n');
220
+ };
221
+ exports.callResultText = callResultText;
@@ -10,6 +10,7 @@ const get_1 = __importDefault(require("./integration/get"));
10
10
  const add_1 = __importDefault(require("./integration/add"));
11
11
  const delete_1 = __importDefault(require("./integration/delete"));
12
12
  const types_1 = __importDefault(require("./integration/types"));
13
+ const exec_1 = __importDefault(require("./integration/exec"));
13
14
  const commandIntegration = (0, utils_1.newCommand)('integration', texts_1.DESC_COMMAND_INTEGRATION);
14
15
  commandIntegration.alias('int');
15
16
  commandIntegration.addCommand(list_1.default);
@@ -17,4 +18,5 @@ commandIntegration.addCommand(get_1.default);
17
18
  commandIntegration.addCommand(add_1.default);
18
19
  commandIntegration.addCommand(delete_1.default);
19
20
  commandIntegration.addCommand(types_1.default);
21
+ commandIntegration.addCommand(exec_1.default);
20
22
  exports.default = commandIntegration;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const utils_1 = require("../../utils");
7
+ const texts_1 = require("../../texts");
8
+ const output_1 = __importDefault(require("../../output"));
9
+ const input_1 = __importDefault(require("../../input"));
10
+ const scope_1 = require("./scope");
11
+ const commandVariableCreate = (0, utils_1.newCommand)('create', texts_1.DESC_COMMAND_VARIABLE_CREATE, {
12
+ requiredScopes: [utils_1.REST_API_SCOPE.VARIABLE_WRITE],
13
+ examples: texts_1.EXAMPLE_VARIABLE_CREATE,
14
+ });
15
+ commandVariableCreate.alias('add');
16
+ commandVariableCreate.option('-w, --workspace <domain>', texts_1.OPTION_REST_API_WORKSPACE);
17
+ commandVariableCreate.option('-p, --project <name>', texts_1.OPTION_REST_API_PROJECT);
18
+ commandVariableCreate.option('--pipeline <identifier>', texts_1.OPTION_VARIABLE_PIPELINE);
19
+ commandVariableCreate.option('--environment <identifier>', texts_1.OPTION_VARIABLE_ENVIRONMENT);
20
+ commandVariableCreate.option('--sandbox <identifier>', texts_1.OPTION_VARIABLE_SANDBOX);
21
+ commandVariableCreate.option('--scope <scope>', texts_1.OPT_COMMAND_VARIABLE_SCOPE);
22
+ commandVariableCreate.option('--file <path>', texts_1.OPTION_VARIABLE_FILE);
23
+ commandVariableCreate.option('--encrypted', texts_1.OPTION_VARIABLE_ENCRYPTED);
24
+ commandVariableCreate.option('--settable', texts_1.OPTION_VARIABLE_SETTABLE);
25
+ commandVariableCreate.option('--disable', texts_1.OPTION_VARIABLE_DISABLE);
26
+ commandVariableCreate.option('--note <note>', texts_1.OPT_COMMAND_NOTE);
27
+ commandVariableCreate.option('--agent-note <note>', texts_1.OPT_COMMAND_AGENT_NOTE);
28
+ commandVariableCreate.option('--format <text|json>', texts_1.OPTION_FORMAT);
29
+ commandVariableCreate.argument('<key>', texts_1.OPTION_VARIABLE_KEY);
30
+ commandVariableCreate.argument('[value]', texts_1.OPTION_VARIABLE_VALUE);
31
+ commandVariableCreate.action(async (key, value, options) => {
32
+ const workspace = input_1.default.restApiWorkspace(options.workspace);
33
+ const { project, scope } = (0, scope_1.variableScope)(options);
34
+ const body = {
35
+ key,
36
+ value: input_1.default.variableValue(value, options.file) ?? '',
37
+ encrypted: !!options.encrypted,
38
+ settable: !!options.settable,
39
+ };
40
+ if (options.disable)
41
+ body.disabled = true;
42
+ if (options.note !== undefined)
43
+ body.note = options.note;
44
+ if (options.agentNote !== undefined)
45
+ body.agent_note = options.agentNote;
46
+ const client = input_1.default.restApiTokenClient();
47
+ const params = await (0, scope_1.resolveVariableScope)(client, workspace, project, scope, options);
48
+ const result = await client.createVariable(workspace, params, body);
49
+ if (options.format === 'json') {
50
+ output_1.default.json(result);
51
+ output_1.default.exitNormal();
52
+ }
53
+ output_1.default.exitSuccess((0, texts_1.TXT_VARIABLE_CREATED)(result.key, (0, scope_1.variableScopeOf)(result)));
54
+ });
55
+ exports.default = commandVariableCreate;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const utils_1 = require("../../utils");
7
+ const texts_1 = require("../../texts");
8
+ const output_1 = __importDefault(require("../../output"));
9
+ const input_1 = __importDefault(require("../../input"));
10
+ const scope_1 = require("./scope");
11
+ const commandVariableDelete = (0, utils_1.newCommand)('delete', texts_1.DESC_COMMAND_VARIABLE_DELETE, {
12
+ requiredScopes: [utils_1.REST_API_SCOPE.VARIABLE_MANAGE],
13
+ examples: texts_1.EXAMPLE_VARIABLE_DELETE,
14
+ });
15
+ commandVariableDelete.alias('rm');
16
+ commandVariableDelete.option('-w, --workspace <domain>', texts_1.OPTION_REST_API_WORKSPACE);
17
+ commandVariableDelete.option('-p, --project <name>', texts_1.OPTION_REST_API_PROJECT);
18
+ commandVariableDelete.option('--pipeline <identifier>', texts_1.OPTION_VARIABLE_PIPELINE);
19
+ commandVariableDelete.option('--environment <identifier>', texts_1.OPTION_VARIABLE_ENVIRONMENT);
20
+ commandVariableDelete.option('--sandbox <identifier>', texts_1.OPTION_VARIABLE_SANDBOX);
21
+ commandVariableDelete.option('--scope <scope>', texts_1.OPT_COMMAND_VARIABLE_SCOPE);
22
+ commandVariableDelete.option('-f, --force', texts_1.OPTION_CONFIRM_FORCE);
23
+ commandVariableDelete.option('--format <text|json>', texts_1.OPTION_FORMAT);
24
+ commandVariableDelete.argument('<key>', texts_1.OPTION_VARIABLE_KEY);
25
+ commandVariableDelete.action(async (key, options) => {
26
+ const workspace = input_1.default.restApiWorkspace(options.workspace);
27
+ const client = input_1.default.restApiTokenClient();
28
+ const { variable, scope } = await (0, scope_1.resolveVariable)(client, workspace, key, options,
29
+ /* strictScope */ true);
30
+ const json = options.format === 'json';
31
+ const confirmed = await output_1.default.confirmDestructive(options.force, (0, texts_1.TXT_VARIABLE_DELETE_CONFIRM)(key, scope), json);
32
+ if (!confirmed)
33
+ output_1.default.exitResult(json, { deleted: null });
34
+ await client.deleteVariable(workspace, variable.id);
35
+ output_1.default.exitResult(json, { deleted: key }, (0, texts_1.TXT_VARIABLE_DELETED)(key));
36
+ });
37
+ exports.default = commandVariableDelete;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const utils_1 = require("../../utils");
7
+ const texts_1 = require("../../texts");
8
+ const output_1 = __importDefault(require("../../output"));
9
+ const input_1 = __importDefault(require("../../input"));
10
+ const scope_1 = require("./scope");
11
+ const commandVariableGet = (0, utils_1.newCommand)('get', texts_1.DESC_COMMAND_VARIABLE_GET, {
12
+ requiredScopes: [utils_1.REST_API_SCOPE.VARIABLE_READ],
13
+ examples: texts_1.EXAMPLE_VARIABLE_GET,
14
+ });
15
+ commandVariableGet.option('-w, --workspace <domain>', texts_1.OPTION_REST_API_WORKSPACE);
16
+ commandVariableGet.option('-p, --project <name>', texts_1.OPTION_REST_API_PROJECT);
17
+ commandVariableGet.option('--pipeline <identifier>', texts_1.OPTION_VARIABLE_PIPELINE);
18
+ commandVariableGet.option('--environment <identifier>', texts_1.OPTION_VARIABLE_ENVIRONMENT);
19
+ commandVariableGet.option('--sandbox <identifier>', texts_1.OPTION_VARIABLE_SANDBOX);
20
+ commandVariableGet.option('--scope <scope>', texts_1.OPT_COMMAND_VARIABLE_SCOPE);
21
+ commandVariableGet.option('--format <text|json>', texts_1.OPTION_FORMAT);
22
+ commandVariableGet.argument('<key>', texts_1.OPTION_VARIABLE_KEY);
23
+ commandVariableGet.action(async (key, options) => {
24
+ const workspace = input_1.default.restApiWorkspace(options.workspace);
25
+ const client = input_1.default.restApiTokenClient();
26
+ const { variable, scope } = await (0, scope_1.resolveVariable)(client, workspace, key, options);
27
+ if (options.format === 'json') {
28
+ output_1.default.json(variable);
29
+ }
30
+ else {
31
+ const data = [
32
+ ['Field', 'Value'],
33
+ ['ID', String(variable.id)],
34
+ ['Key', variable.key],
35
+ ['Value', (0, scope_1.variableValueOf)(variable)],
36
+ ['Type', variable.type || '-'],
37
+ ['Scope', scope],
38
+ ['Encrypted', variable.encrypted ? 'true' : 'false'],
39
+ ['Settable', variable.settable ? 'true' : 'false'],
40
+ ['Disabled', variable.disabled ? 'true' : 'false'],
41
+ ['Note', variable.note || '-'],
42
+ ['Agent note', variable.agent_note || '-'],
43
+ ];
44
+ if (variable.html_url) {
45
+ data.push(['URL', variable.html_url]);
46
+ }
47
+ output_1.default.table(data);
48
+ }
49
+ output_1.default.exitNormal();
50
+ });
51
+ exports.default = commandVariableGet;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const utils_1 = require("../../utils");
7
+ const texts_1 = require("../../texts");
8
+ const output_1 = __importDefault(require("../../output"));
9
+ const input_1 = __importDefault(require("../../input"));
10
+ const scope_1 = require("./scope");
11
+ const commandVariableList = (0, utils_1.newCommand)('list', texts_1.DESC_COMMAND_VARIABLE_LIST, {
12
+ requiredScopes: [utils_1.REST_API_SCOPE.VARIABLE_READ],
13
+ examples: texts_1.EXAMPLE_VARIABLE_LIST,
14
+ });
15
+ commandVariableList.alias('ls');
16
+ commandVariableList.option('-w, --workspace <domain>', texts_1.OPTION_REST_API_WORKSPACE);
17
+ commandVariableList.option('-p, --project <name>', texts_1.OPTION_REST_API_PROJECT);
18
+ commandVariableList.option('--pipeline <identifier>', texts_1.OPTION_VARIABLE_PIPELINE);
19
+ commandVariableList.option('--environment <identifier>', texts_1.OPTION_VARIABLE_ENVIRONMENT);
20
+ commandVariableList.option('--sandbox <identifier>', texts_1.OPTION_VARIABLE_SANDBOX);
21
+ commandVariableList.option('--scope <scope>', texts_1.OPT_COMMAND_VARIABLE_SCOPE);
22
+ commandVariableList.option('--format <text|json>', texts_1.OPTION_FORMAT);
23
+ commandVariableList.action(async (options) => {
24
+ const workspace = input_1.default.restApiWorkspace(options.workspace);
25
+ const { project, scope } = (0, scope_1.variableScope)(options);
26
+ input_1.default.projectScopeNote(options, project, scope, 'variables');
27
+ const client = input_1.default.restApiTokenClient();
28
+ const params = await (0, scope_1.resolveVariableScope)(client, workspace, project, scope, options);
29
+ const result = await client.getVariables(workspace, params);
30
+ const variables = result.variables || [];
31
+ if (options.format === 'json') {
32
+ output_1.default.json(variables);
33
+ }
34
+ else {
35
+ if (variables.length === 0) {
36
+ output_1.default.exitNormal(texts_1.TXT_VARIABLE_NOT_FOUND);
37
+ }
38
+ const data = [
39
+ ['KEY', 'VALUE', 'TYPE', 'ENCRYPTED', 'SETTABLE', 'SCOPE'],
40
+ ];
41
+ for (const variable of variables) {
42
+ data.push([
43
+ variable.key,
44
+ (0, scope_1.variableValueOf)(variable, 40),
45
+ variable.type || '-',
46
+ variable.encrypted ? 'true' : 'false',
47
+ variable.settable ? 'true' : 'false',
48
+ // the list is exact-scope and its items carry no owner object
49
+ scope,
50
+ ]);
51
+ }
52
+ output_1.default.table(data);
53
+ }
54
+ output_1.default.exitNormal();
55
+ });
56
+ exports.default = commandVariableList;
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.variableValueOf = exports.variableScopeOf = exports.variableUpdateBody = exports.resolveVariable = exports.resolveVariableScope = exports.variableScope = void 0;
7
+ const utils_1 = require("../../utils");
8
+ const texts_1 = require("../../texts");
9
+ const cliError_1 = __importDefault(require("../../cliError"));
10
+ const input_1 = __importDefault(require("../../input"));
11
+ // Scope of the variable from flags/env, validated (Input.variableScope).
12
+ const variableScope = (options) => {
13
+ const project = input_1.default.restApiProject(options.project, true);
14
+ const scope = input_1.default.variableScope(project, options.pipeline, options.environment, options.sandbox, options.scope);
15
+ return { project, scope };
16
+ };
17
+ exports.variableScope = variableScope;
18
+ // Identifiers of the scope -> the ids the variables endpoints take.
19
+ // Environments and sandboxes exist on both workspace and project level, so
20
+ // for them project is optional and narrows the lookup when given.
21
+ const resolveVariableScope = async (client, workspace, project, scope, options) => {
22
+ const params = {};
23
+ if (scope === utils_1.VARIABLE_SCOPE.PIPELINE) {
24
+ const { pipeline_id } = await client.getPipelineByIdentifier(workspace, project, options.pipeline);
25
+ if (!pipeline_id) {
26
+ throw new cliError_1.default('not_found', texts_1.ERR_PIPELINE_NOT_FOUND, texts_1.HINT_PIPELINE_LIST);
27
+ }
28
+ params.pipelineId = pipeline_id;
29
+ }
30
+ else if (scope === utils_1.VARIABLE_SCOPE.ENVIRONMENT) {
31
+ params.environmentId = await input_1.default.environmentId(client, workspace, options.environment, options);
32
+ }
33
+ else if (scope === utils_1.VARIABLE_SCOPE.SANDBOX) {
34
+ params.sandboxId = await resolveSandboxId(client, workspace, project, options);
35
+ }
36
+ else if (scope === utils_1.VARIABLE_SCOPE.PROJECT) {
37
+ params.project = project;
38
+ }
39
+ return params;
40
+ };
41
+ exports.resolveVariableScope = resolveVariableScope;
42
+ // Like Input.environmentId: a project implied by BUDDY_PROJECT or a linked
43
+ // directory must not hide a workspace sandbox, an explicit -p does.
44
+ const resolveSandboxId = async (client, workspace, project, options) => {
45
+ const identifier = options.sandbox;
46
+ let sandboxId;
47
+ if (project) {
48
+ const resolved = await client.getSandboxByIdentifier(workspace, project, identifier);
49
+ sandboxId = resolved.sandbox_id;
50
+ }
51
+ if (!sandboxId && !options.project) {
52
+ sandboxId = await client.findSandboxByIdentifier(workspace, identifier, null);
53
+ }
54
+ if (!sandboxId) {
55
+ throw new cliError_1.default('not_found', texts_1.ERR_SANDBOX_NOT_FOUND, texts_1.HINT_SANDBOX_LIST);
56
+ }
57
+ return sandboxId;
58
+ };
59
+ // Variables are not in /identifiers: a key is unique within its scope, so the
60
+ // variable is looked up by key in the list of that exact scope. The list
61
+ // items carry no owner object (project, pipeline, ...), so the scope comes
62
+ // back from here rather than from variableScopeOf.
63
+ //
64
+ // Reads fall back to the workspace variable when the PROJECT scope was only
65
+ // implied (BUDDY_PROJECT or a linked project), the way targets do. strictScope
66
+ // is for commands that change the variable (update/delete): the scope the
67
+ // command runs in is the one acted on, no fallback. A miss there still looks
68
+ // at the workspace, only to point at --scope WORKSPACE: get finds the
69
+ // variable through the fallback, so a bare "not found" from rm would be
70
+ // a dead end.
71
+ const resolveVariable = async (client, workspace, key, options, strictScope = false) => {
72
+ const { project, scope } = (0, exports.variableScope)(options);
73
+ const find = async (s) => {
74
+ const params = await (0, exports.resolveVariableScope)(client, workspace, project, s, options);
75
+ const result = await client.getVariables(workspace, params);
76
+ return (result.variables || []).find((v) => v.key === key);
77
+ };
78
+ let variable = await find(scope);
79
+ if (variable)
80
+ return { variable, scope };
81
+ if (scope === utils_1.VARIABLE_SCOPE.PROJECT &&
82
+ !options.project &&
83
+ !options.scope) {
84
+ variable = await find(utils_1.VARIABLE_SCOPE.WORKSPACE);
85
+ if (variable && !strictScope) {
86
+ return { variable, scope: utils_1.VARIABLE_SCOPE.WORKSPACE };
87
+ }
88
+ if (variable) {
89
+ throw new cliError_1.default('not_found', texts_1.ERR_VARIABLE_NOT_FOUND, texts_1.HINT_VARIABLE_LIST_WORKSPACE);
90
+ }
91
+ }
92
+ throw new cliError_1.default('not_found', texts_1.ERR_VARIABLE_NOT_FOUND, texts_1.HINT_VARIABLE_LIST);
93
+ };
94
+ exports.resolveVariable = resolveVariable;
95
+ // The PATCH is not partial: a writable field left out of the body is reset
96
+ // (an absent settable turns false). So the update re-sends what is stored,
97
+ // minus the read-only and owner fields and the secrets - value, password and
98
+ // passphrase are kept by the API when absent.
99
+ const VARIABLE_NOT_CARRIED = [
100
+ 'url',
101
+ 'html_url',
102
+ 'id',
103
+ 'type',
104
+ 'value',
105
+ 'password',
106
+ 'passphrase',
107
+ 'public_value',
108
+ 'key_fingerprint',
109
+ 'checksum',
110
+ 'binary',
111
+ 'project',
112
+ 'pipeline',
113
+ 'action',
114
+ 'sandbox',
115
+ 'environment',
116
+ 'base_environment',
117
+ ];
118
+ const variableUpdateBody = (variable, changes) => {
119
+ const body = {};
120
+ for (const [field, value] of Object.entries(variable)) {
121
+ if (!VARIABLE_NOT_CARRIED.includes(field))
122
+ body[field] = value;
123
+ }
124
+ return { ...body, ...changes };
125
+ };
126
+ exports.variableUpdateBody = variableUpdateBody;
127
+ // For API responses of create/update, which carry the owner object. An
128
+ // action-scoped variable also carries its pipeline, so the action goes first.
129
+ const variableScopeOf = (variable) => {
130
+ if (variable.action)
131
+ return 'ACTION';
132
+ if (variable.pipeline)
133
+ return utils_1.VARIABLE_SCOPE.PIPELINE;
134
+ if (variable.sandbox)
135
+ return utils_1.VARIABLE_SCOPE.SANDBOX;
136
+ if (variable.environment)
137
+ return utils_1.VARIABLE_SCOPE.ENVIRONMENT;
138
+ if (variable.project)
139
+ return utils_1.VARIABLE_SCOPE.PROJECT;
140
+ return utils_1.VARIABLE_SCOPE.WORKSPACE;
141
+ };
142
+ exports.variableScopeOf = variableScopeOf;
143
+ // Text views never print an encrypted value: the API already masks it, but
144
+ // the masked form is noise. Multi-line values (keys, files) are cut to the
145
+ // first line so a table stays readable; the full value is in --format json.
146
+ const variableValueOf = (variable, maxLength) => {
147
+ if (variable.encrypted)
148
+ return '***';
149
+ const value = variable.value ?? '';
150
+ if (!value)
151
+ return '-';
152
+ const lines = value.split('\n');
153
+ let shown = lines[0];
154
+ let cut = lines.length > 1;
155
+ if (maxLength && shown.length > maxLength) {
156
+ shown = shown.slice(0, maxLength);
157
+ cut = true;
158
+ }
159
+ return cut ? `${shown}...` : shown;
160
+ };
161
+ exports.variableValueOf = variableValueOf;