|
1 | 1 | from __future__ import print_function |
2 | 2 |
|
3 | | -from cmd2 import Cmd |
| 3 | +import click |
4 | 4 |
|
| 5 | +from stackdio.cli.utils import print_summary |
5 | 6 |
|
6 | | -class FormulaMixin(Cmd): |
7 | | - FORMULA_COMMANDS = ["list", "import", "delete"] |
8 | 7 |
|
9 | | - def do_formulas(self, arg): |
10 | | - """Entry point to controlling formulas.""" |
| 8 | +@click.group() |
| 9 | +def formulas(): |
| 10 | + """ |
| 11 | + Perform actions on formulas |
| 12 | + """ |
| 13 | + pass |
11 | 14 |
|
12 | | - USAGE = "Usage: formulas COMMAND\nWhere COMMAND is one of: %s" % ( |
13 | | - ", ".join(self.FORMULA_COMMANDS)) |
14 | 15 |
|
15 | | - args = arg.split() |
16 | | - if not args or args[0] not in self.FORMULA_COMMANDS: |
17 | | - print(USAGE) |
18 | | - return |
| 16 | +@formulas.command(name='list') |
| 17 | +@click.pass_obj |
| 18 | +def list_formulas(obj): |
| 19 | + """ |
| 20 | + List all formulas |
| 21 | + """ |
| 22 | + client = obj['client'] |
19 | 23 |
|
20 | | - formula_cmd = args[0] |
21 | | - if formula_cmd == "list": |
22 | | - self._list_formulas() |
23 | | - elif formula_cmd == "import": |
24 | | - self._import_formula(args[1:]) |
25 | | - elif formula_cmd == "delete": |
26 | | - self._delete_formula(args[1:]) |
| 24 | + click.echo('Getting formulas ... ') |
| 25 | + print_summary('Formula', client.list_formulas()) |
27 | 26 |
|
28 | | - else: |
29 | | - print(USAGE) |
30 | 27 |
|
31 | | - def complete_formulas(self, text, line, begidx, endidx): |
32 | | - # not using line, begidx, or endidx, thus the following pylint disable |
33 | | - # pylint: disable=W0613 |
34 | | - return [i for i in self.FORMULA_COMMANDS if i.startswith(text)] |
| 28 | +@formulas.command(name='import') |
| 29 | +@click.pass_obj |
| 30 | +@click.argument('uri') |
| 31 | +@click.option('-u', '--username', type=click.STRING, help='Git username') |
| 32 | +@click.option('-p', '--password', type=click.STRING, prompt=True, hide_input=True, |
| 33 | + help='Git password') |
| 34 | +def import_formula(obj, uri, username, password): |
| 35 | + """ |
| 36 | + Import a formula |
| 37 | + """ |
| 38 | + client = obj['client'] |
35 | 39 |
|
36 | | - def help_formulas(self): |
37 | | - print("Manage formulas.") |
38 | | - print("Sub-commands can be one of:\n\t{0}".format( |
39 | | - ", ".join(self.FORMULA_COMMANDS))) |
40 | | - print("Try 'formulas COMMAND' to get help on (most) sub-commands") |
| 40 | + if username and not password: |
| 41 | + raise click.UsageError('You must provide a password when providing a username') |
41 | 42 |
|
42 | | - def _list_formulas(self): |
43 | | - """List all formulas""" |
| 43 | + click.echo('Importing formula from {0}'.format(uri)) |
| 44 | + formula = client.import_formula(uri, git_username=username, git_password=password) |
44 | 45 |
|
45 | | - print("Getting formulas ... ") |
46 | | - formulas = self.stacks.list_formulas() |
47 | | - self._print_summary("Formula", formulas) |
| 46 | + click.echo('Detail: {0}'.format(formula['status_detail'])) |
48 | 47 |
|
49 | | - def _import_formula(self, args): |
50 | | - """Import a formula""" |
51 | 48 |
|
52 | | - if len(args) != 1: |
53 | | - print("Usage: formulas import URL") |
54 | | - return |
| 49 | +def get_formula_id(client, formula_uri): |
| 50 | + found_formulas = client.search_formulas(uri=formula_uri) |
55 | 51 |
|
56 | | - formula_url = args[0] |
57 | | - print("Importing formula from {0}".format(formula_url)) |
58 | | - formula = self.stacks.import_formula(formula_url, public=False) |
| 52 | + if len(found_formulas) == 0: |
| 53 | + raise click.Abort('Formula "{0}" does not exist'.format(formula_uri)) |
| 54 | + else: |
| 55 | + return found_formulas[0]['id'] |
59 | 56 |
|
60 | | - if isinstance(formula, list): |
61 | | - print("Formula imported, try the 'list' command to monitor status") |
62 | | - elif formula.get("detail"): |
63 | | - print("Error importing: {0}".format(formula.get("detail"))) |
64 | 57 |
|
65 | | - def _delete_formula(self, args): |
66 | | - """Delete a formula""" |
| 58 | +@formulas.command(name='delete') |
| 59 | +@click.pass_obj |
| 60 | +@click.argument('uri') |
| 61 | +def delete_formula(obj, uri): |
| 62 | + """ |
| 63 | + Delete a formula |
| 64 | + """ |
| 65 | + client = obj['client'] |
67 | 66 |
|
68 | | - args = " ".join(args) |
69 | | - if len(args) == 0: |
70 | | - print("Usage: formulas delete TITLE") |
71 | | - return |
| 67 | + formula_id = get_formula_id(client, uri) |
72 | 68 |
|
73 | | - formula_id = self.stacks.get_formula_id(args) |
| 69 | + click.confirm('Really delete formula {0}?'.format(uri), abort=True) |
74 | 70 |
|
75 | | - really = raw_input("Really delete formula {0} (y/n)? ".format(args)) |
76 | | - if really not in ["y", "Y"]: |
77 | | - print("Aborting deletion") |
78 | | - return |
79 | | - |
80 | | - self.stacks.delete_formula(formula_id) |
81 | | - |
82 | | - print("Formula deleted, try the 'list' command to monitor status") |
| 71 | + client.delete_formula(formula_id) |
0 commit comments