KiwiCTX -> Instance, "kiwi init -o"

This commit is contained in:
Jörn-Michael Miehe 2021-10-25 13:29:12 +02:00
parent 7388b3f8ea
commit 5b9ed4259e
3 changed files with 26 additions and 25 deletions

View file

@ -1,11 +1,7 @@
import os
from pathlib import Path
import attr
import click
from ..config import Config
class KiwiCLI(click.MultiCommand):
"""Command Line Interface spread over multiple files in this directory"""
@ -27,19 +23,3 @@ class KiwiCLI(click.MultiCommand):
except ImportError:
return
return mod.cmd
@attr.s
class KiwiCTX:
"""this class is used as the commands' shared context"""
instance: Path = attr.ib(factory=lambda: Path('.'))
@property
def config(self) -> Config:
"""shorthand: get the current configuration"""
return Config.from_instance(self.instance)
pass_kiwi_ctx = click.make_pass_decorator(KiwiCTX, ensure=True)

View file

@ -5,9 +5,9 @@ from pathlib import Path
import click
from .cli import KiwiCTX, pass_kiwi_ctx
from .._constants import KIWI_CONF_NAME
from ..config import Config
from ..instance import Instance, pass_instance
from ..misc import user_query
@ -15,6 +15,16 @@ from ..misc import user_query
"init",
short_help="Initializes kiwi-scp",
)
@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,
),
)
@click.option(
"-f/-F",
"--force/--no-force",
@ -25,10 +35,13 @@ from ..misc import user_query
"--show/--no-show",
help=f"show effective {KIWI_CONF_NAME} contents instead",
)
@pass_kiwi_ctx
def cmd(ctx: KiwiCTX, force: bool, show: bool):
@pass_instance
def cmd(ctx: Instance, output: Path, force: bool, show: bool):
"""Initialize or reconfigure a kiwi-scp instance"""
if output is not None:
ctx.directory = output
current_config = Config() if force else ctx.config
if show:
@ -53,6 +66,10 @@ def cmd(ctx: KiwiCTX, force: bool, show: bool):
},
})
# write out as new kiwi.yml
with open(ctx.instance.joinpath(KIWI_CONF_NAME), "w") as file:
# 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:
file.write(Config.parse_obj(kiwi_dict).kiwi_yml)

View file

@ -4,6 +4,7 @@ from pathlib import Path
from typing import List, Dict, Any, Generator
import attr
import click
import yaml
from ._constants import COMPOSE_FILE_NAME
@ -73,3 +74,6 @@ class Instance:
Project.from_directory(self.directory.joinpath(project.name))
for project in self.config.projects
)
pass_instance = click.make_pass_decorator(Instance, ensure=True)