-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
235 lines (183 loc) · 6.72 KB
/
Copy pathcli.py
File metadata and controls
235 lines (183 loc) · 6.72 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
"""CLI commands for GALIAS."""
import typer
from typing import Optional
import sys
from api import get_api, APIError
from ui import (
print_banner, print_aliases_table, print_alias_count,
print_success, print_error, print_json_output,
prompt_alias, prompt_forward, prompt_delete_alias,
confirm_delete, handle_error_display, print_operation_summary
)
from config import DOMAIN, MAX_ALIASES
from pathlib import Path
app = typer.Typer(
name="galias",
help="GALIAS - Terminal-based ImprovMX alias manager",
add_completion=False
)
def show_banner_and_count(skip_banner: bool = False):
"""Show banner and current alias count."""
if not skip_banner:
print_banner()
try:
api = get_api()
count = api.get_alias_count()
print_alias_count(count)
print()
return count
except Exception as e:
handle_error_display(e)
return None
@app.command()
def list(
json_output: bool = typer.Option(False, "--json", help="Output raw JSON for scripting"),
no_color: bool = typer.Option(False, "--no-color", help="Disable colored output"),
quiet: bool = typer.Option(False, "-q", "--quiet", help="Skip banner and just show aliases")
):
"""List all aliases and show current count."""
try:
# Set up console for no-color mode
if no_color:
from ui import console
console._color_system = None
api = get_api()
aliases_data = api.list_aliases()
if json_output:
print_json_output(aliases_data)
return
if not quiet:
show_banner_and_count()
print_aliases_table(aliases_data)
if not quiet:
count = len(aliases_data.get("aliases", []))
print_alias_count(count)
except Exception as e:
handle_error_display(e)
sys.exit(1)
@app.command()
def add(
alias: Optional[str] = typer.Argument(None, help="Alias name (without domain)"),
forward: Optional[str] = typer.Argument(None, help="Email address to forward to"),
json_output: bool = typer.Option(False, "--json", help="Output raw JSON for scripting"),
no_color: bool = typer.Option(False, "--no-color", help="Disable colored output"),
quiet: bool = typer.Option(False, "-q", "--quiet", help="Skip banner and progress display")
):
"""Add a new alias."""
try:
# Set up console for no-color mode
if no_color:
from ui import console
console._color_system = None
if not quiet:
show_banner_and_count()
# Interactive prompts if arguments not provided
if alias is None:
alias = prompt_alias()
if forward is None:
forward = prompt_forward()
# Validate inputs
if not alias or not forward:
print_error("Both alias and forward email are required")
sys.exit(1)
# Validate email format (basic check)
if "@" not in forward or "." not in forward:
print_error("Invalid email format for forward address")
sys.exit(1)
api = get_api()
result = api.add_alias(alias, forward)
if json_output:
print_json_output(result)
return
print_operation_summary("add", alias, forward)
# Show updated count
if not quiet:
print()
count = api.get_alias_count()
print_alias_count(count)
except Exception as e:
handle_error_display(e)
sys.exit(1)
@app.command()
def delete(
alias: Optional[str] = typer.Argument(None, help="Alias name to delete"),
force: bool = typer.Option(False, "-f", "--force", help="Skip confirmation prompt"),
json_output: bool = typer.Option(False, "--json", help="Output raw JSON for scripting"),
no_color: bool = typer.Option(False, "--no-color", help="Disable colored output"),
quiet: bool = typer.Option(False, "-q", "--quiet", help="Skip banner and progress display")
):
"""Delete an existing alias."""
try:
# Set up console for no-color mode
if no_color:
from ui import console
console._color_system = None
if not quiet:
show_banner_and_count()
# Interactive prompt if alias not provided
if alias is None:
alias = prompt_delete_alias()
if not alias:
print_error("Alias name is required")
sys.exit(1)
# Confirmation prompt (unless forced or in JSON mode)
if not force and not json_output:
if not confirm_delete(alias):
print("Operation cancelled.")
return
api = get_api()
result = api.delete_alias(alias)
if json_output:
print_json_output(result)
return
print_operation_summary("delete", alias)
# Show updated count
if not quiet:
print()
count = api.get_alias_count()
print_alias_count(count)
except Exception as e:
handle_error_display(e)
sys.exit(1)
@app.command()
def status(
json_output: bool = typer.Option(False, "--json", help="Output raw JSON for scripting"),
no_color: bool = typer.Option(False, "--no-color", help="Disable colored output")
):
"""Show current alias count and status."""
try:
# Set up console for no-color mode
if no_color:
from ui import console
console._color_system = None
api = get_api()
count = api.get_alias_count()
if json_output:
status_data = {
"current_aliases": count,
"max_aliases": MAX_ALIASES,
"domain": DOMAIN,
"usage_percentage": round((count / MAX_ALIASES) * 100, 1)
}
print_json_output(status_data)
return
print_banner()
print_alias_count(count)
except Exception as e:
handle_error_display(e)
sys.exit(1)
@app.callback(invoke_without_command=True)
def main(
ctx: typer.Context,
version: bool = typer.Option(False, "--version", help="Show version information")
):
"""GALIAS - Terminal-based ImprovMX alias manager."""
if version:
typer.echo("GALIAS v1.0.0")
typer.echo("ImprovMX Alias Manager")
raise typer.Exit()
if ctx.invoked_subcommand is None:
typer.echo("GALIAS - Terminal-based ImprovMX alias manager")
typer.echo("Use --help for more information.")
if __name__ == "__main__":
app()