From 3eb31e4207d0c53bbdedf59d964af6deaf1a9179 Mon Sep 17 00:00:00 2001 From: Tim McCarthy Date: Fri, 2 Sep 2022 14:17:22 -0700 Subject: [PATCH] Add helpful CLI utilities --- bin/,di | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/,nixup | 6 ++++++ 2 files changed, 67 insertions(+) create mode 100755 bin/,di create mode 100755 bin/,nixup diff --git a/bin/,di b/bin/,di new file mode 100755 index 0000000..ab0b860 --- /dev/null +++ b/bin/,di @@ -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) diff --git a/bin/,nixup b/bin/,nixup new file mode 100755 index 0000000..b22c91f --- /dev/null +++ b/bin/,nixup @@ -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"