Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rudimentary cli implementation #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/cobbler_tftp/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""

Check notice on line 1 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L1

One-line docstring should fit on one line with quotes (found 3) (D200)
Cobbler-tftp will be managable as a command-line service.
"""

import click
import yaml

from cobbler_tftp.settings import SettingsFactory

with open(
"src/cobbler_tftp/settings/data/settings.yml", "r", encoding="utf-8"
) as stream:
try:
SETTINGS = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)

_context_settings = dict(help_option_names=["-h", "--help"])

Check notice on line 18 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L18

Consider using '{"help_option_names": ['-h', '--help']}' instead of a call to 'dict'. (use-dict-literal)


@click.group(context_settings=_context_settings, invoke_without_command=True)
@click.pass_context
def cli(ctx):
"""
Cobbler-TFTP - Copyright (c) 2022 The Cobbler Team. (github.com/cobbler)\n
Licensed under the terms of the GPLv2.0 license.
"""
if ctx.invoked_subcommand is None:
click.echo(
"No commands given, try 'cobbler-tftp -h' or 'cobbler-tftp --help' to view usage."
)

# Possible default behavior can be invoked here


@cli.command()
@click.option(
"--no-daemon",
"-dd",
is_flag=True,
default=SETTINGS["is_daemon"], # type: ignore
help="Stop cobbler-tftp from running as daemon.",
)
@click.option(
"--enable-automigration",
is_flag=True,
default=SETTINGS["auto_migrate_settings"], # type: ignore
help="Enable auto migration of settings.",
)
@click.option(
"--config", "-c", type=click.Path(), help="Set location of configuration file."
)
@click.option(
"--settings",
"-s",
multiple=True,
help="""Set custom settings in format:\n
<PARENT_YAML_KEY>.<CHILD_YAML_KEY>.<...>.<KEY_NAME>=<VALUE>.\n
Your settings must use single quotes. If a single quote appears within a value it must be escaped.""",
)
def start(daemon: bool, enable_automigration, config, settings):

Check notice on line 61 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L61

Unused argument 'enable_automigration' (unused-argument)

Check notice on line 61 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L61

Unused variable 'enable_automigration' (unused-variable)
"""
Start the cobbler-tftp server.
"""
click.echo(
f"Cobbler-tftpCopyright (c) 2023\nLicensed under the terms of the GPL-v2.0 license.\nInitializing Cobbler-tftp server...\n"

Check notice on line 66 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L66

Using an f-string that does not have any interpolated variables (f-string-without-interpolation)

Check notice on line 66 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L66

line too long (131 > 120 characters) (E501)
)
if daemon:
click.echo(
f"'--daemon' flag set.\nCobbler-tftp will be running as a daemon in the background."

Check notice on line 70 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L70

Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
)
else:
click.echo(f"Server running...")

Check notice on line 73 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L73

Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
settings_factory: SettingsFactory = SettingsFactory()
# settings_file = SettingsFactory.load_config_file(settings_factory, config)
# environment_variables = SettingsFactory.load_env_variables(settings_factory)
# cli_arguments = SettingsFactory.load_cli_options(
# settings_factory, daemon, enable_automigration, settings
# )
application_settings = SettingsFactory.build_settings(

Check warning on line 80 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L80

Unused variable 'application_settings'

Check notice on line 80 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L80

Unused variable 'application_settings' (unused-variable)
settings_factory, config, settings
)


@cli.command()
def version():
"""
Check cobbler-tftp version. If there are any cobbler servers connected their versions will be printed as well.
"""
pass

Check warning on line 90 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L90

Unnecessary pass statement

Check notice on line 90 in src/cobbler_tftp/cli.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/cobbler_tftp/cli.py#L90

Unnecessary pass statement (unnecessary-pass)


@cli.command()
def print_default_config():
"""
Print the default application parameters.
"""
pass


@cli.command()
def stop():
"""
Stop the cobbler-tftp server daemon if it is running
"""
pass


cli.add_command(start)
cli.add_command(version)
cli.add_command(print_default_config)
cli.add_command(stop)
9 changes: 7 additions & 2 deletions src/cobbler_tftp/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ def __init__(self) -> None:
"""Initialize a new Settings dicitionary."""
self._settings_dict: SettingsDict = {}

def build_settings(self, config_path: Optional[Path], cli_flags) -> Settings:
def build_settings(self, config_path: Optional[Path], cli_arguments) -> Settings:
"""
Build new Settings object using parameters from all sources.

:config_path: Path to the configuration file
:type config_path: Path
:cli_flags: List of all CLI configuration options
:type cli_flags: List[str]
:return: Settings object
:rtype: Settings
"""

# Load config file
Expand All @@ -106,7 +111,7 @@ def build_settings(self, config_path: Optional[Path], cli_flags) -> Settings:
self.load_env_variables()

# Load CLI options
self.load_cli_options(cli_flags)
self.load_cli_options(cli_arguments)

if not migrations.validate(self._settings_dict):
raise ValueError(
Expand Down
Loading