Skip to content

Commit 9fa2ea1

Browse files
CopilotMaestroError
andcommitted
Refactor EnvFormatter to eliminate code duplication (DRY)
Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
1 parent 09f517c commit 9fa2ea1

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

src/mci/cli/envs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def extract_env_vars_from_schema(schema_path: str) -> dict[str, list[str]]:
8787
except Exception as e:
8888
# Warn but continue if toolset file is invalid
8989
console = Console()
90-
console.print(f"[yellow]⚠[/yellow] Warning: Could not load toolset '{toolset_name}': {e}", style="yellow")
90+
console.print(
91+
f"[yellow]⚠[/yellow] Warning: Could not load toolset '{toolset_name}': {e}",
92+
style="yellow",
93+
)
9194

9295
# Scan MCP servers
9396
mcp_servers = main_schema.get("mcp_servers", {})

src/mci/cli/formatters/env_formatter.py

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,26 @@ class EnvFormatter:
1616
"""
1717

1818
@staticmethod
19-
def format_to_file(env_vars: dict[str, list[str]], output_path: str | None = None) -> str:
19+
def format_to_string(env_vars: dict[str, list[str]]) -> str:
2020
"""
21-
Format environment variables and write to .env.example.mci file.
21+
Format environment variables as a string in .env format.
2222
2323
Args:
2424
env_vars: Dictionary mapping variable names to list of locations where used
25-
output_path: Optional custom output path (default: .env.example.mci)
2625
2726
Returns:
28-
Path to the generated file
27+
Formatted string in .env format
2928
3029
Example:
31-
>>> env_vars = {
32-
... "API_KEY": ["main", "weather-toolset"],
33-
... "DB_URL": ["database-toolset"]
34-
... }
35-
>>> path = EnvFormatter.format_to_file(env_vars)
36-
>>> print(path)
37-
.env.example.mci
30+
>>> env_vars = {"API_KEY": ["main"], "DB_URL": ["database-toolset"]}
31+
>>> print(EnvFormatter.format_to_string(env_vars))
32+
# .env.example.mci
33+
...
3834
"""
39-
if output_path is None:
40-
output_path = ".env.example.mci"
41-
4235
# Sort variables alphabetically
4336
sorted_vars = sorted(env_vars.keys())
4437

45-
# Generate file content
38+
# Generate content
4639
lines: list[str] = []
4740
lines.append("# .env.example.mci")
4841
lines.append("# Environment variables used in MCI configuration")
@@ -58,46 +51,33 @@ def format_to_file(env_vars: dict[str, list[str]], output_path: str | None = Non
5851
lines.append(f"{var_name}=")
5952
lines.append("")
6053

61-
# Write to file
62-
content = "\n".join(lines)
63-
Path(output_path).write_text(content)
64-
65-
return output_path
54+
return "\n".join(lines)
6655

6756
@staticmethod
68-
def format_to_string(env_vars: dict[str, list[str]]) -> str:
57+
def format_to_file(env_vars: dict[str, list[str]], output_path: str | None = None) -> str:
6958
"""
70-
Format environment variables as a string in .env format.
59+
Format environment variables and write to .env.example.mci file.
7160
7261
Args:
7362
env_vars: Dictionary mapping variable names to list of locations where used
63+
output_path: Optional custom output path (default: .env.example.mci)
7464
7565
Returns:
76-
Formatted string in .env format
66+
Path to the generated file
7767
7868
Example:
79-
>>> env_vars = {"API_KEY": ["main"], "DB_URL": ["database-toolset"]}
80-
>>> print(EnvFormatter.format_to_string(env_vars))
81-
# .env.example.mci
82-
...
69+
>>> env_vars = {
70+
... "API_KEY": ["main", "weather-toolset"],
71+
... "DB_URL": ["database-toolset"]
72+
... }
73+
>>> path = EnvFormatter.format_to_file(env_vars)
74+
>>> print(path)
75+
.env.example.mci
8376
"""
84-
# Sort variables alphabetically
85-
sorted_vars = sorted(env_vars.keys())
86-
87-
# Generate content
88-
lines: list[str] = []
89-
lines.append("# .env.example.mci")
90-
lines.append("# Environment variables used in MCI configuration")
91-
lines.append("#")
92-
lines.append("# Copy this file to .env.mci and fill in your values")
93-
lines.append("")
77+
if output_path is None:
78+
output_path = ".env.example.mci"
9479

95-
for var_name in sorted_vars:
96-
locations = env_vars[var_name]
97-
# Add comment showing where the variable is used
98-
location_str = ", ".join(sorted(locations))
99-
lines.append(f"# Used in: {location_str}")
100-
lines.append(f"{var_name}=")
101-
lines.append("")
80+
content = EnvFormatter.format_to_string(env_vars)
81+
Path(output_path).write_text(content)
10282

103-
return "\n".join(lines)
83+
return output_path

0 commit comments

Comments
 (0)