-
-
Save ashwch/317ce7d35dd605187bedf39e6b7858a8 to your computer and use it in GitHub Desktop.
Postman to Bruno environment converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # /// script | |
| # dependencies = [] | |
| # /// | |
| """ | |
| Simple Postman to Bruno environment converter. | |
| Usage: | |
| python migrate_postman_envs.py postman_env.json output_dir/ | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| # Characters that require quoting in Bruno | |
| SPECIAL_CHARS = " @!#$%:*=()<>&[]^{}" | |
| def convert_postman_to_bruno(postman_file, output_dir): | |
| """Convert a Postman environment JSON to Bruno .bru format.""" | |
| # Read Postman environment file | |
| try: | |
| with open(postman_file, 'r') as f: | |
| env_data = json.load(f) | |
| except Exception as e: | |
| print(f"❌ Error reading {postman_file}: {e}") | |
| return False | |
| # Extract environment name and create slug | |
| env_name = env_data.get("name", "unknown") | |
| env_slug = env_name.strip().lower().replace(" ", "_") | |
| # Build Bruno content | |
| bruno_content = f"""meta {{ | |
| name: {env_slug} | |
| }} | |
| vars {{ | |
| """ | |
| # Add variables | |
| for var in env_data.get("values", []): | |
| if not var.get("enabled", True): | |
| continue # Skip disabled vars | |
| key = var.get("key", "") | |
| value = str(var.get("value", "")) | |
| # Quote value if it contains special characters | |
| if value and not value.startswith("{{"): | |
| # Escape braces for Bruno format | |
| value = value.replace("{", "\\{").replace("}", "\\}") | |
| if any(c in SPECIAL_CHARS for c in value): | |
| value = f'"{value.replace('"', '\\"')}"' | |
| bruno_content += f" {key}: {value}\n" | |
| bruno_content += "}" | |
| # Write to output file | |
| output_path = Path(output_dir) | |
| output_path.mkdir(parents=True, exist_ok=True) | |
| output_file = output_path / f"{env_slug}.bru" | |
| try: | |
| with open(output_file, 'w') as f: | |
| f.write(bruno_content) | |
| print(f"✅ Converted {postman_file} → {output_file}") | |
| return True | |
| except Exception as e: | |
| print(f"❌ Error writing {output_file}: {e}") | |
| return False | |
| def main(): | |
| """Main function.""" | |
| if len(sys.argv) < 3: | |
| print("Usage: python migrate_postman_envs.py <input> <output_dir>") | |
| print("\nExamples:") | |
| print(" Single file: python migrate_postman_envs.py env.json ./bruno/environments/") | |
| print(" Folder: python migrate_postman_envs.py ./postman_envs/ ./bruno/environments/") | |
| sys.exit(1) | |
| input_path = Path(sys.argv[1]) | |
| output_dir = sys.argv[2] | |
| # Check if input exists | |
| if not input_path.exists(): | |
| print(f"❌ Error: {input_path} does not exist") | |
| sys.exit(1) | |
| # Collect JSON files to process | |
| json_files = [] | |
| if input_path.is_file(): | |
| if input_path.suffix.lower() == '.json': | |
| json_files = [input_path] | |
| else: | |
| print(f"❌ Error: {input_path} is not a JSON file") | |
| sys.exit(1) | |
| elif input_path.is_dir(): | |
| json_files = list(input_path.glob("*.json")) | |
| if not json_files: | |
| print(f"❌ Error: No JSON files found in {input_path}") | |
| sys.exit(1) | |
| # Process all files | |
| print(f"\n🔄 Processing {len(json_files)} file(s)...\n") | |
| success_count = 0 | |
| for json_file in json_files: | |
| if convert_postman_to_bruno(json_file, output_dir): | |
| success_count += 1 | |
| # Summary | |
| print(f"\n✨ Done! Converted {success_count}/{len(json_files)} file(s)") | |
| sys.exit(0 if success_count == len(json_files) else 1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment