This repository has been archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.ts
98 lines (85 loc) · 2.82 KB
/
execute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {flags} from '@oclif/command'
import {readFileSync} from 'fs'
import {ExecutionCreateOutputs} from 'mesg-js/lib/api'
import * as base58 from 'mesg-js/lib/util/base58'
import Command from '../../root-command'
import instanceResolver from '../../utils/instance-resolver'
import ServiceDetail from './detail'
export default class ServiceExecute extends Command {
static description = 'Execute a task on a running service'
static flags = {
...Command.flags,
json: flags.string({char: 'j', description: 'Path to a JSON file containing the task inputs'}),
data: flags.string({
char: 'd',
description: 'Task inputs',
multiple: true,
helpValue: 'key=value'
}),
}
static args = [{
name: 'INSTANCE_HASH',
required: true,
}, {
name: 'TASK',
required: true,
description: 'Task key'
}]
async run(): ExecutionCreateOutputs {
const {args, flags} = this.parse(ServiceExecute)
const instanceHash = await instanceResolver(this.api, args.INSTANCE_HASH)
const instance = await this.api.instance.get({
hash: instanceHash
})
if (!instance.serviceHash) { throw new Error('invalid service hash') }
const service = await ServiceDetail.run([base58.encode(instance.serviceHash), '--silent', ...this.flagsAsArgs(flags)])
const task = service.tasks.find((x: any) => x.key === args.TASK)
if (!task) {
throw new Error(`The task ${args.TASK} does not exist in service`)
}
const inputs = this.convertValue(task.inputs, this.dataFromFlags(flags))
const result = await this.execute({
inputs,
instanceHash,
tags: ['CLI'],
taskKey: args.TASK
})
this.styledJSON(result)
return result
}
dataFromFlags(flags: { data: string[], json: string | undefined }): any {
if (flags.json) {
return JSON.parse(readFileSync(flags.json).toString())
}
return (flags.data || []).reduce((acc, item) => {
const [key, value] = item.split('=')
return {
...acc,
[key]: value
}
}, {})
}
convertValue(inputs: any, data: any): any {
if (!inputs) return {}
return inputs
.filter((x: any) => data[x.key] !== undefined && data[x.key] !== null)
.reduce((prev: any, value: any) => ({
...prev,
[value.key]: this.convert(value.type, data[value.key])
}), {})
}
convert(type: 'Object' | 'String' | 'Boolean' | 'Number' | 'Any', value: string | any): any {
try {
return {
Object: (x: string | any) => typeof x === 'string' ? JSON.parse(x) : x,
String: (x: string) => x,
Boolean: (x: string) => ['true', 't', 'TRUE', 'T', '1'].includes(x),
Number: (x: string) => parseFloat(x),
Any: (x: string) => x,
}[type](value)
} catch {
this.warn(`Cannot parse ${value} in ${type}`)
return value
}
}
}