Skip to content

Commit

Permalink
Add a warning if --env is passed to init (#629)
Browse files Browse the repository at this point in the history
* Add a warning if `--env` is passed to `init`

* Fix typo, `file` was doubled in init help

* Update docstrings for CLI

* Raise error if using `-i` with `init` subcommand

* Update docs to match current behaviour

* add test coverage

Co-authored-by: Bruno Rocha <rochacbruno@users.noreply.github.com>
Co-authored-by: Bruno Rocha <rochacbruno@gmail.com>
  • Loading branch information
3 people committed Aug 20, 2021
1 parent 827bd88 commit cf07219
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
61 changes: 26 additions & 35 deletions docs/cli.md
Expand Up @@ -18,61 +18,48 @@ Usage: dynaconf [OPTIONS] COMMAND [ARGS]...
Dynaconf - Command Line Interface
Documentation: https://dynaconf.com/
Options:
--version Show dynaconf version
--docs Open documentation in browser
--banner Show awesome banner
--version Show dynaconf version
--docs Open documentation in browser
--banner Show awesome banner
-i, --instance TEXT Custom instance of LazySettings
--help Show this message and exit.
--help Show this message and exit.
Commands:
init Inits a dynaconf project By default it...
list Lists all defined config values
init Inits a dynaconf project By default it creates a settings.toml...
list Lists all user defined config values and if `--all` is passed
it...
validate Validates Dynaconf settings based on rules defined in...
write Writes data to specific source
validate Validates based on dynaconf_validators.toml file
```

### dynaconf init

Use init to easily configure your application configuration, once dynaconf is installed go to the root directory of your application and run:

creates settings files in current directory

```
$ dynaconf -i init -v key=value -v foo=bar -s token=1234 -e production
$ dynaconf init -v key=value -v foo=bar -s token=1234
```

The above command will create in the current directory

`settings.toml`

```ini
[default]
KEY = "default"
FOO = "default"

[production]
KEY = "value"
FOO = "bar"
```

also `.secrets.toml`

```ini
[default]
TOKEN = "default"

[production]
TOKEN = "1234"
```

The command will also create a `.env` setting the working environment to **[production]**

```bash
ENV_FOR_DYNACONF="PRODUCTION"
```

And will include the `.secrets.toml` in the `.gitignore`
as well as `.gitignore` file ignoring the generated `.secrets.toml`

```ini
# Ignore dynaconf secret files
Expand All @@ -93,25 +80,26 @@ Usage: dynaconf init [OPTIONS]
This command must run on the project's root folder or you must pass
--path=/myproject/root/folder.
If you want to have a .env created with the ENV defined there e.g:
`ENV_FOR_DYNACONF=production` just pass --env=production and then .env
will also be created and the env defined to production.
The --env/-e is deprecated (kept for compatibility but unused)
Options:
-f, --format [ini|toml|yaml|json|py|env]
-p, --path TEXT defaults to current directory
-e, --env TEXT Sets the working env in `.env` file
-v, --vars TEXT extra values to write to settings file file
e.g: `dynaconf init -v NAME=foo -v X=2
-v, --vars TEXT extra values to write to settings file e.g:
`dynaconf init -v NAME=foo -v X=2
-s, --secrets TEXT secret key values to be written in .secrets
e.g: `dynaconf init -s TOKEN=kdslmflds
--wg / --no-wg
-y
--django TEXT
--help Show this message and exit.
```

Note that `-i`/`--instance` cannot be used with `init` as `-i` must point to an existing instance of the settings.

### dynaconf list

List all defined parameters and optionally export to a json file.
Expand Down Expand Up @@ -150,19 +138,22 @@ dynaconf list -o path/to/file.py --output-flat
### dynaconf write

```
Usage: dynaconf write [OPTIONS] TO
Usage: dynaconf write [OPTIONS] [ini|toml|yaml|json|py|redis|vault|env]
Writes data to specific source
Options:
-v, --vars TEXT key values to be written e.g: `dynaconf write toml
-e NAME=foo -e X=2
-v, --vars TEXT key values to be written e.g: `dynaconf write toml -e
NAME=foo -e X=2
-s, --secrets TEXT secret key values to be written in .secrets e.g:
`dynaconf write toml -s TOKEN=kdslmflds -s X=2
-p, --path TEXT defaults to current directory/settings.{ext}
-e, --env TEXT env to write to defaults to DEVELOPMENT for files for
external sources like Redis and Vault it will be
DYNACONF or the value set in $ENVVAR_PREFIX_FOR_DYNACONF
-y
--help Show this message and exit.
```
Expand Down
20 changes: 18 additions & 2 deletions dynaconf/cli.py
Expand Up @@ -39,6 +39,10 @@ def set_settings(ctx, instance=None):
settings = None

if instance is not None:
if ctx.invoked_subcommand in ["init"]:
raise click.UsageError(
"-i/--instance option is not allowed for `init` command"
)
sys.path.insert(0, ".")
settings = import_settings(instance)
elif "FLASK_APP" in os.environ: # pragma: no cover
Expand Down Expand Up @@ -206,7 +210,10 @@ def main(ctx, instance):
"--path", "-p", default=CWD, help="defaults to current directory"
)
@click.option(
"--env", "-e", default=None, help="Sets the working env in `.env` file"
"--env",
"-e",
default=None,
help="deprecated command (kept for compatibility but unused)",
)
@click.option(
"--vars",
Expand All @@ -216,7 +223,7 @@ def main(ctx, instance):
default=None,
help=(
"extra values to write to settings file "
"file e.g: `dynaconf init -v NAME=foo -v X=2"
"e.g: `dynaconf init -v NAME=foo -v X=2`"
),
)
@click.option(
Expand Down Expand Up @@ -251,6 +258,15 @@ def init(ctx, fileformat, path, env, _vars, _secrets, wg, y, django):
click.echo("-" * 42)
path = Path(path)

if env is not None:
click.secho(
"⚠️ The --env/-e option is deprecated (kept for\n"
" compatibility but unused)\n",
fg="red",
bold=True,
# stderr=True,
)

if settings.get("create_new_settings") is True:
filename = Path("config.py")
if not filename.exists():
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli.py
Expand Up @@ -35,6 +35,25 @@ def test_banner(clean_env):
assert "Learn more at:" in run(["--banner"])


def test_init_with_instance_raises(tmpdir):
result = run(
[
"-i",
"tests.test_cli.settings",
"init",
"--env",
"test",
f"--path={str(tmpdir)}",
]
)
assert "-i/--instance option is not allowed for `init` command" in result


def test_init_with_env_warns(tmpdir):
result = run(["init", "--env", "test", f"--path={str(tmpdir)}"])
assert "The --env/-e option is deprecated" in result


@pytest.mark.parametrize("fileformat", EXTS)
def test_init_with_path(fileformat, tmpdir):
# run twice to force load of existing files
Expand Down

0 comments on commit cf07219

Please sign in to comment.