From fedaecfef629d41e5cf3b50ef366bd18e84640b5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 21:51:44 +0000 Subject: [PATCH 1/3] Initial plan From 1e9ce5bd0a51f1d452db5c737ebe685c2cb579be Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 21:53:56 +0000 Subject: [PATCH 2/3] Add create-package tool and update README Co-authored-by: punppis <6446823+punppis@users.noreply.github.com> --- README.md | 51 ++++++++++++++++++++++++++++++- create-package | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100755 create-package diff --git a/README.md b/README.md index 4c06dc0..53480c0 100644 --- a/README.md +++ b/README.md @@ -1 +1,50 @@ -My collection of bash scripts. \ No newline at end of file +My collection of bash scripts. + +## Tools + +### create-package + +A tool to create new package directories with bash script templates. + +**Usage:** + +```bash +# Using command-line arguments +./create-package "My Tool" + +# Using stdin (piped input) +echo "My Tool" | ./create-package +``` + +**Behavior:** + +- Creates a directory with the sanitized package name +- Generates an executable bash script inside the directory with the same name +- The generated script includes a shebang, error handling, and a simple hello message + +**Name Sanitization Rules:** + +Package names are automatically sanitized: +- Converted to lowercase +- Spaces and underscores are replaced with dashes +- Only `[a-z0-9-]` characters are retained (letters, numbers, dashes) +- Multiple consecutive dashes are collapsed into one +- Leading and trailing dashes are removed + +**Examples:** + +```bash +# Creates: my-tool/my-tool +./create-package "My Tool" + +# Creates: hello-world/hello-world +echo "Hello_World" | ./create-package + +# Creates: test123/test123 +./create-package "test123" +``` + +**Error Handling:** + +- If the sanitized name is empty (e.g., only special characters), the script exits with an error +- If a directory with that name already exists, the script exits with an error message \ No newline at end of file diff --git a/create-package b/create-package new file mode 100755 index 0000000..bd3dde7 --- /dev/null +++ b/create-package @@ -0,0 +1,83 @@ +#!/bin/bash +set -euo pipefail + +# create-package: Create a new package directory with a bash script template +# Usage: ./create-package +# echo "" | ./create-package + +# Color helper functions +red() { + if [[ -t 1 ]]; then + echo -e "\033[31m$*\033[0m" >&2 + else + echo "$*" >&2 + fi +} + +green() { + if [[ -t 1 ]]; then + echo -e "\033[32m$*\033[0m" + else + echo "$*" + fi +} + +# Read package name from stdin or CLI args +if [[ $# -gt 0 ]]; then + package_name="$*" +elif [[ ! -t 0 ]]; then + # Reading from pipe + read -r package_name +else + red "Error: Package name required" + echo "Usage: ./create-package " >&2 + echo " echo \"\" | ./create-package" >&2 + exit 1 +fi + +# Sanitize the package name: +# 1. Convert to lowercase +# 2. Replace spaces and underscores with dashes +# 3. Remove any characters that are not [a-z0-9-] +# 4. Collapse multiple consecutive dashes into one +# 5. Remove leading/trailing dashes +sanitized=$(echo "$package_name" | \ + tr '[:upper:]' '[:lower:]' | \ + tr ' _' '-' | \ + sed 's/[^a-z0-9-]//g' | \ + sed 's/-\+/-/g' | \ + sed 's/^-//;s/-$//') + +# Check if sanitized name is empty +if [[ -z "$sanitized" ]]; then + red "Error: Package name '$package_name' results in empty string after sanitization" + echo "Package names must contain at least one alphanumeric character" >&2 + exit 1 +fi + +# Check if directory already exists +if [[ -d "$sanitized" ]]; then + red "Error: Directory '$sanitized' already exists" + exit 1 +fi + +# Create the package directory +mkdir -p "$sanitized" + +# Create the package script +script_path="$sanitized/$sanitized" +cat > "$script_path" < Date: Thu, 29 Jan 2026 21:57:00 +0000 Subject: [PATCH 3/3] Address code review: improve portability and fix race condition Co-authored-by: punppis <6446823+punppis@users.noreply.github.com> --- create-package | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/create-package b/create-package index bd3dde7..7788d98 100755 --- a/create-package +++ b/create-package @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -euo pipefail # create-package: Create a new package directory with a bash script template @@ -61,13 +61,16 @@ if [[ -d "$sanitized" ]]; then exit 1 fi -# Create the package directory -mkdir -p "$sanitized" +# Create the package directory (using mkdir without -p to avoid race condition) +if ! mkdir "$sanitized" 2>/dev/null; then + red "Error: Failed to create directory '$sanitized' (may have been created concurrently)" + exit 1 +fi # Create the package script script_path="$sanitized/$sanitized" cat > "$script_path" <