-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcodeflare_cli.py
53 lines (40 loc) · 1.36 KB
/
codeflare_cli.py
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
import click
import os
from codeflare_sdk.cli.cli_utils import load_auth
from codeflare_sdk.cluster.cluster import get_current_namespace
cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "commands"))
class CodeflareContext:
def __init__(self):
self.codeflare_path = _initialize_codeflare_folder()
self.current_namespace = get_current_namespace()
def _initialize_codeflare_folder():
codeflare_folder = os.path.expanduser("~/.codeflare")
if not os.path.exists(codeflare_folder):
os.makedirs(codeflare_folder)
return codeflare_folder
class CodeflareCLI(click.MultiCommand):
def list_commands(self, ctx):
rv = []
for filename in os.listdir(cmd_folder):
if filename.endswith(".py") and filename != "__init__.py":
rv.append(filename[:-3])
rv.sort()
return rv
def get_command(self, ctx, name):
ns = {}
fn = os.path.join(cmd_folder, name + ".py")
try:
with open(fn) as f:
code = compile(f.read(), fn, "exec")
eval(code, ns, ns)
return ns["cli"]
except FileNotFoundError:
return
@click.command(cls=CodeflareCLI)
@click.pass_context
def cli(ctx):
load_auth()
ctx.obj = CodeflareContext() # Ran on every command
pass
if __name__ == "__main__":
cli()