mirror of
https://github.com/basnijholt/compose-farm.git
synced 2026-02-03 14:13:26 +00:00
cli: Default init-env output to current directory (#160)
Previously `cf config init-env` created the .env file next to the compose-farm.yaml config file. This was unintuitive when working in stack subdirectories - users expected the file in their current directory. Now the default is to create .env in the current working directory, which matches typical CLI tool behavior. Use `-o /path/to/.env` to specify a different location.
This commit is contained in:
@@ -693,11 +693,11 @@ cf config symlink
|
||||
# Create symlink to specific file
|
||||
cf config symlink /opt/compose-farm/config.yaml
|
||||
|
||||
# Generate .env file for Docker deployment
|
||||
# Generate .env file in current directory
|
||||
cf config init-env
|
||||
|
||||
# Generate .env in current directory
|
||||
cf config init-env -o .env
|
||||
# Generate .env at specific path
|
||||
cf config init-env -o /opt/stacks/.env
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -17,7 +17,7 @@ curl -O https://raw.githubusercontent.com/basnijholt/compose-farm/main/docker-co
|
||||
**2. Generate `.env` file:**
|
||||
|
||||
```bash
|
||||
cf config init-env -o .env
|
||||
cf config init-env
|
||||
```
|
||||
|
||||
This auto-detects settings from your `compose-farm.yaml`:
|
||||
|
||||
@@ -342,7 +342,7 @@ def config_init_env(
|
||||
output: Annotated[
|
||||
Path | None,
|
||||
typer.Option(
|
||||
"--output", "-o", help="Output .env file path. Defaults to .env in config directory."
|
||||
"--output", "-o", help="Output .env file path. Defaults to .env in current directory."
|
||||
),
|
||||
] = None,
|
||||
force: _ForceOption = False,
|
||||
@@ -357,14 +357,14 @@ def config_init_env(
|
||||
|
||||
Example::
|
||||
|
||||
cf config init-env # Create .env next to config
|
||||
cf config init-env -o .env # Create .env in current directory
|
||||
cf config init-env # Create .env in current directory
|
||||
cf config init-env -o /path/to/.env # Create .env at specific path
|
||||
|
||||
"""
|
||||
config_file, cfg = _load_config_with_path(path)
|
||||
|
||||
# Determine output path
|
||||
env_path = output.expanduser().resolve() if output else config_file.parent / ".env"
|
||||
# Determine output path (default: current directory)
|
||||
env_path = output.expanduser().resolve() if output else Path.cwd() / ".env"
|
||||
|
||||
if env_path.exists() and not force:
|
||||
console.print(f"[yellow].env file already exists:[/] {env_path}")
|
||||
|
||||
@@ -370,7 +370,7 @@ class TestConfigInitEnv:
|
||||
assert "Aborted" in result.stdout
|
||||
assert env_file.read_text() == "KEEP_THIS=true"
|
||||
|
||||
def test_init_env_defaults_to_config_dir(
|
||||
def test_init_env_defaults_to_current_dir(
|
||||
self,
|
||||
runner: CliRunner,
|
||||
tmp_path: Path,
|
||||
@@ -378,12 +378,20 @@ class TestConfigInitEnv:
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.delenv("CF_CONFIG", raising=False)
|
||||
config_file = tmp_path / "compose-farm.yaml"
|
||||
config_dir = tmp_path / "config"
|
||||
config_dir.mkdir()
|
||||
config_file = config_dir / "compose-farm.yaml"
|
||||
config_file.write_text(yaml.dump(valid_config_data))
|
||||
|
||||
# Create a separate working directory
|
||||
work_dir = tmp_path / "workdir"
|
||||
work_dir.mkdir()
|
||||
monkeypatch.chdir(work_dir)
|
||||
|
||||
result = runner.invoke(app, ["config", "init-env", "-p", str(config_file)])
|
||||
|
||||
assert result.exit_code == 0
|
||||
# Should create .env in same directory as config
|
||||
env_file = tmp_path / ".env"
|
||||
# Should create .env in current directory, not config directory
|
||||
env_file = work_dir / ".env"
|
||||
assert env_file.exists()
|
||||
assert not (config_dir / ".env").exists()
|
||||
|
||||
Reference in New Issue
Block a user