-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (85 loc) · 3.02 KB
/
main.py
File metadata and controls
110 lines (85 loc) · 3.02 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
import typer
from rich import print, print_json
import utils as utl
from typing_extensions import Annotated
app = typer.Typer()
# default command for HubSwitch
@app.command()
def activate(id: str):
"""
Activate github account based on id
"""
# Find selected account
accountFound = True
try:
details = utl.config["accounts"][id]
except KeyError as err:
print(f"[bold red]Error:[/bold red] Account {id} does not exist")
accountFound = False
if accountFound:
try:
account_name = details["account_name"]
username = details["username"]
email = details["email"]
PAT = details["PAT"]
except:
print("[bold red]Error:[/bold red] Cannot extract account details. Make sure config file matches latest schema")
# Update git credentials
utl.update_credential(username, PAT)
# Update git config file
utl.run_command(f'git config --global user.name "{username}"')
utl.run_command(f'git config --global user.email "{email}"')
# Update current account property
utl.config["current"] = id
utl.saveConfig()
print(f"[bold green]{account_name}[/bold green] account activated.")
@app.command()
def current(verbose: Annotated[bool, typer.Option("--verbose", "-v", help="Display detailed information of activated account")] = False):
"""
Display activated account
"""
id = utl.config["current"]
# Fetch details of current account
try:
details = utl.config["accounts"][id]
account_name = details["account_name"]
except KeyError as err:
print(f"[bold red]Error:[/bold red] Could not fetch details of account {id}")
# Display information
if verbose:
print(f"Current: [bold green]{account_name} ({id})[/bold green]")
print_json(data=details)
else:
print(f"Current: [bold green]{account_name} ({id})[/bold green]")
@app.command()
def accounts(verbose: Annotated[bool, typer.Option("--verbose", "-v", help="Display detailed information of all accounts")] = False):
f"""
List all accounts stored in the config file
"""
current_id= utl.config["current"]
pos = 1
for id, details in utl.config["accounts"].items():
account_name = details["account_name"]
if verbose:
if id == current_id:
print(f"{pos}. [bold green]{account_name} ({id})[/bold green]")
else:
print(f"{pos}. {account_name} ({id})")
print_json(data=details)
else:
if id == current_id:
print(f"{pos}. [bold green]{account_name}[/bold green]")
else:
print(f"{pos}. {account_name}")
pos+=1
@app.command()
def schema():
f"""
Show schema of config file (v{utl.config_version})
"""
print(utl.config_schema_example)
if __name__ == "__main__":
# Setup config file
utl.findConfig()
utl.validateConfig()
app()