|
1 | 1 | import json |
2 | | -import subprocess |
3 | 2 |
|
| 3 | +from data import TOOLS |
| 4 | +from lib.commands import local_cmd |
4 | 5 | from lib.typing import JSONType |
5 | 6 |
|
6 | | -from typing import Dict, Literal, overload |
| 7 | +from typing import Literal, overload |
7 | 8 |
|
8 | | -# TODO: either: |
9 | | -# * replace simple_output and use_json by a single output type |
10 | | -# * make sure that simple_output=False and use_json=True are not being used together |
11 | | - |
12 | | -@overload |
13 | | -def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[True] = True, |
14 | | - use_json: Literal[False] = False) -> str: |
15 | | - ... |
16 | | -@overload |
17 | | -def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[True] = True, |
18 | | - use_json: Literal[True]) -> JSONType: |
19 | | - ... |
20 | 9 | @overload |
21 | | -def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[False], |
22 | | - use_json: bool = False) -> subprocess.CompletedProcess[bytes]: |
| 10 | +def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: Literal[False] = False) -> str: |
23 | 11 | ... |
24 | 12 | @overload |
25 | | -def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: bool = True, |
26 | | - use_json: bool = False) -> subprocess.CompletedProcess[bytes] | JSONType | str: |
| 13 | +def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: Literal[True]) -> JSONType: |
27 | 14 | ... |
28 | | -def xo_cli( |
29 | | - action: str, args: dict[str, str] = {}, check: bool = True, simple_output: bool = True, use_json: bool = False |
30 | | -) -> subprocess.CompletedProcess[bytes] | JSONType | str: |
31 | | - run_array = ['xo-cli', action] |
| 15 | + |
| 16 | +def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: bool = False) -> JSONType | str: |
| 17 | + cmd = [TOOLS.get('xo-cli', 'xo-cli'), action] |
32 | 18 | if use_json: |
33 | | - run_array += ['--json'] |
34 | | - run_array += ["%s=%s" % (key, value) for key, value in args.items()] |
35 | | - res = subprocess.run( |
36 | | - run_array, |
37 | | - stdout=subprocess.PIPE, |
38 | | - stderr=subprocess.STDOUT, |
39 | | - check=check |
40 | | - ) |
41 | | - if simple_output: |
42 | | - output = res.stdout.decode().strip() |
43 | | - if use_json: |
44 | | - return json.loads(output) |
45 | | - return output |
46 | | - return res |
| 19 | + cmd += ['--json'] |
| 20 | + cmd += ["%s=%s" % (key, value) for key, value in args.items()] |
| 21 | + |
| 22 | + res = local_cmd(cmd, check=check) |
| 23 | + |
| 24 | + if use_json: |
| 25 | + return json.loads(res.stdout) |
| 26 | + |
| 27 | + return res.stdout |
47 | 28 |
|
48 | 29 | def xo_object_exists(uuid: str) -> bool: |
49 | | - lst = json.loads(xo_cli('--list-objects', {'uuid': uuid})) |
| 30 | + lst = xo_cli('list-objects', {'uuid': uuid}, use_json=True) |
| 31 | + assert isinstance(lst, list) |
50 | 32 | return len(lst) > 0 |
0 commit comments