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

71 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
# local
2020-08-12 14:43:13 +00:00
from ._subcommand import SubCommand
2020-08-06 12:34:25 +00:00
2020-08-18 10:28:59 +00:00
# parent (display purposes only)
from .._constants import KIWI_CONF_NAME
from ..config import DefaultConfig, LoadedConfig
2020-08-18 10:28:59 +00:00
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:
2020-08-17 13:03:25 +00:00
result = input(f"{prompt} [{config[key]}] ").strip()
2020-08-11 12:03:00 +00:00
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-19 14:23:52 +00:00
action="Creating",
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
def _run_instance(self, runner, args):
2020-08-13 08:48:01 +00:00
logging.info(f"Initializing '{KIWI_CONF_NAME}' in '{os.getcwd()}'")
config = LoadedConfig.get()
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
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")
2020-08-19 10:02:46 +00:00
user_input(config, 'markers:disabled', "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()
2020-08-17 13:00:05 +00:00
return True