forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
157 lines (142 loc) · 5.99 KB
/
commands.ts
File metadata and controls
157 lines (142 loc) · 5.99 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as fs from 'fs-extra';
import { parse } from 'yaml';
import { CommandText, CommandOption } from '../base/command';
import { ClusterVersion, FunctionContent, InvokeFunction } from './types';
export class Utils {
static async getFuncYamlContent(dir: string): Promise<FunctionContent> {
let funcData: FunctionContent;
try {
const funcYaml: string = await fs.readFile(path.join(dir, 'func.yaml'), 'utf-8');
funcData = parse(funcYaml) as FunctionContent;
} catch (error) {
// ignore
}
return funcData;
}
}
export class ServerlessCommand {
static invokeFunction(invokeFunData: InvokeFunction): CommandText {
const commandText = new CommandText('func', 'invoke', [
new CommandOption('-v')
]);
if (invokeFunData.id.length > 0) {
commandText.addOption(new CommandOption('--id', invokeFunData.id));
}
if (invokeFunData.path.length > 0) {
commandText.addOption(new CommandOption('-p', invokeFunData.path));
}
if (invokeFunData.contentType.length > 0) {
commandText.addOption(new CommandOption('--content-type', invokeFunData.contentType));
}
if (invokeFunData.format.length > 0) {
commandText.addOption(new CommandOption('-f', invokeFunData.format));
}
if (invokeFunData.source.length > 0) {
commandText.addOption(new CommandOption('--source', invokeFunData.source));
}
if (invokeFunData.type.length > 0) {
commandText.addOption(new CommandOption('--type', invokeFunData.type));
}
if (invokeFunData.data.length > 0) {
commandText.addOption(new CommandOption('--data', invokeFunData.data));
}
if (invokeFunData.file.length > 0) {
commandText.addOption(new CommandOption('--file', invokeFunData.file));
}
if (invokeFunData.enableURL && invokeFunData.invokeURL.length > 0) {
commandText.addOption(new CommandOption('-t', invokeFunData.invokeURL));
} else {
commandText.addOption(new CommandOption('-t', invokeFunData.instance));
}
return commandText
}
static createFunction(language: string, template: string, location: string): CommandText {
return new CommandText(`func create ${location}`, undefined, [
new CommandOption('-l', language),
new CommandOption('-t', template)
]);
}
static buildFunction(location: string, image: string, clusterVersion: ClusterVersion | null): CommandText {
const commandText = new CommandText('func', 'build', [
new CommandOption('-p', location),
new CommandOption('-i', image),
new CommandOption('-v')
]);
if (clusterVersion) {
commandText.addOption(new CommandOption('-r', ''))
}
return commandText
}
static runFunction(location: string, runBuild: boolean): CommandText {
const commandText = new CommandText('func', 'run', [
new CommandOption('-p', location),
new CommandOption('-b', runBuild.toString())
]);
return commandText;
}
static deployFunction(location: string,
image: string,
namespace: string,
clusterVersion: ClusterVersion | null): CommandText {
const commandText = new CommandText('func', 'deploy', [
new CommandOption('-p', location),
new CommandOption('-i', image),
new CommandOption('-v')
]);
if (namespace) {
commandText.addOption(new CommandOption('-n', namespace))
}
if (clusterVersion) {
commandText.addOption(new CommandOption('-r', ''))
}
return commandText;
}
static undeployFunction(name: string): CommandText {
const commandText = new CommandText('func', `delete ${name}`);
return commandText
}
static getClusterVersion(): CommandText {
return new CommandText('oc get clusterversion', undefined, [
new CommandOption('-o', 'josn')
]);
}
static config(functionPath: string, mode: string, isAdd: boolean): CommandText {
const option = isAdd ? mode === 'git' ? 'set' : 'add' : 'remove';
const commandText = new CommandText('func', 'config', [
new CommandOption(mode),
new CommandOption(option),
new CommandOption('-p', functionPath)
]);
return commandText;
}
static addRepo(name: string, gitURL: string): CommandText {
const commandText = new CommandText('func', 'repository');
commandText.addOption(new CommandOption('add'));
commandText.addOption(new CommandOption(name));
commandText.addOption(new CommandOption(gitURL));
return commandText;
}
static deleteRepo(name: string): CommandText {
const commandText = new CommandText('func', 'repository');
commandText.addOption(new CommandOption('remove'));
commandText.addOption(new CommandOption(name));
return commandText;
}
static list(): CommandText {
const commandText = new CommandText('func', 'repository');
commandText.addOption(new CommandOption('list'));
return commandText;
}
static renameRepo(oldName: string, newName: string): CommandText {
const commandText = new CommandText('func', 'repository');
commandText.addOption(new CommandOption('rename'));
commandText.addOption(new CommandOption(oldName));
commandText.addOption(new CommandOption(newName));
return commandText;
}
}