Skip to content

refactor: consolidate _build_args_str to utils.shell, fix env file inline comment parsing#2

Open
thor-guardrail wants to merge 1 commit into
digitalforgeca:mainfrom
thor-guardrail:fix/build-args-dedup-env-file-comments
Open

refactor: consolidate _build_args_str to utils.shell, fix env file inline comment parsing#2
thor-guardrail wants to merge 1 commit into
digitalforgeca:mainfrom
thor-guardrail:fix/build-args-dedup-env-file-comments

Conversation

@thor-guardrail
Copy link
Copy Markdown

Summary

Two related code quality improvements found during review.


1. Deduplicate _build_args_str()

Severity: Code quality / DRY violation

_build_args_str() was defined identically in two places:

  • dds/providers/azure/container.py
  • dds/builders/docker.py

Both functions had the same signature, same logic, same docstring. No single-source-of-truth. Any future fix to one would need to be applied to both.

Fix: Moved the canonical (and now shell-safe — see PR #1) implementation to dds/utils/shell.build_args_str(). Both original sites now import and delegate. Module-level _build_args_str aliases are kept for backward compatibility.


2. load_env_file() doesn't strip inline comments from unquoted values

Severity: Bug / correctness

A line like:

DATABASE_URL=postgres://localhost/mydb  # local dev only

...was being parsed as the value postgres://localhost/mydb # local dev only. This would silently produce an invalid connection string at runtime.

Standard .env file semantics (used by dotenv, python-dotenv, godotenv, etc.) strip inline comments from unquoted values. Quoted values are untouched — the comment is part of the value when inside quotes.

Fix: After quote-stripping, scan unquoted values for # or \t# separator and truncate there.

Note: Quoted values with a # inside them are still handled correctly:

PASSWORD="hunter#2"   # ← value is hunter#2, not hunter
COMMENT="hello"       # world  ← value is hello

Files changed

  • dds/utils/shell.py — new build_args_str()
  • dds/providers/azure/container.py — delegates to shared util
  • dds/builders/docker.py — delegates to shared util
  • dds/secrets.py — inline comment stripping

…line comment parsing

Two improvements:

1. _build_args_str() was duplicated identically in both
   dds/providers/azure/container.py and dds/builders/docker.py.
   Moved the canonical implementation to dds/utils/shell.build_args_str()
   (with shell-quoting via shlex.quote — see PR digitalforgeca#1 for the safety fix).
   Both original sites now import and delegate to the shared version.
   Module-level _build_args_str aliases kept for backward compatibility.

2. load_env_file() in dds/secrets.py did not strip inline comments from
   unquoted values, so a line like:
     DATABASE_URL=postgres://localhost/mydb  # local dev only
   would produce 'postgres://localhost/mydb  # local dev only' as the value.
   This is incorrect — standard .env file parsers (dotenv et al.) strip
   inline comments from unquoted values. Quoted values are left untouched
   (the comment is considered part of the value when quoted).

   Added stripping for ' #' and '\t#' comment separators on unquoted values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant