-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconnectViaServiceKey.js
186 lines (174 loc) · 4.8 KB
/
connectViaServiceKey.js
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
// @ts-check
import * as base from '../utils/base.js'
export const command = 'serviceKey [instance] [key]'
export const aliases = ['key', 'servicekey', 'service-key']
export const describe = base.bundle.getText("serviceKey")
export const builder = base.getBuilder({
instance: {
alias: ['i', 'Instance'],
desc: base.bundle.getText("instance")
},
key: {
alias: ['k', 'key'],
desc: base.bundle.getText("key")
},
encrypt: {
alias: ['e', 'Encrypt', 'ssl'],
desc: base.bundle.getText("encrypt"),
type: 'boolean',
default: true
},
validate: {
alias: ['v', 'Validate', 'validateCertificate'],
desc: base.bundle.getText("validate"),
type: 'boolean',
default: false
},
cf: {
alias: ['c', 'cmd'],
desc: base.bundle.getText("cfxs"),
type: 'boolean',
default: true
},
save: {
alias: ['s', 'Save'],
desc: base.bundle.getText("save2"),
type: 'boolean',
default: true
}
}, false)
export function handler (argv) {
base.promptHandler(argv, setKeyDetails, {
instance: {
description: base.bundle.getText("instance"),
required: true
},
key: {
description: base.bundle.getText("key"),
required: true
},
encrypt: {
description: base.bundle.getText("encrypt"),
type: 'boolean',
default: true,
required: false
},
validate: {
description: base.bundle.getText("validate"),
type: 'boolean',
default: false,
required: false
},
cf: {
description: base.bundle.getText("cfxs"),
type: 'boolean',
default: true,
required: false
},
save: {
description: base.bundle.getText("save2"),
type: 'boolean',
required: true
}
}, false)
}
export async function setKeyDetails(input) {
base.debug('setKeyDetails')
base.debug(input)
const { default:child_process } = await import('child_process')
let child = child_process.exec
//create serviceKey
try {
let script = ''
if (input.cf) {
script = `cf create-service-key ${input.instance} ${input.key}`
} else {
script = `xs create-service-key ${input.instance} ${input.key}`
}
child(script, (err) => {
if (err) {
return base.error(err)
}
console.log(`Service Key ${input.key} created`)
let script = ''
if (input.cf) {
script = `cf service-key ${input.instance} ${input.key}`
} else {
script = `xs service-key ${input.instance} ${input.key}`
}
child(script, (err, stdout) => {
if (err) {
return base.error(err)
} else {
let lines = stdout.split('\n')
console.log(lines[0])
lines.splice(0, 2)
if (!input.cf) {
lines.splice(-3, 3)
}
let newtext = lines.join('\n')
let returnContent = JSON.parse(newtext)
if (input.save) {
saveEnv(returnContent, input)
}
}
})
return base.end()
})
} catch (error) {
base.error(error)
}
}
export async function saveEnv(options, input) {
base.debug('saveEnv')
let defaultEnv = {}
defaultEnv.VCAP_SERVICES = {}
let credentials
if(options["credentials"]){
credentials = options.credentials
}else{
credentials = options
}
defaultEnv.VCAP_SERVICES.hana = [{
name: input.instance,
instance_name: input.instance,
label: "hana",
tags: [
"hana",
"database",
"relational"
],
plan: "hdi-shared",
credentials: credentials
}]
defaultEnv.VCAP_SERVICES.hana[0].credentials.encrypt = input.encrypt
//defaultEnv.VCAP_SERVICES.hana[0].credentials.sslCryptoProvider = 'openssl'
if (input.validate) {
defaultEnv.VCAP_SERVICES.hana[0].credentials.sslValidateCertificate = true
} else {
defaultEnv.VCAP_SERVICES.hana[0].credentials.sslValidateCertificate = false
delete defaultEnv.VCAP_SERVICES.hana[0].credentials.certificate
}
base.debug(defaultEnv.VCAP_SERVICES)
try {
const db = await base.createDBConnection(defaultEnv.VCAP_SERVICES.hana[0].credentials) //options)
let results = await db.execSQL(`SELECT CURRENT_USER AS "Current User", CURRENT_SCHEMA AS "Current Schema" FROM DUMMY`);
base.outputTableFancy(results)
let resultsSession = await db.execSQL(`SELECT * FROM M_SESSION_CONTEXT WHERE CONNECTION_ID = (SELECT SESSION_CONTEXT('CONN_ID') FROM "DUMMY")`);
base.outputTableFancy(resultsSession)
} catch (error) {
base.error(error)
}
if (input.save) {
const { default:fs } = await import('fs')
fs.writeFile("default-env.json", JSON.stringify(defaultEnv, null, '\t'), (err) => {
if (err) {
throw new Error(`${base.bundle.getText("errConn")} ${JSON.stringify(err)}`)
}
console.log(base.bundle.getText("saved"))
base.end()
})
} else {
base.end()
}
}