Skip to content

Commit 8373120

Browse files
committed
feat(cli): add agents-settings-list-agents command and centralize command config
1 parent 44ed789 commit 8373120

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Env0ApiClient = require('../lib/api-client');
2+
const logger = require('../lib/logger');
3+
const { options } = require('../config/constants');
4+
5+
const { API_KEY, API_SECRET, ORGANIZATION_ID } = options;
6+
7+
const agentsSettingsListAgents = async options => {
8+
const apiClient = new Env0ApiClient();
9+
10+
await apiClient.init(options[API_KEY], options[API_SECRET]);
11+
12+
const result = await apiClient.callApi('get', 'agents', {
13+
params: { organizationId: options[ORGANIZATION_ID] }
14+
});
15+
16+
logger.info(JSON.stringify(result, null, 2));
17+
};
18+
19+
module.exports = agentsSettingsListAgents;

node/src/commands/run-command.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
const DeployUtils = require('../lib/deploy-utils');
22
const configManager = require('../lib/config-manager');
3-
const deploy = require('./deploy');
4-
const destroy = require('./destroy');
5-
const approve = require('./approve');
6-
const cancel = require('./cancel');
3+
const { commands } = require('../config/commands');
74
const { options } = require('../config/constants');
85
const logger = require('../lib/logger');
96
const _ = require('lodash');
107
const { argumentsMap } = require('../config/arguments');
118

129
const { API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME, REQUIRES_APPROVAL } = options;
1310

14-
const assertRequiredOptions = options => {
15-
const requiredOptions = [API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME];
11+
const assertRequiredOptions = (command, options) => {
12+
const defaultRequiredOptions = [API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME];
13+
const commandConfig = commands[command] || {};
14+
const requiredOptions = commandConfig.requiredOptions || defaultRequiredOptions;
1615

1716
let missingOptions = [];
1817
requiredOptions.forEach(opt => !Object.keys(options).includes(opt) && missingOptions.push(opt));
@@ -34,24 +33,21 @@ const assertRequiresApprovalOption = requiresApproval => {
3433

3534
const runCommand = async (command, options, variables) => {
3635
options = configManager.read(options);
37-
assertRequiredOptions(options);
36+
assertRequiredOptions(command, options);
3837
assertRequiresApprovalOption(options[REQUIRES_APPROVAL]);
3938

4039
logger.setSecrets(options);
4140

4241
logger.info(`Running ${command} with the following arguments:`);
4342
Object.keys(options).forEach(opt => logger.info(`$ ${opt}: ${options[opt]}`));
4443

45-
const commands = {
46-
destroy: destroy,
47-
deploy: deploy,
48-
approve: approve,
49-
cancel: cancel
50-
};
44+
const commandConfig = commands[command];
5145

52-
await DeployUtils.init(options);
46+
if (commandConfig && commandConfig.useDeployUtils) {
47+
await DeployUtils.init(options);
48+
}
5349

54-
await commands[command](options, variables);
50+
await commandConfig.handler(options, variables);
5551
logger.info(`Command ${command} has finished successfully.`);
5652
};
5753

node/src/config/arguments.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const argumentsMap = {
2626
description: 'env0 API Key',
2727
prompt: 'env0 API Key',
2828
secret: true,
29-
group: ['deploy', 'destroy', 'approve', 'cancel', 'configure']
29+
group: ['deploy', 'destroy', 'approve', 'cancel', 'configure', 'agents-settings-list-agents']
3030
},
3131
[API_SECRET]: {
3232
name: API_SECRET,
@@ -35,15 +35,15 @@ const argumentsMap = {
3535
description: 'env0 API Secret',
3636
prompt: 'env0 API Secret',
3737
secret: true,
38-
group: ['deploy', 'destroy', 'approve', 'cancel', 'configure']
38+
group: ['deploy', 'destroy', 'approve', 'cancel', 'configure', 'agents-settings-list-agents']
3939
},
4040
[ORGANIZATION_ID]: {
4141
name: ORGANIZATION_ID,
4242
alias: 'o',
4343
type: String,
4444
description: 'env0 Organization ID',
4545
prompt: 'Organization ID',
46-
group: ['deploy', 'destroy', 'approve', 'cancel', 'configure']
46+
group: ['deploy', 'destroy', 'approve', 'cancel', 'configure', 'agents-settings-list-agents']
4747
},
4848
[PROJECT_ID]: {
4949
name: PROJECT_ID,

node/src/config/commands.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
const { argumentsMap, allArguments, baseArguments } = require('./arguments');
22
const { options } = require('./constants');
3+
const deploy = require('../commands/deploy');
4+
const destroy = require('../commands/destroy');
5+
const approve = require('../commands/approve');
6+
const cancel = require('../commands/cancel');
7+
const agentsSettingsListAgents = require('../commands/agents-settings-list-agents');
38

49
const { API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME, BLUEPRINT_ID, SKIP_STATE_REFRESH, REQUIRES_APPROVAL } = options;
510

11+
const BASE_REQUIRED_OPTIONS = [API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME];
12+
const ORG_REQUIRED_OPTIONS = [API_KEY, API_SECRET, ORGANIZATION_ID];
13+
614
const commands = {
715
deploy: {
16+
handler: deploy,
17+
requiredOptions: BASE_REQUIRED_OPTIONS,
18+
useDeployUtils: true,
819
options: allArguments,
920
help: [
1021
{
@@ -14,6 +25,9 @@ const commands = {
1425
]
1526
},
1627
destroy: {
28+
handler: destroy,
29+
requiredOptions: BASE_REQUIRED_OPTIONS,
30+
useDeployUtils: true,
1731
options: [...baseArguments, argumentsMap[REQUIRES_APPROVAL], argumentsMap[SKIP_STATE_REFRESH]],
1832
help: [
1933
{
@@ -23,6 +37,9 @@ const commands = {
2337
]
2438
},
2539
approve: {
40+
handler: approve,
41+
requiredOptions: BASE_REQUIRED_OPTIONS,
42+
useDeployUtils: true,
2643
options: baseArguments,
2744
help: [
2845
{
@@ -32,6 +49,9 @@ const commands = {
3249
]
3350
},
3451
cancel: {
52+
handler: cancel,
53+
requiredOptions: BASE_REQUIRED_OPTIONS,
54+
useDeployUtils: true,
3555
options: baseArguments,
3656
help: [
3757
{
@@ -40,6 +60,18 @@ const commands = {
4060
}
4161
]
4262
},
63+
'agents-settings-list-agents': {
64+
handler: agentsSettingsListAgents,
65+
requiredOptions: ORG_REQUIRED_OPTIONS,
66+
useDeployUtils: false,
67+
options: baseArguments,
68+
help: [
69+
{
70+
desc: 'Lists organization agents',
71+
example: `$ env0 agents-settings-list-agents -k <${API_KEY}> -s <${API_SECRET}> -o <${ORGANIZATION_ID}>\n`
72+
}
73+
]
74+
},
4375
configure: {
4476
options: [...baseArguments, argumentsMap[BLUEPRINT_ID]],
4577
help: [

0 commit comments

Comments
 (0)