-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcs.js
67 lines (58 loc) · 1.54 KB
/
dcs.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
#!/usr/bin/env node
const ipt = require('ipt')
const { dockerCommand } = require('docker-cli-js')
const options = {
machineName: null,
currentWorkingDirectory: null,
echo: false
}
const getContexts = async () => {
const data = await dockerCommand('context ls', options)
const raw = data.raw
const split = raw.split('\n')
const contexts = []
for (let i = 0; i < split.length; i++) {
const context = split[i]
if (context !== '') {
const chunck = context.split(/\s+/)
if (chunck[0] !== 'NAME') {
if (chunck[1] === '*') {
contexts.push(chunck[0] + ' *DEFAULT*')
} else {
contexts.push(chunck[0])
}
}
}
}
return contexts
}
(async () => {
const contexts = await getContexts()
const choices = contexts.map((el) => ({
name: `Set context > ${el}`,
value: `context use ${el}`
}))
ipt(
[...choices],
{
message: 'Select docker context you want to use as default',
autocomplete: true,
multiple: false,
size: choices.length
})
.then(async (command) => {
if (`"${command}"`.includes('*DEFAULT*')) {
console.warn('\x1b[41m%s\x1b[0m', 'CONTEXT ALREADY AS DEFAULT')
return
}
await dockerCommand(`"${command}"`, options)
// await dockerCommand('context ls', { ...options, echo: true })
const check = await getContexts()
check.forEach(item => {
if (item.includes('*DEFAULT*')) {
console.warn('\x1b[41m%s\x1b[0m', item)
}
})
})
.catch(() => {})
})()