2020-08-06 12:34:25 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
from .._constants import KIWI_CONF_NAME
|
|
|
|
from ..config import DefaultConfig
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-12 14:43:13 +00:00
|
|
|
from ._subcommand import SubCommand
|
2020-08-06 12:34:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def user_input(config, key, prompt):
|
|
|
|
# prompt user as per argument
|
2020-08-11 12:03:00 +00:00
|
|
|
try:
|
|
|
|
result = input("{} [{}] ".format(prompt, config[key])).strip()
|
|
|
|
except EOFError:
|
|
|
|
print()
|
|
|
|
result = None
|
2020-08-06 12:34:25 +00:00
|
|
|
|
|
|
|
# store result if present
|
|
|
|
if result:
|
|
|
|
config[key] = result
|
|
|
|
|
|
|
|
|
|
|
|
class InitCommand(SubCommand):
|
2020-08-10 13:48:15 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
|
|
|
'init',
|
2020-08-10 09:24:39 +00:00
|
|
|
description="Create a new kiwi-config instance"
|
|
|
|
)
|
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
self._sub_parser.add_argument(
|
2020-08-10 09:24:39 +00:00
|
|
|
'-f', '--force',
|
|
|
|
action='store_true',
|
2020-08-10 14:35:38 +00:00
|
|
|
help=f"use default values even if {KIWI_CONF_NAME} is present"
|
2020-08-10 09:24:39 +00:00
|
|
|
)
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
def run(self, config, args):
|
2020-08-10 12:05:19 +00:00
|
|
|
logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'")
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
if args.force and os.path.isfile(KIWI_CONF_NAME):
|
2020-08-10 12:50:47 +00:00
|
|
|
logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!")
|
2020-08-10 09:24:39 +00:00
|
|
|
config = DefaultConfig.get()
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
try:
|
|
|
|
# version
|
|
|
|
user_input(config, 'version', "Enter kiwi-config version for this instance")
|
|
|
|
|
|
|
|
# runtime
|
|
|
|
user_input(config, 'runtime:storage', "Enter local directory for service data")
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
# markers
|
|
|
|
user_input(config, 'markers:project', "Enter marker string for project directories")
|
|
|
|
user_input(config, 'markers:down', "Enter marker string for disabled projects")
|
2020-08-08 17:41:11 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
# network
|
|
|
|
user_input(config, 'network:name', "Enter name for local docker network")
|
|
|
|
user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
config.save()
|
2020-08-06 12:34:25 +00:00
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
logging.warning(f"'{self}' aborted, input discarded.")
|