Add helpful CLI utilities

This commit is contained in:
2022-09-02 14:17:22 -07:00
parent 9b64eda0f5
commit 3eb31e4207
2 changed files with 67 additions and 0 deletions

61
bin/,di Executable file
View File

@@ -0,0 +1,61 @@
#!/usr/bin/env python3
import os
from pathlib import Path
import tempfile
import shutil
import subprocess
DOCKERFILE_TEXT = """
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .
"""
def dockerignore_check(args):
ignore_file = Path(args.file)
if not ignore_file.exists():
print(f"Dockerignore file {ignore_file} doesn't exist.")
with tempfile.TemporaryDirectory() as td:
dpath = Path(td)
docker_path = dpath / "Dockerfile"
ignore_path = dpath / "Dockerfile.dockerignore"
docker_path.write_text(DOCKERFILE_TEXT)
shutil.copyfile(ignore_file, ignore_path)
env = os.environ.copy()
env["DOCKER_BUILDKIT"] = "1"
subprocess.run(
f"docker image build --no-cache -t dockerignore-build-context -f {docker_path} .".split(),
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.run(
"docker run -it --rm dockerignore-build-context".split(),
env=env,
)
subprocess.run(
"docker image rm dockerignore-build-context".split(),
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
",di",
description="Shows all files in a project seen by Docker after applying an ignore file.",
)
parser.add_argument("file")
args = parser.parse_args()
dockerignore_check(args)

6
bin/,nixup Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
pushd $SCRIPT_DIR/../nix &> /dev/null
home-manager switch --flake ".#std"