gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
557 stars 102 forks source link

Docker compose support for multiple env files (--env-file, compose_env_file) #553

Closed marcinhlybin closed 3 months ago

marcinhlybin commented 8 months ago

The docker-compose command includes the --env-file option, which accepts multiple files by allowing the option to be repeated. This is indicated in the command help as stringArray, meaning it can take an array of strings, each representing a path to an environment file.

To enhance functionality, a proposed update introduces the compose_env_files setting, which accepts a List[ValidPath]. This addition enables specifying multiple environment files directly in the configuration and it uses the same naming convention as for other options.

However, removing the existing compose_env_file setting would lead to breaking changes due to loss of backward compatibility.

A more user-friendly approach might be to retain compose_env_file for compatibility while expanding its capabilities to accept either a single path or a list of paths. This dual support ensures backward compatibility and introduces the flexibility of specifying multiple environment files, either through the new compose_env_files setting or by extending the functionality of the existing compose_env_file option.

diff --git a/python_on_whales/client_config.py b/python_on_whales/client_config.py
index 207961c..fb8b045 100644
--- a/python_on_whales/client_config.py
+++ b/python_on_whales/client_config.py
@@ -61,6 +61,7 @@ class ClientConfig:
     compose_files: List[ValidPath] = field(default_factory=list)
     compose_profiles: List[str] = field(default_factory=list)
     compose_env_file: Optional[ValidPath] = None
+    compose_env_files: List[ValidPath] = field(default_factory=list)
     compose_project_name: Optional[str] = None
     compose_project_directory: Optional[ValidPath] = None
     compose_compatibility: Optional[bool] = None
@@ -147,6 +148,7 @@ class ClientConfig:
         base_cmd.add_args_list("--file", self.compose_files)
         base_cmd.add_args_list("--profile", self.compose_profiles)
         base_cmd.add_simple_arg("--env-file", self.compose_env_file)
+        base_cmd.add_args_list("--env-file", self.compose_env_files)
         base_cmd.add_simple_arg("--project-name", self.compose_project_name)
         base_cmd.add_simple_arg("--project-directory", self.compose_project_directory)
         base_cmd.add_flag("--compatibility", self.compose_compatibility)
gabrieldemarmiesse commented 8 months ago

I would be happy with such a feature indeed, thanks for opening this issue. PR welcome!

We can add a warning for the old argument compose_env_file later on. Some old compose clients might rely on it.

einarwar commented 3 months ago

Sorry if i yoinked your PR, but seeing there was no activity since you raised this issue @marcinhlybin, I took the liberty of making a PR for this one at #616 :)