Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow Whitespace in CLI Variable Values #105

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node

var spawn = require('cross-spawn')
var path = require('path')
const spawn = require('cross-spawn')
const path = require('path')

var argv = require('minimist')(process.argv.slice(2))
var dotenv = require('dotenv')
var dotenvExpand = require('dotenv-expand').expand
const argv = require('minimist')(process.argv.slice(2))
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand').expand

function printHelp () {
console.log([
Expand All @@ -29,14 +29,14 @@ if (argv.help) {
process.exit()
}

const override = argv.o || argv.override;
const override = argv.o || argv.override

if (argv.c && override) {
console.error('Invalid arguments. Cascading env variables conflicts with overrides.')
process.exit(1)
}

var paths = []
let paths = []
if (argv.e) {
if (typeof argv.e === 'string') {
paths.push(argv.e)
Expand All @@ -56,22 +56,23 @@ if (argv.c) {
}

function validateCmdVariable (param) {
if (!param.match(/^\w+=[a-zA-Z0-9"=^!?%@_&\-/:;.]+$/)) {
console.error('Unexpected argument ' + param + '. Expected variable in format variable=value')
const [, key, val] = param.match(/^(\w+)=([\s\S]+)$/m) || []
if (!key || !val) {
console.error(`Invalid variable name. Expected variable in format '-v variable=value', but got: \`-v ${param}\`.`)
process.exit(1)
}

return param
return [key, val]
}
var variables = []
const variables = []
if (argv.v) {
if (typeof argv.v === 'string') {
variables.push(validateCmdVariable(argv.v))
} else {
variables.push(...argv.v.map(validateCmdVariable))
}
}
var parsedVariables = dotenv.parse(Buffer.from(variables.join('\n')))
const parsedVariables = Object.fromEntries(variables)

if (argv.debug) {
console.log(paths)
Expand All @@ -80,20 +81,23 @@ if (argv.debug) {
}

paths.forEach(function (env) {
var parsedFile = dotenv.config({ path: path.resolve(env), override })
const parsedFile = dotenv.config({ path: path.resolve(env), override })
if (argv.expand !== false) {
dotenvExpand(parsedFile)
}
})
Object.assign(process.env, parsedVariables)

if (argv.p) {
var value = process.env[argv.p]
const value = process.env[argv.p]
if (typeof value === 'string') {
value = `\`${value}\``
}
console.log(value != null ? value : '')
process.exit()
}

var command = argv._[0]
const command = argv._[0]
if (!command) {
printHelp()
process.exit(1)
Expand Down