Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
247 lines (195 loc) · 7.75 KB
/
Copy pathcli.py
File metadata and controls
247 lines (195 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
"""
Command-line interface for Email Routing Manager.
Provides both legacy compatibility and new OOP interface.
"""
import sys
import asyncio
import argparse
from pathlib import Path
def setup_parser():
"""Set up the command line argument parser."""
parser = argparse.ArgumentParser(
description="Cloudflare Email Routing Manager",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s create dest@example.com 10 # Create 10 email rules
%(prog)s update new@example.com # Update all destinations
%(prog)s delete --filter temp # Delete rules with 'temp' in name
%(prog)s list --filter routing # List rules with 'routing' in name
%(prog)s print results.json # Print mappings from file
"""
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Create command
create_parser = subparsers.add_parser('create', help='Create email rules')
create_parser.add_argument('destination', help='Destination email address')
create_parser.add_argument('count', type=int, help='Number of rules to create (1-200)')
create_parser.add_argument('--length', type=int, default=10, help='Email prefix length (default: 10)')
create_parser.add_argument('--delay', type=float, default=0.5, help='Delay between requests (default: 0.5)')
# Update command
update_parser = subparsers.add_parser('update', help='Update destination emails')
update_parser.add_argument('destination', help='New destination email address')
update_parser.add_argument('--filter', help='Filter rules by name')
update_parser.add_argument('--delay', type=float, default=0.5, help='Delay between requests (default: 0.5)')
# Delete command
delete_parser = subparsers.add_parser('delete', help='Delete email rules')
delete_parser.add_argument('--filter', help='Filter rules by name')
delete_parser.add_argument('--delay', type=float, default=0.5, help='Delay between requests (default: 0.5)')
# List command
list_parser = subparsers.add_parser('list', help='List email rules')
list_parser.add_argument('--filter', help='Filter rules by name')
# Print command
print_parser = subparsers.add_parser('print', help='Print email mappings from file')
print_parser.add_argument('filename', help='JSON file to read')
# Interactive command
subparsers.add_parser('interactive', help='Run in interactive mode')
# Legacy command
legacy_parser = subparsers.add_parser('legacy', help='Run legacy scripts')
legacy_parser.add_argument('script', choices=['app', 'change', 'delete', 'print'],
help='Legacy script to run')
legacy_parser.add_argument('args', nargs='*', help='Arguments for the legacy script')
return parser
async def run_create_command(args):
"""Run the create command."""
from main import EmailRoutingApp
from config import API_TOKEN, ZONE_ID, DOMAIN
if not API_TOKEN or not ZONE_ID or not DOMAIN:
print("Error: Please configure API_TOKEN, ZONE_ID, and DOMAIN in config.py")
return 1
app = EmailRoutingApp(API_TOKEN, ZONE_ID, DOMAIN)
try:
await app.manager.create_email_rules(
destination_email=args.destination,
count=args.count,
email_length=args.length,
delay=args.delay
)
return 0
except Exception as e:
print(f"Error: {e}")
return 1
async def run_update_command(args):
"""Run the update command."""
from main import EmailRoutingApp
from config import API_TOKEN, ZONE_ID, DOMAIN
if not API_TOKEN or not ZONE_ID or not DOMAIN:
print("Error: Please configure API_TOKEN, ZONE_ID, and DOMAIN in config.py")
return 1
app = EmailRoutingApp(API_TOKEN, ZONE_ID, DOMAIN)
try:
await app.manager.update_destinations(
new_destination=args.destination,
filter_text=args.filter,
delay=args.delay
)
return 0
except Exception as e:
print(f"Error: {e}")
return 1
async def run_delete_command(args):
"""Run the delete command."""
from main import EmailRoutingApp
from config import API_TOKEN, ZONE_ID, DOMAIN
if not API_TOKEN or not ZONE_ID or not DOMAIN:
print("Error: Please configure API_TOKEN, ZONE_ID, and DOMAIN in config.py")
return 1
app = EmailRoutingApp(API_TOKEN, ZONE_ID, DOMAIN)
try:
await app.manager.delete_rules(
filter_text=args.filter,
delay=args.delay
)
return 0
except Exception as e:
print(f"Error: {e}")
return 1
async def run_list_command(args):
"""Run the list command."""
from main import EmailRoutingApp
from config import API_TOKEN, ZONE_ID, DOMAIN
if not API_TOKEN or not ZONE_ID or not DOMAIN:
print("Error: Please configure API_TOKEN, ZONE_ID, and DOMAIN in config.py")
return 1
app = EmailRoutingApp(API_TOKEN, ZONE_ID, DOMAIN)
try:
await app.list_rules_interactive()
return 0
except Exception as e:
print(f"Error: {e}")
return 1
async def run_print_command(args):
"""Run the print command."""
from main import EmailRoutingApp
from config import API_TOKEN, ZONE_ID, DOMAIN
app = EmailRoutingApp(API_TOKEN, ZONE_ID, DOMAIN)
try:
await app.print_file_mappings(args.filename)
return 0
except Exception as e:
print(f"Error: {e}")
return 1
async def run_interactive_command():
"""Run the interactive command."""
from main import main
await main()
return 0
def run_legacy_command(args):
"""Run legacy scripts for backward compatibility."""
script_map = {
'app': 'app.py',
'change': 'change_destination.py',
'delete': 'delete_emails.py',
'print': 'print_emails.py'
}
script_file = script_map.get(args.script)
if not script_file or not Path(script_file).exists():
print(f"Error: Legacy script {script_file} not found")
return 1
print(f"Running legacy script: {script_file}")
print("Note: Consider using the new OOP interface instead")
# Import and run the legacy script
import subprocess
import sys
cmd = [sys.executable, script_file] + args.args
result = subprocess.run(cmd)
return result.returncode
async def main():
"""Main CLI entry point."""
parser = setup_parser()
if len(sys.argv) == 1:
# No arguments provided, show help
parser.print_help()
return 0
args = parser.parse_args()
if not args.command:
parser.print_help()
return 0
try:
if args.command == 'create':
return await run_create_command(args)
elif args.command == 'update':
return await run_update_command(args)
elif args.command == 'delete':
return await run_delete_command(args)
elif args.command == 'list':
return await run_list_command(args)
elif args.command == 'print':
return await run_print_command(args)
elif args.command == 'interactive':
return await run_interactive_command()
elif args.command == 'legacy':
return run_legacy_command(args)
else:
print(f"Unknown command: {args.command}")
return 1
except KeyboardInterrupt:
print("\nOperation cancelled by user")
return 1
except Exception as e:
print(f"Unexpected error: {e}")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)