1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 04:43:00 +00:00

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 import os
from pathlib import Path
import attr
import click import click
from ..config import Config
class KiwiCLI(click.MultiCommand): class KiwiCLI(click.MultiCommand):
"""Command Line Interface spread over multiple files in this directory""" """Command Line Interface spread over multiple files in this directory"""
@ -27,19 +23,3 @@ class KiwiCLI(click.MultiCommand):
except ImportError: except ImportError:
return return
return mod.cmd 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 import click
from .cli import KiwiCTX, pass_kiwi_ctx
from .._constants import KIWI_CONF_NAME from .._constants import KIWI_CONF_NAME
from ..config import Config from ..config import Config
from ..instance import Instance, pass_instance
from ..misc import user_query from ..misc import user_query
@ -15,6 +15,16 @@ from ..misc import user_query
"init", "init",
short_help="Initializes kiwi-scp", 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( @click.option(
"-f/-F", "-f/-F",
"--force/--no-force", "--force/--no-force",
@ -25,10 +35,13 @@ from ..misc import user_query
"--show/--no-show", "--show/--no-show",
help=f"show effective {KIWI_CONF_NAME} contents instead", help=f"show effective {KIWI_CONF_NAME} contents instead",
) )
@pass_kiwi_ctx @pass_instance
def cmd(ctx: KiwiCTX, force: bool, show: bool): def cmd(ctx: Instance, output: Path, force: bool, show: bool):
"""Initialize or reconfigure a kiwi-scp instance""" """Initialize or reconfigure a kiwi-scp instance"""
if output is not None:
ctx.directory = output
current_config = Config() if force else ctx.config current_config = Config() if force else ctx.config
if show: if show:
@ -53,6 +66,10 @@ def cmd(ctx: KiwiCTX, force: bool, show: bool):
}, },
}) })
# write out as new kiwi.yml # ensure output directory exists
with open(ctx.instance.joinpath(KIWI_CONF_NAME), "w") as file: 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) 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 from typing import List, Dict, Any, Generator
import attr import attr
import click
import yaml import yaml
from ._constants import COMPOSE_FILE_NAME from ._constants import COMPOSE_FILE_NAME
@ -73,3 +74,6 @@ class Instance:
Project.from_directory(self.directory.joinpath(project.name)) Project.from_directory(self.directory.joinpath(project.name))
for project in self.config.projects for project in self.config.projects
) )
pass_instance = click.make_pass_decorator(Instance, ensure=True)