Commit b02e9499 authored by Mark Hewitt's avatar Mark Hewitt
Browse files

split global options, search table output fields changed

parent fb46344f
Pipeline #70275 passed with stages
in 1 minute and 29 seconds
# OSDU Command Line Interface
[![Build](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/badges/main/pipeline.svg)](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/badges/main/pipeline.svg)
[![Build](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/badges/main/pipeline.svg)](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/-/commits/main)
[![License](https://img.shields.io/pypi/l/osducli.svg)](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/-/blob/main/LICENSE)
[![PyPi Version](https://img.shields.io/pypi/v/osducli.svg?color=informational)](https://pypi.org/project/osducli/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/osducli.svg?color=informational)](https://pypi.org/project/osducli/)
![Unknown coverage](https://img.shields.io/badge/coverage-%3F%3F%3F-important)
[![Coverage](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/badges/main/coverage.svg)](https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/-/merge_requests?scope=all&state=all)
Command-line interface for interacting with OSDU.
......
......@@ -28,6 +28,12 @@ For more information, specify the `-h` flag:
Change Log
==========
0.0.18
------
- split global options in help text for clarity
- search table output fields changed
0.0.17
------
......
......@@ -12,4 +12,4 @@
""" OSDU command line environment"""
__VERSION__ = "0.0.17"
__VERSION__ = "0.0.18"
......@@ -17,10 +17,8 @@ import os.path
import pkgutil
import click
from click.core import Context
from click.formatting import HelpFormatter
from osducli.click_cli import State
from osducli.click_cli import CustomClickGroup, State
def get_commands_from_pkg(pkg) -> dict:
......@@ -32,13 +30,11 @@ def get_commands_from_pkg(pkg) -> dict:
Returns:
dict: [description]
"""
# groups with only 1 command that should be kept as groups for future expandability
keep_groups = [
"osducli.commands.legal",
"osducli.commands.list",
"osducli.commands.unit",
"osducli.commands.schema",
"osducli.commands.search",
"osducli.commands.workflow",
]
pkg_obj = importlib.import_module(pkg)
......@@ -61,51 +57,21 @@ def get_commands_from_pkg(pkg) -> dict:
commands[module.name.replace("_", "-")] = click_command
elif len(group_commands) >= 1:
# print(f"Add group {module.name.replace('_', '-')}\n{group_commands}")
commands[module.name.replace("_", "-")] = click.Group(
commands[module.name.replace("_", "-")] = CustomClickGroup(
context_settings={"help_option_names": ["-h", "--help"]},
help=module_obj.__doc__,
commands=group_commands,
)
# else:
# print(f"Skip group {module.name.replace('_', '-')}")
# if len(commands) > 0:
# print(f"return {len(commands)}")
# print(commands)
return commands
class CustomHelpGroup(click.Group):
"""Custom help text for the base click command
Args:
click ([type]): [description]
"""
def format_help(self, ctx, formatter):
self.format_usage(ctx, formatter)
self.format_help_text(ctx, formatter)
# click.Command.format_options(self, ctx, formatter)
self.format_commands(ctx, formatter)
self.format_epilog(ctx, formatter)
self.format_global_options(ctx, formatter)
def format_global_options(self, ctx: Context, formatter: HelpFormatter) -> None:
"""Writes all the options into the formatter if they exist."""
opts = []
for param in self.get_params(ctx):
_rv = param.get_help_record(ctx)
if _rv is not None:
opts.append(_rv)
if opts:
with formatter.section("Global Options"):
formatter.write_dl(opts)
return commands
# Main entry point for OSDU CLI.
# noqa: W606,W605 pylint: disable=W1401
@click.group(
# cls=CustomHelpGroup,
cls=CustomClickGroup,
commands=get_commands_from_pkg("osducli.commands"),
context_settings={"help_option_names": ["-h", "--help"]},
)
......@@ -121,7 +87,10 @@ def cli(ctx):
Welcome to the OSDU CLI!
\033[33mNote: This is currently a work in progress and may contain bugs or breaking changes.
Please share ideas / issues on the git page.\033[39m
Please share ideas / issues on the git page:
\b
https://community.opengroup.org/osdu/platform/data-flow/data-loading/osdu-cli/-/issues\033[39m
Use `osdu version` to display the current version.
......
......@@ -21,13 +21,13 @@ import logging
from os import path
import click
from click.core import Context
from click.formatting import HelpFormatter
from osducli.config import CLI_ENV_VAR_PREFIX, CLIConfig
from osducli.log import get_logger
from osducli.state import get_default_config
# from osducli.util import is_help_command
class State: # pylint: disable=too-few-public-methods
"""Global state passed to all click commands"""
......@@ -43,6 +43,42 @@ class State: # pylint: disable=too-few-public-methods
return f"State=Debug: {self.debug}, Config path: {self.config_path}"
def _format_click_options(cmd: click.Command, ctx: Context, formatter: HelpFormatter) -> None:
"""Writes options into the formatter split between Options and Common Options."""
opts = []
common_opts = []
for param in cmd.get_params(ctx):
_rv = param.get_help_record(ctx)
if _rv is not None:
if param.name in ["help", "debug", "config", "output", "query"]:
common_opts.append(_rv)
else:
opts.append(_rv)
if opts:
with formatter.section("Options"):
formatter.write_dl(opts)
if common_opts:
with formatter.section("Common Options"):
formatter.write_dl(common_opts)
class CustomClickGroup(click.Group):
"""Custom click.Group class providing customised help text"""
def format_options(self, ctx: Context, formatter: HelpFormatter) -> None:
_format_click_options(self, ctx, formatter)
self.format_commands(ctx, formatter)
class CustomClickCommand(click.Command):
"""Custom click.GrouCommand class providing customised help text"""
def format_options(self, ctx: Context, formatter: HelpFormatter) -> None:
_format_click_options(self, ctx, formatter)
def global_params(func):
"""Handle global parameters setting to setup state and remove parameters from those passed
to the decorated function call."""
......
......@@ -16,14 +16,14 @@ from pathlib import Path
import click
from osducli.click_cli import State, global_params
from osducli.click_cli import CustomClickCommand, State, global_params
from osducli.cliclient import handle_cli_exceptions
from osducli.state import get_default_config_path, set_default_config_path
from osducli.util.prompt import prompt
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@handle_cli_exceptions
@global_params
def _click_command(state: State):
......
......@@ -17,7 +17,7 @@ import os
import click
from osducli.click_cli import global_params
from osducli.click_cli import CustomClickCommand, global_params
from osducli.cliclient import handle_cli_exceptions
from osducli.commands.config.consts import (
MSG_HEADING_CURRENT_CONFIG_INFO,
......@@ -28,7 +28,7 @@ from osducli.config import CLI_ENV_VAR_PREFIX, CLIConfig
# from osducli.util import is_help_command
@click.command()
@click.command(cls=CustomClickCommand)
@global_params
@handle_cli_exceptions
def _click_command(state):
......
......@@ -16,7 +16,7 @@ import configparser
import click
from osducli.click_cli import State, global_params
from osducli.click_cli import CustomClickCommand, State, global_params
from osducli.cliclient import handle_cli_exceptions
from osducli.commands.config.consts import (
AUTHENTICATION_LIST,
......@@ -76,7 +76,7 @@ from osducli.util.prompt import prompt, prompt_choice_list, prompt_y_n
# from osducli.util import is_help_command
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-k", "--key", help="A specific key to update.")
@click.option("-v", "--value", help="Value to update key with.")
@global_params
......
......@@ -18,7 +18,7 @@ import os
import click
import requests
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.commands.dataload.status import check_status
from osducli.commands.dataload.verify import batch_verify
......@@ -40,7 +40,7 @@ VERIFY_BATCH_SIZE = 200
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option(
"-p",
"--path",
......
......@@ -17,7 +17,7 @@ import time
import click
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.config import CONFIG_WORKFLOW_URL, CLIConfig
from osducli.log import get_logger
......@@ -34,7 +34,7 @@ logger = get_logger(__name__)
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-r", "--runid", help="Runid to query status of.")
@click.option(
"-rl",
......
......@@ -16,7 +16,7 @@ import json
import click
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.config import CONFIG_SEARCH_URL, CLIConfig
from osducli.log import get_logger
......@@ -26,7 +26,7 @@ logger = get_logger(__name__)
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option(
"-p",
"--path",
......
......@@ -15,12 +15,12 @@
import click
from osdu.entitlements import EntitlementsClient
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-g", "--group", help="Email address of the group", required=True)
@handle_cli_exceptions
@command_with_output("")
......
......@@ -15,12 +15,12 @@
import click
from osdu.entitlements import EntitlementsClient
from osducli.click_cli import State, global_params
from osducli.click_cli import CustomClickCommand, State, global_params
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-g", "--group", help="Email address of the group", required=True)
@handle_cli_exceptions
@global_params
......
......@@ -15,12 +15,12 @@
import click
from osdu.entitlements import EntitlementsClient
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-g", "--group", help="Email address of the group", required=True)
@handle_cli_exceptions
@command_with_output("members[*]")
......
......@@ -15,12 +15,12 @@
import click
from osdu.entitlements import EntitlementsClient
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-m", "--member", help="Email of the member to be added.", required=True)
@click.option("-g", "--group", help="Email address of the group", required=True)
@click.option(
......
......@@ -14,14 +14,14 @@
import click
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import handle_cli_exceptions
from ..groups.members import list_group_members
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-g", "--group", help="Email address of the group", required=True)
@handle_cli_exceptions
@command_with_output("members[*]")
......
......@@ -15,12 +15,12 @@
import click
from osdu.entitlements import EntitlementsClient
from osducli.click_cli import State, global_params
from osducli.click_cli import CustomClickCommand, State, global_params
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@click.option("-m", "--member", help="Email of the member to be remove.", required=True)
@click.option("-g", "--group", help="Email address of the group", required=True)
@handle_cli_exceptions
......
......@@ -15,12 +15,12 @@
import click
from osdu.entitlements import EntitlementsClient
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
# click entry point
@click.command()
@click.command(cls=CustomClickCommand)
@handle_cli_exceptions
@command_with_output("groups[*]")
def _click_command(state: State):
......
......@@ -13,12 +13,12 @@
"""Custom cluster upgrade specific commands"""
import click
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.config import CONFIG_LEGAL_URL
@click.command()
@click.command(cls=CustomClickCommand)
@handle_cli_exceptions
@command_with_output(
"legalTags[*].{Name:name,Description:description,Classification:properties.securityClassification,PersonalData:properties.personalData,Export:properties.exportClassification,Origin:properties.originator}" # noqa: E501 pylint: disable=C0301
......
......@@ -13,12 +13,12 @@
"""Custom cluster upgrade specific commands"""
import click
from osducli.click_cli import State, command_with_output
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.config import CONFIG_SEARCH_URL
@click.command()
@click.command(cls=CustomClickCommand)
@handle_cli_exceptions
@command_with_output("sort_by(aggregations,&key)[*].{Key:key,Count:count}")
def _click_command(state: State):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment