-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.py
More file actions
82 lines (67 loc) · 2.17 KB
/
Copy pathconfig.py
File metadata and controls
82 lines (67 loc) · 2.17 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
import json
import os
from dataclasses import asdict, dataclass
from typing import Optional
import click
import questionary as q
from rich.console import Console
from rich.table import Table
@dataclass
class ContextObject:
self_hosted: Optional[bool] = False
gateway_endpoint: Optional[str] = None
api_key: Optional[str] = None
@staticmethod
def config_path():
config_dir = click.get_app_dir("launch")
if not os.path.exists(config_dir):
os.makedirs(config_dir)
return os.path.join(config_dir, "config.json")
def load(self):
try:
with open(self.config_path(), "r", encoding="utf-8") as f:
new_items = json.load(f)
for key, value in new_items.items():
if hasattr(self, key):
setattr(self, key, value)
except FileNotFoundError:
pass
return self
def save(self):
with open(self.config_path(), "w", encoding="utf-8") as f:
json.dump(asdict(self), f, indent=4)
@click.group("config")
@click.pass_context
def config(ctx: click.Context):
"""
Config is a wrapper around getting and setting your API key and other configuration options
"""
@config.command("get")
@click.pass_context
def get_config(ctx: click.Context):
table = Table(
"Self-Hosted",
"API Key",
"Gateway Endpoint",
)
table.add_row(str(ctx.obj.self_hosted), ctx.obj.api_key, ctx.obj.gateway_endpoint)
console = Console()
console.print(table)
@config.command("set")
@click.pass_context
def set_config(ctx: click.Context):
ctx.obj.api_key = q.text(
message="Your Scale API Key?",
default=ctx.obj.api_key or "",
validate=lambda x: isinstance(x, str) and len(x) > 16, # Arbitrary length right now
).ask()
ctx.obj.self_hosted = q.confirm(
message="Is your installation of Launch self-hosted?",
default=ctx.obj.self_hosted,
).ask()
if ctx.obj.self_hosted:
ctx.obj.gateway_endpoint = q.text(
message="Your Gateway Endpoint?",
default=ctx.obj.gateway_endpoint or "",
).ask()
ctx.obj.save()