Skip to content

Commit 20696df

Browse files
authored
Merge pull request #69 from mailscript/feat/set-edit
feat (cli): implement active and set commands
2 parents babb97a + 43f8565 commit 20696df

4 files changed

Lines changed: 301 additions & 0 deletions

File tree

docs/docs/cli/workflows.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
Configure your workflows
44

5+
### Index
6+
- [mailscript workflows:add](#mailscript-workflows:add)
7+
- [mailscript workflows:delete](#mailscript-workflows:delete)
8+
- [mailscript workflows:list](#mailscript-workflows:list)
9+
- [mailscript workflows:inspect](#mailscript-workflows:inspect)
10+
- [mailscript workflows:active](#mailscript-workflows:active)
11+
- [mailscript workflows:set](#mailscript-workflows:set)
12+
13+
514
## `mailscript workflows:add`
615

716
add a workflow
@@ -57,3 +66,36 @@ OPTIONS
5766
-v, --verbose Verbose
5867
5968
```
69+
## `mailscript workflows:active`
70+
71+
Retrieve whether a workflow is enabled or not; Optionally enable/disable a workflow.
72+
73+
```
74+
USAGE
75+
$ mailscript workflows:active ID [VALUE]
76+
77+
ARGUMENTS
78+
ID id of the workflow to be acted on
79+
VALUE "on" or "off"
80+
81+
OPTIONS
82+
-h, --help show CLI help
83+
84+
```
85+
## `mailscript workflows:set`
86+
87+
Set a property of a workflow
88+
89+
```
90+
USAGE
91+
$ mailscript workflows:set ID KEY VALUE
92+
93+
ARGUMENTS
94+
ID id of the workflow to be acted on
95+
KEY key of the property
96+
VALUE value of the property
97+
98+
OPTIONS
99+
-h, --help show CLI help
100+
-t, --type=type type of the value
101+
```

src/api.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export type AddWorkflowRequest = {
108108
trigger?: string
109109
action: string
110110
}
111+
export type KeyValuePair = {
112+
key: string
113+
value: any
114+
}
115+
export type SetWorkflowRequest = {
116+
id: string
117+
pairs: KeyValuePair[]
118+
}
111119
export type Workflow = {
112120
id: string
113121
name: string
@@ -117,6 +125,7 @@ export type Workflow = {
117125
input: string
118126
trigger: string
119127
action: string
128+
active?: boolean
120129
}
121130
export type GetAllWorkflowsResponse = {
122131
list: Workflow[]
@@ -676,6 +685,38 @@ export function addWorkflow(
676685
}),
677686
)
678687
}
688+
/**
689+
* Set workflow property
690+
*/
691+
export function setWorkflow(
692+
setWorkflowRequest: SetWorkflowRequest,
693+
opts?: Oazapfts.RequestOpts,
694+
) {
695+
return oazapfts.fetchJson<
696+
| {
697+
status: 204
698+
}
699+
| {
700+
status: 400
701+
data: ErrorResponse
702+
}
703+
| {
704+
status: 403
705+
data: ErrorResponse
706+
}
707+
| {
708+
status: 405
709+
data: ErrorResponse
710+
}
711+
>(
712+
'/workflows/set',
713+
oazapfts.json({
714+
...opts,
715+
method: 'POST',
716+
body: setWorkflowRequest,
717+
}),
718+
)
719+
}
679720
/**
680721
* Get all workflows you have access to
681722
*/

src/commands/workflows/active.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

src/commands/workflows/set.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 WorkflowsSet extends Command {
16+
static description = 'set a property on a workflow'
17+
18+
static flags = {
19+
help: flags.help({ char: 'h' }),
20+
type: flags.string({
21+
char: 't',
22+
description: 'type of the value',
23+
required: false,
24+
}),
25+
}
26+
27+
static args = [
28+
{
29+
name: 'id',
30+
required: true,
31+
description: 'id of the workflow to be acted on',
32+
},
33+
{
34+
name: 'key',
35+
required: true,
36+
description: 'key of the property',
37+
},
38+
{
39+
name: 'value',
40+
required: true,
41+
description: 'value of the property',
42+
},
43+
]
44+
45+
async run() {
46+
const { flags, args } = this.parse(WorkflowsSet)
47+
48+
const client = await setupApiClient()
49+
50+
if (!client) {
51+
this.exit(1)
52+
}
53+
54+
return this.set(client, flags, args)
55+
}
56+
57+
async set(client: typeof api, flags: FlagsType, args: any): Promise<void> {
58+
let value
59+
switch (flags.type) {
60+
case 'boolean': {
61+
value = Boolean(args.value)
62+
break
63+
}
64+
case 'number': {
65+
value = Number(args.value)
66+
break
67+
}
68+
case 'string': {
69+
value = args.value
70+
break
71+
}
72+
default: {
73+
value = args.value
74+
break
75+
}
76+
}
77+
const pairs: api.KeyValuePair = {
78+
key: args.key,
79+
value,
80+
}
81+
const payload: api.SetWorkflowRequest = {
82+
id: args.id,
83+
pairs: [pairs],
84+
}
85+
86+
return handle(
87+
client.setWorkflow(payload),
88+
withStandardErrors(
89+
{
90+
'201': () => {
91+
this.log(`Workflow property has been set`)
92+
},
93+
'403': ({ error }: api.ErrorResponse) => {
94+
this.log(chalk.red(`${chalk.bold('Error')}: ${error}`))
95+
this.exit(1)
96+
},
97+
'405': ({ error }: api.ErrorResponse) => {
98+
this.log(chalk.red(`${chalk.bold('Error')}: ${error}`))
99+
this.exit(1)
100+
},
101+
},
102+
this,
103+
),
104+
)
105+
}
106+
}

0 commit comments

Comments
 (0)