-
Notifications
You must be signed in to change notification settings - Fork 42
/
ValidationsHandler.ts
204 lines (193 loc) · 6.35 KB
/
ValidationsHandler.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as fs from 'fs-extra'
import { execSync } from 'child_process'
import { sync as commandExistsSync } from 'command-exists'
import StdOutUtil from './StdOutUtil'
import StorageHelper from './StorageHelper'
import Constants from './Constants'
import Utils from './Utils'
import { IAppDef } from '../models/AppDef'
const isWindows = process.platform === 'win32'
export function validateIsGitRepository() {
if (!fs.pathExistsSync('./.git')) {
StdOutUtil.printError(
'You are not in a git root directory: this command will only deploy the current directory.\n' +
'Run "caprover deploy --help" to know more deployment options... (e.g. tar file or image name)\n',
true
)
}
if (!commandExistsSync('git')) {
StdOutUtil.printError(
'"git" command not found: CapRover needs "git" to create tar file from your branch source files.\n' +
'Run "caprover deploy --help" to know more deployment options... (e.g. tar file or image name)\n',
true
)
}
}
export function validateDefinitionFile() {
if (!fs.pathExistsSync('./captain-definition')) {
if (fs.pathExistsSync('./Dockerfile')) {
StdOutUtil.printWarning('**** Warning ****')
StdOutUtil.printMessage(
'No captain-definition was found in main directory: falling back to Dockerfile.\n'
)
} else {
StdOutUtil.printWarning('**** Warning ****')
StdOutUtil.printMessage(
'No captain-definition was found in main directory: unless you have specified a special path for your captain-definition, this build will fail!\n'
)
}
} else {
let content = null
try {
content = JSON.parse(
fs.readFileSync('./captain-definition', 'utf8')
)
} catch (e) {
StdOutUtil.printError(
`captain-definition file is not a valid JSON!\n${e.message ||
e}\n`,
true
)
}
if (!content || !content.schemaVersion) {
StdOutUtil.printError(
'captain-definition needs "schemaVersion": please see docs!\n',
true
)
}
}
}
export function isNameValid(value: string): boolean {
return !!(value && value.match(/^[-\d\w]+$/i) && !value.includes('--'))
}
export function getErrorForIP(value: string): true | string {
value = value.trim()
if (value === Constants.SAMPLE_IP) {
return 'Enter a valid IP.'
}
if (!Utils.isIpAddress(value)) {
return `This is an invalid IP: ${value}.`
}
return true
}
export function getErrorForDomain(
value: string,
skipAlreadyStored?: boolean
): true | string {
if (value === Constants.SAMPLE_DOMAIN) {
return 'Enter a valid URL.'
}
const cleaned = Utils.cleanAdminDomainUrl(value)
if (!cleaned) {
return `This is an invalid URL: ${StdOutUtil.getColoredMachineUrl(
value
)}.`
}
if (!skipAlreadyStored) {
const found = StorageHelper.get()
.getMachines()
.find(
machine =>
Utils.cleanAdminDomainUrl(machine.baseUrl) === cleaned
)
if (found) {
return `${StdOutUtil.getColoredMachineUrl(
cleaned
)} already exist as ${StdOutUtil.getColoredMachineName(
found.name
)} in your currently logged in machines. If you want to replace the existing entry, you have to first use <logout> command, and then re-login.`
}
}
return true
}
export function getErrorForPassword(
value: string,
constraint?: number | string
): true | string {
if (!value || !value.trim()) {
return 'Please enter password.'
}
if (typeof constraint === 'number' && value.length < constraint) {
return `Password is too short, min ${constraint} characters.`
}
if (typeof constraint === 'string' && value !== constraint) {
return `Passwords do not match.`
}
return true
}
export function getErrorForMachineName(
value: string,
checkExisting?: boolean
): true | string {
value = value.trim()
const exist: boolean = StorageHelper.get().findMachine(value) ? true : false
if (exist && !checkExisting) {
return `${StdOutUtil.getColoredMachineName(
value
)} already exist. If you want to replace the existing entry, you have to first use <logout> command, and then re-login.`
}
if (checkExisting && !exist) {
return `${StdOutUtil.getColoredMachineName(
value
)} CapRover machine not exist.`
}
if (checkExisting || isNameValid(value)) {
return true
}
return 'Please enter a valid CapRover machine name: small letters, numbers, single hyphen.'
}
export function getErrorForAppName(
apps: IAppDef[],
value: string
): true | string {
value = value.trim()
const app = apps.find(a => a.appName === value)
if (!app) {
return `${StdOutUtil.getColoredAppName(
value
)} app not exist on this CapRover machine.`
}
if (app.isAppBuilding) {
return `${StdOutUtil.getColoredAppName(
value
)} app is currently in a building process.`
}
return true
}
export function getErrorForBranchName(value: string): true | string {
if (!value || !value.trim()) {
return 'Please enter branch name.'
}
value = value.trim()
try {
const cmd = isWindows
? execSync(`git rev-parse ${value} > NUL`)
: execSync(`git rev-parse ${value} 2>/dev/null`)
if (cmd) {
return true
}
} catch (e) {
// Do nothing
}
return `Cannot find hash of last commit on branch "${value}".`
}
export function getErrorForEmail(value: string): true | string {
if (!value || !value.trim()) {
return 'Please enter email.'
}
if (!Utils.isValidEmail(value)) {
return 'Please enter a valid email.'
}
return true
}
export function userCancelOperation(cancel: boolean, c?: boolean): boolean {
if (cancel) {
StdOutUtil.printMessage(
(c ? '\n' : '') +
'\nOperation cancelled by the user!' +
(!c ? '\n' : ''),
true
)
}
return false
}