diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..6c5dec4 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,48 @@ +# Agents.md + +## Bash + +The following guideline is heavily based on the [Bash Style Guide | ysap.sh](https://style.ysap.sh/), which should be considered the primary reference for style. Adhere to the following principles to ensure scripts are safe, predictable, and maintainable. + +**Shebang:** Start scripts with `#!/usr/bin/env bash` for portability. + +**Quoting:** This is critical. + +- Use double quotes (`"`) for strings that require variable expansion. +- Use single quotes (`'`) for all other literal strings. + +**Always quote variable expansions** (`"$var"`) to prevent word-splitting and globbing issues. + +**Variables:** Theck if the var is used locally or from environment. + +- Use lowercase variable names (e.g., `my_var`). +- Use `local` for all variables inside functions. +- Use uppercase names for environment variables (e.g. `TASK_CONFIG_DIR`) + +**Functions:** + +- Do not use the `function` keyword. +- Use `my-func() { ... }` syntax. +- Function names are in kebab case. + +**Conditionals:** + +- Always use `[[ ... ]]` for conditional testing, not `[ ... ]` or `test`. +- Use `((...))` for arithmetic comparisons (e.g., `((a > b))`). + +**Command Substitution:** + +- Always use `$(...)` for command substitution, not backticks. + +**Arrays:** + +- Use Bash arrays to manage lists of items instead of space-separated strings. +- Iterate over arrays using `for item in "${my_array[@]}"; do ... done`. + +**Avoid External Commands:** + +- Use Bash's built-in parameter expansion for string manipulation (e.g., `${var/foo/bar}`). +- Use globbing (`*`) to iterate over files, not `ls`. +- Avoid `cat` when a command can read a file directly (e.g., `grep "pattern" file`). + +**`eval`:** Never use `eval`. diff --git a/README.md b/README.md index d9627e0..a0c1d63 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,10 @@ The task file functions use the first style. The name of the function starts wit +## Style + +The bash scripts follow the [Bash Style Guide | ysap.sh](https://style.ysap.sh/). The style converstions are + ## Patterns The task file showed above is very basic. Commands can have parameters and call each other. The following is a collection of more complex task file patterns. diff --git a/bin/backup-env-files b/bin/backup-env-files index 34048aa..ecbfd2b 100755 --- a/bin/backup-env-files +++ b/bin/backup-env-files @@ -1,12 +1,12 @@ -#!/bin/bash +#!/usr/bin/env bash -function backup-env-files() { - if test -z "$1"; then echo "\$1 is empty."; exit 1; fi +backup-env-files() { + if [[ -z "$1" ]]; then echo "\$1 is empty."; exit 1; fi - TASK_CONFIG_DIR=${TASK_CONFIG_DIR:-$HOME/.config/taskfile.build} - BACKUP_FILEPATH="$1/$(date +'%Y-%m-%d')-backup-env-files.tar.gz" + local task_config_dir=${TASK_CONFIG_DIR:-$HOME/.config/taskfile.build} + local backup_filepath="$1/$(date +'%Y-%m-%d')-backup-env-files.tar.gz" - echo "Backing up env files to: $BACKUP_FILEPATH" - cd "$TASK_CONFIG_DIR" || exit 1 - tar -czf "$BACKUP_FILEPATH" .env.* + echo "Backing up env files to: $backup_filepath" + cd "$task_config_dir" || exit 1 + tar -czf "$backup_filepath" .env.* } diff --git a/bin/build-image b/bin/build-image index 42a4783..548e4fc 100755 --- a/bin/build-image +++ b/bin/build-image @@ -1,17 +1,17 @@ -#!/bin/bash +#!/usr/bin/env bash -function build-image() { - DOCKERFILE=${DOCKERFILE:="Dockerfile"} +build-image() { + local dockerfile=${DOCKERFILE:="Dockerfile"} - BUILDX_OUTPUT="--load" + local buildx_output="--load" if [[ "$BUILDX_PLATFORM" == *,* ]]; then - BUILDX_OUTPUT="--push" + buildx_output="--push" fi echo "Building Docker image:" SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) docker buildx build . \ - --file "$DOCKERFILE" \ + --file "$dockerfile" \ --platform "$BUILDX_PLATFORM" \ --tag "${CONTAINER_REGISTRY}/${CONTAINER_TAG}" \ - $BUILDX_OUTPUT + $buildx_output } diff --git a/bin/commit-with-llm b/bin/commit-with-llm index c07f628..0d973fa 100755 --- a/bin/commit-with-llm +++ b/bin/commit-with-llm @@ -1,11 +1,11 @@ -#!/bin/bash +#!/usr/bin/env bash -function commit-with-llm() { +commit-with-llm() { mkdir -p tmp - PROMPT_FILE="tmp/llm_update" - echo -e "\nWrite prompt to $PROMPT_FILE." - cat << EOF > "$PROMPT_FILE" + local prompt_file="tmp/llm_update" + echo -e "\nWrite prompt to $prompt_file." + cat << EOF > "$prompt_file" Look at the git log and the git diff below and reply with a git commit message matching the changes and style of the log. The commit message must comply with the conventional commits specification. Reply with the commit message only. @@ -18,30 +18,30 @@ EOF # Run the llm command echo -e "Send prompt with staged git diff and wait for the response of the $(llm models | sed -n 's/^Default: //p') LLM." - RESULT=$(cat "$PROMPT_FILE" | llm) + local result=$(cat "$prompt_file" | llm) # Check if result is empty - if [ -z "$RESULT" ]; then + if [[ -z "$result" ]]; then echo "No response from the model. Exiting." exit 1 fi # Save the result to a file - RESULT_FILE="tmp/llm_update_result" - echo "$RESULT" > "$RESULT_FILE" - echo -e "Saved response to $RESULT_FILE.\n" + local result_file="tmp/llm_update_result" + echo "$result" > "$result_file" + echo -e "Saved response to $result_file.\n" # Show a preview of result file - less "$RESULT_FILE" + less "$result_file" # Ask for confirmation before updating files - read -p "Do you want to commit or edit before commit the message? (y/e/n): " CONFIRM - if [[ ! "$CONFIRM" =~ ^[YyEe]$ ]]; then + read -p "Do you want to commit or edit before commit the message? (y/e/n): " confirm + if [[ ! "$confirm" =~ ^[YyEe]$ ]]; then exit 0 fi - git commit -m "$RESULT" - if [[ "$CONFIRM" =~ ^[Ee]$ ]]; then + git commit -m "$result" + if [[ "$confirm" =~ ^[Ee]$ ]]; then git commit --amend fi }