|
| 1 | +/* eslint-disable complexity */ |
| 2 | +import { Command, flags } from '@oclif/command' |
| 3 | +import chalk from 'chalk' |
| 4 | +import { handle } from 'oazapfts' |
| 5 | +import * as api from '../../api' |
| 6 | +import setupApiClient from '../../setupApiClient' |
| 7 | +import withStandardErrors from '../../utils/errorHandling' |
| 8 | + |
| 9 | +type FlagsType = { |
| 10 | + type?: string |
| 11 | + |
| 12 | + [key: string]: any |
| 13 | +} |
| 14 | + |
| 15 | +export default class WorkflowsActive extends Command { |
| 16 | + static description = 'toggle a workflow on or off' |
| 17 | + |
| 18 | + static flags = { |
| 19 | + help: flags.help({ char: 'h' }), |
| 20 | + } |
| 21 | + |
| 22 | + static args = [ |
| 23 | + { |
| 24 | + name: 'id', |
| 25 | + required: true, |
| 26 | + description: 'id of the workflow to be acted on', |
| 27 | + }, |
| 28 | + { |
| 29 | + name: 'value', |
| 30 | + required: false, |
| 31 | + description: '"on" or "off"', |
| 32 | + }, |
| 33 | + ] |
| 34 | + |
| 35 | + async run() { |
| 36 | + const { flags, args } = this.parse(WorkflowsActive) |
| 37 | + |
| 38 | + const client = await setupApiClient() |
| 39 | + |
| 40 | + if (!client) { |
| 41 | + this.exit(1) |
| 42 | + } |
| 43 | + |
| 44 | + return this.set(client, flags, args) |
| 45 | + } |
| 46 | + |
| 47 | + async set(client: typeof api, flags: FlagsType, args: any): Promise<void> { |
| 48 | + if (!args.value) { |
| 49 | + const workflow = ( |
| 50 | + await handle( |
| 51 | + client.getAllWorkflows(), |
| 52 | + withStandardErrors( |
| 53 | + { '200': ({ list }: api.GetAllWorkflowsResponse) => list }, |
| 54 | + this, |
| 55 | + ), |
| 56 | + ) |
| 57 | + ).find(({ id }: any) => id === args.id) |
| 58 | + if (workflow.active === true) { |
| 59 | + this.log('This workflow is active') |
| 60 | + } else if (workflow.active === false) { |
| 61 | + this.log('This workflow is not active') |
| 62 | + } else { |
| 63 | + // default to active if no status is set. |
| 64 | + this.log('This workflow is active') |
| 65 | + } |
| 66 | + |
| 67 | + return |
| 68 | + } |
| 69 | + let value |
| 70 | + switch (args.value) { |
| 71 | + case 'on': { |
| 72 | + value = true |
| 73 | + break |
| 74 | + } |
| 75 | + case 'off': { |
| 76 | + value = false |
| 77 | + break |
| 78 | + } |
| 79 | + default: { |
| 80 | + this.log(`Invalid input ${args.value}`) |
| 81 | + } |
| 82 | + } |
| 83 | + const pairs: api.KeyValuePair = { |
| 84 | + key: 'active', |
| 85 | + value, |
| 86 | + } |
| 87 | + const payload: api.SetWorkflowRequest = { |
| 88 | + id: args.id, |
| 89 | + pairs: [pairs], |
| 90 | + } |
| 91 | + |
| 92 | + return handle( |
| 93 | + client.setWorkflow(payload), |
| 94 | + withStandardErrors( |
| 95 | + { |
| 96 | + '201': () => { |
| 97 | + this.log(`Workflow property has been set to ${args.value}`) |
| 98 | + }, |
| 99 | + '403': ({ error }: api.ErrorResponse) => { |
| 100 | + this.log(chalk.red(`${chalk.bold('Error')}: ${error}`)) |
| 101 | + this.exit(1) |
| 102 | + }, |
| 103 | + '405': ({ error }: api.ErrorResponse) => { |
| 104 | + this.log(chalk.red(`${chalk.bold('Error')}: ${error}`)) |
| 105 | + this.exit(1) |
| 106 | + }, |
| 107 | + }, |
| 108 | + this, |
| 109 | + ), |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
0 commit comments