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

Add possibility to pass variables from command line. #57

Merged
merged 3 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ Some applications load from `.env`, `.env.local`, `.env.development` and `.env.d
(see [#37](https://github.com/entropitor/dotenv-cli/issues/37) for more information).
`dotenv-cli` supports this using the `-c` flag for just `.env` and `.env.local` and `-c development` for the ones above.

### Setting variable from command line
It is possible to set variable directly from command line using the -v flag:
```bash
$ dotenv -v VARIABLE=somevalue <command with arguments>
```

Multiple variables can be specified:
```bash
$ dotenv -v VARIABLE1=somevalue1 -v VARIABLE2=somevalue2 <command with arguments>
```

Variables set up from command line have higher priority than from env files.

> Purpose of this is that standard approach `VARIABLE=somevalue <command with arguments>` doesn't work on Windows. The -v flag works on all the platforms.

### Check env variable
If you want to check the value of an environment variable, use the `-p` flag
```bash
Expand Down
25 changes: 24 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ var dotenvExpand = require('dotenv-expand')

function printHelp () {
console.log([
'Usage: dotenv [--help] [--debug] [-e <path>] [-p <variable name>] [-c [environment]] [-- command]',
'Usage: dotenv [--help] [--debug] [-e <path>] [-v <name>=<value>] [-p <variable name>] [-c [environment]] [-- command]',
' --help print help',
' --debug output the files that would be processed but don\'t actually parse them or run the `command`',
' -e <path> parses the file <path> as a `.env` file and adds the variables to the environment',
' -e <path> multiple -e flags are allowed',
' -v <name>=<value> put variable <name> into environment using value <value>',
' -v <name>=<value> multiple -v flags are allowed',
' -p <variable> print value of <variable> to the console. If you specify this, you do not have to specify a `command`',
' -c [environment] support cascading env variables from `.env`, `.env.local`, `.env.<environment>`, `.env.<environment>.local` files',
' command `command` is the actual command you want to run. Best practice is to precede this command with ` -- `. Everything after `--` is considered to be your command. So any flags will not be parsed by this tool but be passed to your command. If you do not do it, this tool will strip those flags'
Expand Down Expand Up @@ -45,14 +47,35 @@ if (argv.c) {
), [])
}


function validateCmdVariable(param){
const indexOfEqualSign = param.indexOf('=')
if(indexOfEqualSign === -1 || indexOfEqualSign === 0 || indexOfEqualSign === param.length - 1){
console.error('Unexpected argument ' + param + '. Expected variable in format variable=value')
process.exit(1)
}
return param
}
var variables = []
if (argv.v) {
if(typeof argv.v === 'string')
variables.push(validateCmdVariable(argv.v))
else
variables.push(...argv.v.map(validateCmdVariable))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you follow the code style and put braces around these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I update PR to reflect code style in project.

}
var parsed = dotenv.parse(Buffer.from(variables.join('\n')))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put this closer to line 78? and maybe name parsedVariables or something? It feels a bit generic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the variable, but I didn't want to move it down, as I believe debug option should print the variables. And it should be after line 73 to overwrite variables from env files.



if (argv.debug) {
console.log(paths)
console.log(parsed)
process.exit()
}

paths.forEach(function (env) {
dotenvExpand(dotenv.config({ path: path.resolve(env) }))
})
Object.assign(process.env, parsed)

if (argv.p) {
var value = process.env[argv.p]
Expand Down