kiwi-scp/kiwi_scp/commands/cmd_init.py

77 lines
2.1 KiB
Python
Raw Normal View History

2021-10-20 12:32:45 +00:00
import logging
import os
from ipaddress import IPv4Network
from pathlib import Path
import click
2021-10-29 11:37:59 +00:00
from .cli import pass_instance
2021-10-20 12:32:45 +00:00
from .._constants import KIWI_CONF_NAME
from ..config import KiwiConfig
2021-10-29 11:37:59 +00:00
from ..instance import Instance
2021-10-20 12:32:45 +00:00
from ..misc import user_query
@click.command(
"init",
2021-10-22 15:43:29 +00:00
short_help="Initializes kiwi-scp",
)
2021-10-25 11:29:12 +00:00
@click.option(
"-o",
"--output",
help=f"initialize a kiwi-scp instance in another directory",
type=click.Path(
path_type=Path,
dir_okay=True,
writable=True,
),
)
2021-10-20 12:32:45 +00:00
@click.option(
"-f/-F",
"--force/--no-force",
help=f"use default values even if {KIWI_CONF_NAME} is present",
)
@click.option(
"-s/-S",
"--show/--no-show",
help=f"show effective {KIWI_CONF_NAME} contents instead",
)
2021-10-25 11:29:12 +00:00
@pass_instance
def cmd(ctx: Instance, output: Path, force: bool, show: bool):
"""Initialize or reconfigure a kiwi-scp instance"""
2021-10-25 11:29:12 +00:00
if output is not None:
ctx.directory = output
current_config = KiwiConfig() if force else ctx.config
2021-10-20 12:32:45 +00:00
if show:
# just show the currently effective kiwi.yml
click.echo_via_pager(current_config.kiwi_yml)
return
# check force switch
if force and os.path.isfile(KIWI_CONF_NAME):
logging.warning(f"Overwriting an existing '{KIWI_CONF_NAME}'!")
# build new kiwi dict
kiwi_dict = current_config.kiwi_dict
kiwi_dict.update({
"version": user_query("kiwi-scp version to use in this instance", current_config.version),
"storage": {
"directory": user_query("local directory for service data", current_config.storage.directory, Path),
},
"network": {
"name": user_query("name for local network hub", current_config.network.name),
"cidr": user_query("CIDRv4 block for local network hub", current_config.network.cidr, IPv4Network),
},
})
2021-10-25 11:29:12 +00:00
# ensure output directory exists
if not os.path.isdir(ctx.directory):
os.mkdir(ctx.directory)
# write out the new kiwi.yml
with open(ctx.directory.joinpath(KIWI_CONF_NAME), "w") as file:
KiwiConfig.parse_obj(kiwi_dict).dump_kiwi_yml(file)