1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 21:03:00 +00:00
kiwi-scp/src/kiwi/subcommands/init.py

68 lines
1.9 KiB
Python
Raw Normal View History

2020-08-12 15:46:50 +00:00
# system
2020-08-06 12:34:25 +00:00
import logging
import os
2020-08-12 15:46:50 +00:00
# parent (display purposes only)
2020-08-11 12:03:00 +00:00
from .._constants import KIWI_CONF_NAME
2020-08-06 12:34:25 +00:00
2020-08-12 15:46:50 +00:00
# local
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):
2020-08-13 08:48:01 +00:00
"""query user for new config value"""
2020-08-06 12:34:25 +00:00
# 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-13 08:48:01 +00:00
"""kiwi init"""
def __init__(self):
super().__init__(
'init',
2020-08-10 09:24:39 +00:00
description="Create a new kiwi-config instance"
)
2020-08-13 08:48:01 +00:00
# -f switch: Initialize with default config
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-17 12:13:09 +00:00
def run(self, runner, config, args):
2020-08-13 08:48:01 +00:00
logging.info(f"Initializing '{KIWI_CONF_NAME}' in '{os.getcwd()}'")
2020-08-06 12:34:25 +00:00
2020-08-13 12:26:49 +00:00
# check force switch
2020-08-11 12:03:00 +00:00
if args.force and os.path.isfile(KIWI_CONF_NAME):
2020-08-12 15:46:50 +00:00
from ..config import DefaultConfig
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-13 12:26:49 +00:00
# version
user_input(config, 'version', "Enter kiwi-config version for this instance")
2020-08-11 12:03:00 +00:00
2020-08-13 12:26:49 +00:00
# runtime
user_input(config, 'runtime:storage', "Enter local directory for service data")
2020-08-06 12:34:25 +00:00
2020-08-13 12:26:49 +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-13 12:26:49 +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-13 12:26:49 +00:00
config.save()