Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added help text, show-config command, sane defaults
Browse files Browse the repository at this point in the history
eric-anderton-at-rearc committed Oct 17, 2023
1 parent 13ab424 commit ef0c5ef
Showing 5 changed files with 61 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Setting up a development environment

1. Clone this repo
1. Create a virtualenv: `python3 -m venv venv`
1. Start virtualenv: `source venv/bin/activate`
1. Install dependencies and this project in editable mode: `pip install --editable .`

To run the examples, use the
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Add `setup` feature for modules
- Add build profiles to control the generation of the bakefile(s)
- Add duplicate name check to config file validation
54 changes: 47 additions & 7 deletions docker_printer/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import subprocess
import textwrap
from pathlib import Path
@@ -22,20 +23,31 @@ def version_callback(value: bool):
if value:
typer.echo(__version__)
raise typer.Exit()


def base_dir_callback(value: str = None):
# Change CWD to target so path resolution works as expected
if value:
resoled_value = Path(value).resolve()
os.chdir(str(resoled_value))


@app.callback()
def main(
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
),
base_dir: str = typer.Option(
None, "--basedir", callback=base_dir_callback, is_eager=True
),
):
# Do other global stuff, handle other global options here
return


@app.command()
def synth():
"""Synthesizes new Dockerfiles from configuration."""
_synth()


@@ -57,21 +69,48 @@ def _synth():
bakefile = build_config.generate_bakefile(targets)
with open(bakefile_path, "w", newline="\n") as f:
f.write(bakefile + "\n")
typer.echo(build_config.build_command)
# typer.echo(build_config.build_command)

return targets, build_configs


@app.command()
def build(name: str):
def build(name: str = "default"):
"""Builds the current configuration from synthesized Dockerfile(s)."""
_, build_configs = _synth()

try:
config = next(cfg for cfg in build_configs.__root__ if cfg.name == name)
except StopIteration:
raise typer.Abort(f"No build config found with name '{name}'")
names = ", ".join(set([x.name for x in build_configs.__root__]))
typer.secho(f"Error: No build config found with name '{name}'", fg=typer.colors.RED)
typer.secho(f"Valid names: {names}", fg=typer.colors.YELLOW)
typer.Exit(1)
else:
typer.echo(config.build_command)
subprocess.run(config.build_command, shell=True)

@app.command()
def show_config():
"""List the current config files and build targets."""
preload_modules()

subprocess.run(config.build_command)
target_config_file = targets_file()
build_config_file = builds_file()
targets = TargetCollection.parse_obj(yml_load(target_config_file))
build_configs = BuildConfigCollection.parse_obj(yml_load(build_config_file))

typer.secho("Config files", bold=True, fg=typer.colors.GREEN)
typer.echo(str(target_config_file))
typer.echo(str(build_config_file))

typer.secho("\nTargets", bold=True, fg=typer.colors.GREEN)
for target in targets.__root__:
typer.echo(target)

typer.secho("\nBuilds", bold=True, fg=typer.colors.GREEN)
for build_config in build_configs.__root__:
typer.echo(f"{build_config.name} {build_config.image}")


@app.command()
@@ -82,10 +121,11 @@ def init(
dir_okay=True,
)
):
"""Initializes a new project tree."""
base_dir = path / "docker-printer"
if base_dir.exists():
typer.echo(f"{base_dir} already exists, cannot initialize new project")
raise typer.Abort()
typer.secho(f"Error: {base_dir} already exists, cannot initialize new project", fg=typer.colors.RED)
typer.Exit(1)

base_dir.mkdir(exist_ok=False, parents=False)
(base_dir / "modules").mkdir(exist_ok=False, parents=False)
@@ -98,4 +138,4 @@ def init(
*.rendered.yml
""".lstrip()
)
)
)
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ dependencies = [
"jinja2",
"click",
"pydantic",
"typer",
"rich"
]

[project.readme]
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pyyaml
jinja2
click
pydantic
pydantic==1.*
typer
rich

0 comments on commit ef0c5ef

Please sign in to comment.