-
Notifications
You must be signed in to change notification settings - Fork 67
/
CliCommand.service.ts
65 lines (57 loc) 路 2.17 KB
/
CliCommand.service.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
import Test from 'models/Test.model';
import {getServerBaseUrl} from 'utils/Common';
export enum CliCommandOption {
Wait = 'wait',
UseHostname = 'useHostname',
UseCurrentEnvironment = 'useCurrentEnvironment',
// UseId = 'useId',
GeneratesJUnit = 'generateJUnit',
useDocker = 'useDocker',
}
export enum CliCommandFormat {
Pretty = 'pretty',
Json = 'json',
}
export type TCliCommandEnabledOptions = Record<CliCommandOption, boolean>;
export type TCliCommandConfig = {
options: TCliCommandEnabledOptions;
format: CliCommandFormat;
environmentId?: string;
test: Test;
fileName: string;
};
type TApplyProps = {
command: string;
test: Test;
environmentId?: string;
enabled: boolean;
};
type TApplyOption = (props: TApplyProps) => string;
const CliCommandService = () => ({
applyOptions: {
[CliCommandOption.Wait]: ({command, enabled}) => (enabled ? `${command} --wait-for-result` : command),
[CliCommandOption.UseHostname]: ({command, enabled}) => {
const baseUrl = getServerBaseUrl();
return enabled ? `${command} -s ${baseUrl}` : command;
},
[CliCommandOption.UseCurrentEnvironment]: ({command, enabled, environmentId}) =>
enabled && environmentId ? `${command} -e ${environmentId}` : command,
// [CliCommandOption.UseId]: ({enabled, command, test: {id}}) => `${command} -d ${enabled ? id : `${id}.yaml`}`,
[CliCommandOption.GeneratesJUnit]: ({command, enabled}) => (enabled ? `${command} --junit result.junit` : command),
[CliCommandOption.useDocker]: ({enabled, command}) =>
`${
enabled
? 'docker run --rm -it -v$(pwd):$(pwd) -w $(pwd) --network host --entrypoint tracetest kubeshop/tracetest:latest -s http://localhost:11633/'
: 'tracetest'
} ${command}`,
} as Record<CliCommandOption, TApplyOption>,
getCommand({options, format, test, environmentId, fileName}: TCliCommandConfig) {
const command = Object.entries(options).reduce(
(acc, [option, enabled]) =>
this.applyOptions[option as CliCommandOption]({command: acc, enabled, test, environmentId}),
`test run -d ${fileName}`
);
return `${command} -o ${format}`;
},
});
export default CliCommandService();