-
Notifications
You must be signed in to change notification settings - Fork 3
/
step.ts
120 lines (102 loc) · 3.42 KB
/
step.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {IConfig} from '@oclif/config'
import {flags} from '@oclif/parser'
import cli from 'cli-ux'
import * as debug from 'debug'
import * as inquirer from 'inquirer'
import * as uuidv4 from 'uuid/v4'
import {Step as StepRunner} from '../../models/step'
import {CogServiceClient} from '../../proto/cog_grpc_pb'
import {Step as ProtoStep} from '../../proto/cog_pb'
import {CogManager} from '../../services/cog-manager'
import StepAwareCommand from '../../step-aware-command'
// tslint:disable:ignore no-unused
export default class Step extends StepAwareCommand {
static description = 'Run a single Cog step interactively.'
static examples = [
'$ crank cog:step MyCog',
'$ crank cog:step MyCog --step=MyStepId'
]
static flags = {
'use-ssl': flags.boolean({
char: 's',
description: 'Use SSL to secure communications between crank and all cogs (useful for testing SSL support for cogs you are building).'
}),
step: flags.string({
description: 'The stepId of the step you wish to run'
}),
debug: flags.boolean({
description: 'More verbose output to aid in diagnosing issues using Crank',
})
}
static args = [{name: 'cogName', required: true}]
protected cogManager: CogManager
protected logDebug: debug.Debugger
constructor(argv: string[], config: IConfig) {
super(argv, config)
this.cogManager = new CogManager({registries: this.registry})
this.logDebug = debug('crank:step')
}
async init() {
const {flags} = this.parse(Step)
if (flags.debug) {
debug.enable('crank:*')
this.cogManager.setDebug(true)
}
}
async run() {
const {args, flags} = this.parse(Step)
const cogConfig = this.registry.getCogConfigFromRegistry(args.cogName)
let cogClient: CogServiceClient
if (!cogConfig || !cogConfig._runConfig || !cogConfig.stepDefinitionsList) {
this.error(`Couldn't find a Cog named ${args.cogName}`, {exit: false})
return this.exit(1)
}
let stepId = flags.step || ''
if (!stepId) {
this.logDebug('Prompting for step name')
const stepResponse: any = await inquirer.prompt({
name: 'step',
message: 'Step',
type: 'list',
choices: cogConfig.stepDefinitionsList.map(stepDef => {
return {value: stepDef.stepId, name: stepDef.name}
})
})
stepId = stepResponse.step
}
this.logDebug('Building protobuffer step for %s', stepId)
const protoStep: ProtoStep = await this.gatherStepInput(cogConfig, stepId)
this.coerceProtoStepTypes(protoStep, args.cogName)
try {
this.logDebug('Attempting to start Cog')
cogClient = await this.cogManager.startCogAndGetClient(cogConfig._runConfig, flags['use-ssl'])
} catch (e) {
this.log(`There was a problem starting Cog ${args.cogName}: ${e && e.message ? e.message : 'unknown error'}`)
this.log('You may need to reinstall it')
process.exitCode = 1
return
}
cli.action.start('Running')
const step = new StepRunner({
cog: args.cogName,
stepText: '',
client: cogClient,
registries: this.registry,
protoSteps: [protoStep],
scenarioId: uuidv4(),
})
this.log()
try {
await this.runStep(step, 2, false, true, flags.debug)
} catch (e) {
this.log()
cli.action.stop('Done')
return this.exit(1)
}
this.log()
cli.action.stop('Done')
}
async finally() {
this.cogManager.stopAllCogs()
}
}