|
| 1 | +const Command = require('../../Command'); |
| 2 | +const CFError = require('cf-errors'); |
| 3 | +const { sdk } = require('../../../../logic'); |
| 4 | + |
| 5 | +const applyRoot = require('../root/apply.cmd'); |
| 6 | +const { ignoreHttpError } = require('../../helpers/general'); |
| 7 | + |
| 8 | +const command = new Command({ |
| 9 | + command: 'agent [id|name]', |
| 10 | + aliases: [], |
| 11 | + parent: applyRoot, |
| 12 | + description: 'Patch an agent', |
| 13 | + webDocs: { |
| 14 | + category: 'Agents', |
| 15 | + title: 'Patch Agent', |
| 16 | + }, |
| 17 | + builder: (yargs) => { |
| 18 | + yargs |
| 19 | + .positional('id|name', { |
| 20 | + describe: 'agent id or name', |
| 21 | + }) |
| 22 | + .option('newName', { |
| 23 | + describe: 'New name', |
| 24 | + alias: 'n', |
| 25 | + }) |
| 26 | + .option('runtimes', { |
| 27 | + array: true, |
| 28 | + alias: 're', |
| 29 | + describe: 'Agent runtimes', |
| 30 | + }) |
| 31 | + .example( |
| 32 | + 'codefresh patch agent ID --name NEW_NAME', |
| 33 | + 'Replace agent name. Specifying agent by ID', |
| 34 | + ) |
| 35 | + .example( |
| 36 | + 'codefresh patch agent NAME --re runtime1', |
| 37 | + 'Replace agent runtimes. Specifying agent by NAME', |
| 38 | + ); |
| 39 | + |
| 40 | + return yargs; |
| 41 | + }, |
| 42 | + handler: async (argv) => { |
| 43 | + const { id, name } = argv; |
| 44 | + |
| 45 | + const { |
| 46 | + newName, |
| 47 | + runtimes, |
| 48 | + } = argv; |
| 49 | + |
| 50 | + let agent = await sdk.agents.get({ agentId: id }).catch(ignoreHttpError); |
| 51 | + agent = agent || await sdk.agents.getByName({ name }).catch(ignoreHttpError); |
| 52 | + if (!agent) { |
| 53 | + throw new CFError(`No such agent: "${name || id}"`); |
| 54 | + } |
| 55 | + |
| 56 | + await sdk.agents.replace({ agentId: agent.id }, { name: newName, runtimes }); |
| 57 | + console.log(`Agent: "${name || id}" patched.`); |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +module.exports = command; |
0 commit comments