-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcommands.py
More file actions
98 lines (63 loc) · 2.41 KB
/
commands.py
File metadata and controls
98 lines (63 loc) · 2.41 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
# -*- coding: utf-8 -*-
# This file is part of the CloudBlue Connect connect-cli.
# Copyright (c) 2025 CloudBlue. All rights reserved.
import os
import sys
import click
from connect.cli.core import group
from connect.cli.core.config import pass_config
from connect.cli.plugins.play.context import Context
@group(name='play', short_help='Play connect scripts.')
def grp_play():
pass
class PlayOptions:
context_file = 'context.json'
class PassArgumentDecorator:
def __init__(self, arg):
self.obj = arg
def __call__(self, f):
def wrapped(*args, **kwargs):
f(self.obj, *args, **kwargs)
return wrapped
pass_arg = PassArgumentDecorator
def setup_script_command(cls):
@pass_config
@pass_arg(cls)
def cmd_play_custom(script_class, config, **kwargs):
Context.context_file_name = PlayOptions.context_file
ctx = Context.create(**kwargs)
if 'endpoint' not in ctx or not ctx.endpoint:
ctx.endpoint = config.active.endpoint
if 'distributor_account_token' not in ctx or not ctx.distributor_account_token:
ctx.distributor_account_token = config.active.api_key
ctx | script_class()
ctx.save()
for o in cls.options():
cmd_play_custom = click.option(*o.args, **o.kwargs)(cmd_play_custom)
grp_play.command(name=cls.command(), short_help=cls.help())(cmd_play_custom)
def load_one_script(scripts, filename):
modname = filename[0:-3]
try:
mod = __import__(modname, globals={'__name__': __name__}, fromlist=['*'])
if not hasattr(mod, '__all__'):
print(f'Warning: {filename} has no __all__ defined', file=sys.stderr)
return
for cname in mod.__all__:
cls = getattr(mod, cname)
setup_script_command(cls)
except Exception as e:
print(f'Failed to import {scripts}/{filename}: {e}')
def load_scripts_actions():
scripts = os.environ.get('CCLI_SCRIPTS', 'scripts')
if scripts[0] != '/':
scripts = os.path.join(os.getcwd(), scripts)
if os.path.isdir(scripts):
print(f'Reading scripts library from {scripts}')
sys.path.append(scripts)
for filename in sorted(os.listdir(scripts)):
if not filename.endswith('.py') or filename[0] == '_':
continue
load_one_script(scripts, filename)
load_scripts_actions()
def get_group():
return grp_play