|
| 1 | +const Command = require('../../Command'); |
| 2 | +const _ = require('lodash'); |
| 3 | +const { section: sectionLogic, board: boardLogic } = require('../../../../logic').api; |
| 4 | + |
| 5 | +const applyRoot = require('../root/apply.cmd'); |
| 6 | + |
| 7 | + |
| 8 | +const command = new Command({ |
| 9 | + command: 'section [id]', |
| 10 | + aliases: [], |
| 11 | + parent: applyRoot, |
| 12 | + description: 'Update a section', |
| 13 | + webDocs: { |
| 14 | + category: 'Section', |
| 15 | + title: 'Update Section', |
| 16 | + }, |
| 17 | + builder: (yargs) => { |
| 18 | + yargs |
| 19 | + .positional('id', { |
| 20 | + describe: 'Id of existing section', |
| 21 | + }) |
| 22 | + .option('boardId', { |
| 23 | + describe: 'Id of existing board', |
| 24 | + }) |
| 25 | + .option('boardName', { |
| 26 | + describe: 'Name of existing board', |
| 27 | + }) |
| 28 | + .option('name', { |
| 29 | + describe: 'Name of existing section', |
| 30 | + }) |
| 31 | + .option('newName', { |
| 32 | + describe: 'New name', |
| 33 | + }) |
| 34 | + .option('cluster', { |
| 35 | + describe: 'Link to cluster', |
| 36 | + }) |
| 37 | + .option('color', { |
| 38 | + describe: 'New color', |
| 39 | + }) |
| 40 | + .option('index', { |
| 41 | + describe: 'New index', |
| 42 | + }) |
| 43 | + .example( |
| 44 | + 'codefresh patch section ID --new-name NEW_NAME --filter /app-*/gi', |
| 45 | + 'Update name and filter of section. Specifying section by ID', |
| 46 | + ) |
| 47 | + .example( |
| 48 | + 'codefresh patch section --name OLD_NAME --new-name NEW_NAME --filter /app-*/gi', |
| 49 | + 'Update name and filter of section. Specifying section by NAME', |
| 50 | + ); |
| 51 | + |
| 52 | + return yargs; |
| 53 | + }, |
| 54 | + handler: async (argv) => { |
| 55 | + let { id, boardId } = argv; |
| 56 | + |
| 57 | + if (!argv.boardId) { |
| 58 | + if (!argv.boardName) throw Error('Nor board-id nor board-name was specified'); |
| 59 | + |
| 60 | + const boardObj = await boardLogic.getBoardByName(argv.boardName); |
| 61 | + boardId = boardObj.id; |
| 62 | + } |
| 63 | + |
| 64 | + const data = _.merge({}, { |
| 65 | + boardId, |
| 66 | + section: argv.cluster, |
| 67 | + color: argv.color, |
| 68 | + index: argv.index, |
| 69 | + name: argv.newName, |
| 70 | + }); |
| 71 | + |
| 72 | + if (!id) { |
| 73 | + if (!argv.name) throw Error('Nor section-id nor section-name was specified'); |
| 74 | + |
| 75 | + const sectionObj = await sectionLogic.getSectionByName({ boardId, name: argv.name }); |
| 76 | + id = sectionObj.id; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + await sectionLogic.updateSection(id, data); |
| 81 | + console.log(`Section: "${argv.name}" patched.`); |
| 82 | + }, |
| 83 | +}); |
| 84 | + |
| 85 | +module.exports = command; |
0 commit comments