-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (72 loc) · 2.56 KB
/
index.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
#!/usr/bin/env node
const execa = require('execa')
const { join } = require('path')
const { homedir } = require('os')
const chalk = require('chalk')
const prompt = require('prompts')
const { ConditionalCatch } = require('conditional-catch')
const { fileExistsAsync, readFileAsJsonAsync } = require('./helper')
const defaultKFile = require('./default-kfile')
const kFileName = '.kfile.json'
const kubectlExecutable = 'kubectl'
const kubectlConfigParameter = '--kubeconfig'
async function readConfigurationFromHomeDirectory () {
const path = join(homedir(), kFileName)
const kFileExists = await fileExistsAsync(path)
if (!kFileExists) {
throw new Error(`Configuration file not found at ${path}`)
}
return readFileAsJsonAsync(path)
}
function expandShorthands (kubeArguments, kFile) {
return kubeArguments
.reduce((acc, next) => [...acc, ...(kFile.shorthands[next] || [next])], [])
}
function parseCommandLineArgsFrom (argv, kFile) {
// First two are not needed, since they contain the
// executable- and filename
if (argv.length <= 2) throw new Error('No environment name provided')
const environmentName = argv[2]
const kubeArguments = expandShorthands(argv.splice(3), kFile)
return {
environmentName,
kubeArguments
}
}
const runAsync = async (argv) => {
const kFile = Object.assign({}, defaultKFile, await readConfigurationFromHomeDirectory())
const { environmentName, kubeArguments } = parseCommandLineArgsFrom(argv, kFile)
if (!Object.keys(kFile.environments).includes(environmentName)) {
throw new Error(`Environment "${environmentName}" not found in kfile`)
}
const environment = kFile.environments[environmentName]
console.log(chalk.green(`[k] Choosen environment '${environmentName}'`))
if (environment.requiresConfirmation) {
const { confirmed } = await prompt({
type: 'confirm',
name: 'confirmed',
message: `Please confirm the you want to execute "${chalk.blue(kubeArguments.join(' '))}" on ${chalk.red(environmentName)}`,
initial: false
})
if (!confirmed) {
return
}
}
try {
await execa(kubectlExecutable, [ kubectlConfigParameter, environment.kubeconfig, ...kubeArguments ], {
stderr: process.stderr,
stdin: process.stdin,
stdout: process.stdout
})
} catch (err) {
ConditionalCatch.createFrom(err)
.when(e => e.failed, () => {})
.handleOrThrow()
}
}
runAsync(process.argv)
.catch(err => {
console.error(chalk.red(`[k] errored with: ${err.message}`))
console.log(Object.keys(err).join(', '))
process.exit(err.code || 1)
})