From f188d05e566a17e71641cb357440ee7e323672d6 Mon Sep 17 00:00:00 2001 From: ldericher Date: Mon, 10 Aug 2020 11:24:39 +0200 Subject: [PATCH] rework "SubCommand" class members --- example/base.conf | 2 +- example/kiwi.yml | 2 +- src/kiwi/runner.py | 2 +- src/kiwi/subcommands/_utils.py | 4 +--- src/kiwi/subcommands/init.py | 33 ++++++++++++++++++++------------- src/kiwi/subcommands/logs.py | 11 +++++------ src/kiwi/subcommands/show.py | 11 +++++------ 7 files changed, 34 insertions(+), 31 deletions(-) diff --git a/example/base.conf b/example/base.conf index 0c8a157..ac5fcfc 100644 --- a/example/base.conf +++ b/example/base.conf @@ -4,4 +4,4 @@ export SUFFIX_DOWN=.down export DOCKERNET=kiwinet export DOCKERCIDR=10.22.46.0/24 -export TARGETROOT=/var/kiwi +export TARGETROOT=/tmp/kiwi diff --git a/example/kiwi.yml b/example/kiwi.yml index 4a18c26..a41a7a9 100644 --- a/example/kiwi.yml +++ b/example/kiwi.yml @@ -5,7 +5,7 @@ version: '0.1' runtime: - storage: /var/kiwi + storage: /tmp/kiwi env: null markers: diff --git a/src/kiwi/runner.py b/src/kiwi/runner.py index 553f497..a0da00f 100644 --- a/src/kiwi/runner.py +++ b/src/kiwi/runner.py @@ -21,6 +21,6 @@ class Runner: args = Parser.get_args() for cmd in cls.__commands: - if cmd.get_cmd() == args.command: + if cmd.command == args.command: cmd.run() return diff --git a/src/kiwi/subcommands/_utils.py b/src/kiwi/subcommands/_utils.py index c985e57..2501b52 100644 --- a/src/kiwi/subcommands/_utils.py +++ b/src/kiwi/subcommands/_utils.py @@ -4,9 +4,7 @@ from ..config import LoadedConfig class SubCommand: - @classmethod - def get_cmd(cls): - pass + command = None @classmethod def setup(cls): diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py index 4e342d6..2791784 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/init.py @@ -2,14 +2,14 @@ import logging import os from ..core import KIWI_CONF_NAME, Parser -from ..config import DefaultConfig +from ..config import DefaultConfig, LoadedConfig from ._utils import SubCommand def user_input(config, key, prompt): # prompt user as per argument - result = input("{} [Default: {}] ".format(prompt, config[key])).strip() + result = input("{} [{}] ".format(prompt, config[key])).strip() # store result if present if result: @@ -36,27 +36,34 @@ def user_input_exe(config, program_name): class InitCommand(SubCommand): - __parser = None - - @classmethod - def get_cmd(cls): - return 'init' + command = 'init' @classmethod def setup(cls): - cls.__parser = Parser.get_subparsers().add_parser(cls.get_cmd(), help="Create new kiwi-config instance") - # cls.__parser.add_argument('cmd', metavar='command', type=str, help='subcommand to execute') + parser = Parser.get_subparsers().add_parser( + cls.command, + description="Create a new kiwi-config instance" + ) + + parser.add_argument( + '-f', '--force', + action='store_true', + help="Use default values even if {} is present".format(KIWI_CONF_NAME) + ) @classmethod def run(cls): - config = DefaultConfig.get() + logging.info("Initializing kiwi-config instance in '%s'", os.getcwd()) - if os.path.isfile(KIWI_CONF_NAME): + if Parser.get_args().force and os.path.isfile(KIWI_CONF_NAME): logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME) + config = DefaultConfig.get() + else: + config = LoadedConfig.get() - user_input(config, 'version', "Choose kiwi-config version") + user_input(config, 'version', "Enter kiwi-config version for this instance") - user_input(config, 'runtime:storage', "Enter main directory for local data") + user_input(config, 'runtime:storage', "Enter local directory for service data") user_input(config, 'markers:project', "Enter marker string for project directories") user_input(config, 'markers:down', "Enter marker string for disabled projects") diff --git a/src/kiwi/subcommands/logs.py b/src/kiwi/subcommands/logs.py index f9bf622..5494a30 100644 --- a/src/kiwi/subcommands/logs.py +++ b/src/kiwi/subcommands/logs.py @@ -4,15 +4,14 @@ from ._utils import SubCommand, Docker class LogsCommand(SubCommand): - __parser = None - - @classmethod - def get_cmd(cls): - return 'logs' + command = 'logs' @classmethod def setup(cls): - cls.__parser = Parser.get_subparsers().add_parser(cls.get_cmd(), help="Show logs of a project") + parser = Parser.get_subparsers().add_parser( + cls.command, + description="Show logs of a project" + ) @classmethod def run(cls): diff --git a/src/kiwi/subcommands/show.py b/src/kiwi/subcommands/show.py index 593a949..0ae5eb9 100644 --- a/src/kiwi/subcommands/show.py +++ b/src/kiwi/subcommands/show.py @@ -5,15 +5,14 @@ from ._utils import SubCommand class ShowCommand(SubCommand): - __parser = None - - @classmethod - def get_cmd(cls): - return 'show' + command = 'show' @classmethod def setup(cls): - cls.__parser = Parser.get_subparsers().add_parser(cls.get_cmd(), help="Show effective kiwi.yml") + parser = Parser.get_subparsers().add_parser( + cls.command, + description="Show effective kiwi.yml" + ) @classmethod def run(cls):