-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from fabiobarkoski/cli
CLI Improvements
- Loading branch information
Showing
13 changed files
with
314 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version = "0.2.19" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import sys | ||
import pkgutil | ||
from pathlib import Path | ||
from ruamel.yaml import YAML | ||
from typing import TypeAlias | ||
from collections.abc import Callable, Iterator | ||
from importlib.util import find_spec | ||
|
||
from uplib import Prompt | ||
from up.help_texts import list_help_text, details_help_text, prompts_help_text | ||
|
||
yaml=YAML() | ||
|
||
Arguments: TypeAlias = list[str] | ||
Commands: TypeAlias = dict[str, tuple[Callable[[Arguments], None], str]] | ||
|
||
def _get_prompts(plugin_name: str) -> Iterator[dict]: | ||
plugin_path = Path(find_spec(f"up_{plugin_name}").origin).parent | ||
prompts_path = Path(plugin_path, 'up.yaml') | ||
try: | ||
with open(prompts_path, 'r') as file: | ||
prompts = (yaml.load(file))['prompts'] | ||
for prompt in prompts: | ||
yield prompt | ||
except FileNotFoundError: | ||
raise FileNotFoundError("Plugin doens't have yaml file!") | ||
|
||
def _list_plugins(args: Arguments): | ||
""" | ||
Shows all installed plugins | ||
""" | ||
plugins = [] | ||
for pkg in pkgutil.iter_modules(): | ||
if pkg.name.startswith("up_"): | ||
plugins.append(pkg.name) | ||
print("Installed plugins:") | ||
print("\n".join(plugins)) | ||
|
||
def _list_prompts(args: Arguments): | ||
""" | ||
Shows the prompts from up.yaml of a plugin | ||
""" | ||
for prompt in _get_prompts(args[0]): | ||
yaml.dump(prompt, sys.stdout) | ||
|
||
def _prompt_details(args: Arguments): | ||
""" | ||
Shows the prompt configuration from up.yaml of a plugin | ||
""" | ||
for prompt in _get_prompts(args[0]): | ||
if prompt['prompt'] == ' '.join(args): | ||
yaml.dump(prompt, sys.stdout) | ||
break | ||
|
||
_commands: Commands = { | ||
"list": (_list_plugins, list_help_text), | ||
"prompts": (_list_prompts, prompts_help_text), | ||
"details": (_prompt_details, details_help_text), | ||
} | ||
|
||
def _get_command_description(command:str) -> str: | ||
description = _commands[command][0].__doc__.replace('\n','') | ||
return description.lstrip() | ||
|
||
def cmd_in_prompt(prompt: Prompt) -> bool: | ||
reserved_keyword = prompt[0] | ||
if reserved_keyword != 'plugin' or len(prompt) <= 1: | ||
return False | ||
command = prompt[1] | ||
match command: | ||
case cmd if cmd in _commands.keys(): | ||
return True | ||
case _: | ||
raise Exception('Command not found!') | ||
|
||
def run_cmd(command: str, arguments: Arguments): | ||
if len(arguments) == 1 and '--help' in arguments or '-h' in arguments: | ||
cmd_description = _get_command_description(command) | ||
print(_commands[command][1] % cmd_description) | ||
exit(0) | ||
_commands[command][0](arguments) | ||
exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
up_help_text = """\rUnified and Pluggable CLI - Version %s | ||
\n\rUsage: | ||
up <prompt> | ||
or | ||
up plugin <command> | ||
\n\rPrompt options: | ||
-Xv\t Set a volume to be used | ||
-Xp\t Set a port to be used | ||
\n\rAvailable commands: | ||
\r%s | ||
\rExamples: | ||
up ansible --version\t Shows ansible version | ||
up -Xp={8080/tcp: 5000} ansible -h\t Will set the port to ansible | ||
up plugin prompts ansible\t List all ansible prompts from up.yaml | ||
""" | ||
list_help_text = """\rDescription: %s | ||
\n\rUsage: | ||
up plugin list | ||
""" | ||
details_help_text = """\rDescription: %s | ||
\n\rUsage: | ||
up plugin details <prompt> | ||
\rExample: | ||
up plugin details ansible --version\t Shows prompt configuration from up.yaml | ||
""" | ||
prompts_help_text = """Description: %s | ||
\n\rUsage: | ||
up plugin prompts <plugin> | ||
\rExample: | ||
up plugin details ansible\t List all ansible prompts from up.yaml | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import re | ||
from shlex import join | ||
|
||
from uplib import options_map, Prompt | ||
|
||
_options_pattern = re.compile(r'\-X\w+\=\S+') | ||
|
||
def _have_options(prompt: Prompt) -> bool: | ||
if _options_pattern.match(join(prompt)): | ||
return True | ||
return False | ||
|
||
def _match_value(pattern: str, string: str) -> str: | ||
value = re.match(pattern, string) | ||
return value.groups()[0] | ||
|
||
def _set_ports(value: str): | ||
formated_value = value.split(':') | ||
options_map['ports'] = {formated_value[0]: formated_value[1]} | ||
|
||
def _set_volumes(value: str): | ||
formated_value = value.split(':') | ||
volume_key = formated_value[0] | ||
volume_bind = formated_value[1].split(',')[0] | ||
volume_mode = formated_value[1].split(',')[1] | ||
options_map['volumes'] = { | ||
volume_key: { | ||
'bind': volume_bind, | ||
'mode': volume_mode, | ||
}, | ||
} | ||
|
||
_options = { | ||
'p': _set_ports, | ||
'v': _set_volumes, | ||
} | ||
|
||
def _set_option(option: str): | ||
key = _match_value(r'\-X(\w)', option) | ||
value = _match_value(r'\-X\w.(.+)', option) | ||
_options[key](value) | ||
|
||
def setup_options(prompt: Prompt) -> Prompt: | ||
prompt_without_options = join(prompt) | ||
if _have_options(prompt): | ||
for option in _options_pattern.findall(join(prompt)): | ||
_set_option(option) | ||
prompt_without_options = _options_pattern.sub( | ||
'', prompt_without_options) | ||
return prompt_without_options.lstrip().split() | ||
return prompt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from up.commands import _commands | ||
|
||
def get_commands_information() -> str: | ||
commands = [] | ||
for key, value in _commands.items(): | ||
command = key | ||
command_description = value[0].__doc__.replace('\n','') | ||
commands.append(f" {command}\t {command_description}") | ||
return "\n".join(commands) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.