From ec1d2e87ae5f4ccdfe9831a6eff7a3da98917716 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Tue, 9 Dec 2025 12:15:04 +0000 Subject: [PATCH] - Refactor templates list to allow templates in sub folders --- .codespellrc | 2 +- examples/flux-schnell.yaml | 2 +- examples/upscale/google.yaml | 7 ++ examples/upscale/real-esrgan.yaml | 7 ++ repli | 101 ++++++++++++++++----------- src/bashly.yml | 1 + src/commands/new.sh | 40 ++--------- src/lib/get_templates_list.sh | 22 ++++-- src/lib/select_template.sh | 37 ++++++++++ test/approvals/repli_new_model_exact | 2 +- test/approve | 4 +- 11 files changed, 140 insertions(+), 85 deletions(-) create mode 100644 examples/upscale/google.yaml create mode 100644 examples/upscale/real-esrgan.yaml create mode 100644 src/lib/select_template.sh diff --git a/.codespellrc b/.codespellrc index 0195446..c3dedc7 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,3 +1,3 @@ [codespell] -skip = CHANGELOG.md,sublime* +skip = CHANGELOG.md,sublime*,approvals ; ignore-words-list = bu \ No newline at end of file diff --git a/examples/flux-schnell.yaml b/examples/flux-schnell.yaml index 42dea27..78dafd7 100644 --- a/examples/flux-schnell.yaml +++ b/examples/flux-schnell.yaml @@ -9,7 +9,7 @@ input: # ADDITIONAL OPTIONS: # output_quality: 95 # 0-100 (not relevant to PNG) - # num_output: 1 # 1-4 + # num_outputs: 1 # 1-4 # num_inference_steps: 4 # 1-4 # go_fast: true # seed: 123 diff --git a/examples/upscale/google.yaml b/examples/upscale/google.yaml new file mode 100644 index 0000000..f4dc63c --- /dev/null +++ b/examples/upscale/google.yaml @@ -0,0 +1,7 @@ +# https://replicate.com/google/upscaler + +model: google/upscaler +input: + image: + upscale_factor: x4 # x2 x4 + compression_quality: 92 diff --git a/examples/upscale/real-esrgan.yaml b/examples/upscale/real-esrgan.yaml new file mode 100644 index 0000000..83533f7 --- /dev/null +++ b/examples/upscale/real-esrgan.yaml @@ -0,0 +1,7 @@ +# https://replicate.com/nightmareai/real-esrgan + +model: nightmareai/real-esrgan +input: + image: + scale: 2 # 0 - 10 + face_enhance: false diff --git a/repli b/repli index 3b47f7e..14de956 100755 --- a/repli +++ b/repli @@ -879,15 +879,27 @@ get_model_info() { # src/lib/get_templates_list.sh get_templates_list() { - local search="${1:-.}" # optional search term + local search="${1:-}" + + while IFS= read -r -d '' rel; do + # Strip .yaml + local name="${rel%.yaml}" + + # If not nested, show only the basename + if [[ "$rel" != */* ]]; then + printf '%s\0' "$name" + else + # Keep the relative directory path + printf '%s\0' "$name" + fi - while IFS= read -r -d '' file; do - basename -s .yaml "$file" done < <( - find "$templates_dir" -maxdepth 1 -type f -name '*.yaml' -print0 | + find "$templates_dir" \ + -type f -name '*.yaml' \ + -printf '%P\0' | # <-- %P = path relative to $templates_dir grep -iz "$search" | sort -zV - ) | tr '\n' '\0' + ) } # src/lib/get_unique_filename.sh @@ -1015,6 +1027,45 @@ replace_file_placeholders() { echo "$json" } +# src/lib/select_template.sh +select_template() { + local search="$1" + local exact="$2" + + # Get templates list matching the search + mapfile -d '' templates < <(get_templates_list "$search") + + # No matches + if [[ ${#templates[@]} -eq 0 ]]; then + log error "no matching templates" + return 1 + fi + + if [[ ${#templates[@]} -eq 1 ]]; then + # Exactly one match → auto-select + echo "${templates[0]}" + return + else + # Show interactive menu + show_templates_list "$search" >&2 + + if [[ "$exact" ]]; then + log error "no exact match" + return 1 + fi + + read -rp "Choose a template (1-${#templates[@]}): " choice + + # Validate selection + if ! [[ "$choice" =~ ^[0-9]+$ ]] || ((choice < 1 || choice > ${#templates[@]})); then + log error "invalid selection" + return 1 + fi + + echo "${templates[choice - 1]}" + fi +} + # src/lib/show_templates_list.sh show_templates_list() { local search="${1:-.}" # optional search term @@ -1155,10 +1206,10 @@ yurl() { repli_new_command() { # src/commands/new.sh - search="${args[search]}" - outfile="${args[--use]}" - force="${args[--force]}" - exact="${args[--exact]}" + local search="${args[search]}" + local outfile="${args[--use]}" + local force="${args[--force]}" + local exact="${args[--exact]}" # Do not overwrite unless --force is used if [[ -e "$outfile" && -z "$force" ]]; then @@ -1167,37 +1218,7 @@ repli_new_command() { return 1 fi - # Get templates list matching the search - mapfile -d '' templates < <(get_templates_list "$search") - - # No matches - if [[ ${#templates[@]} -eq 0 ]]; then - log error "no matching templates" - return 1 - fi - - if [[ ${#templates[@]} -eq 1 ]]; then - # Exactly one match → auto-select - selected="${templates[0]}" - else - # Show interactive menu - show_templates_list "$search" - - if [[ "$exact" ]]; then - log error "no exact match" - return 1 - fi - - read -rp "Choose a template (1-${#templates[@]}): " choice - - # Validate selection - if ! [[ "$choice" =~ ^[0-9]+$ ]] || ((choice < 1 || choice > ${#templates[@]})); then - log error "invalid selection" - return 1 - fi - - selected="${templates[choice - 1]}" - fi + selected="$(select_template "$search" "$exact")" # Copy file infile="${templates_dir}/${selected}.yaml" diff --git a/src/bashly.yml b/src/bashly.yml index 66fd580..e078d71 100644 --- a/src/bashly.yml +++ b/src/bashly.yml @@ -36,6 +36,7 @@ variables: value: $REPLICATE_HOST commands: +# - { name: debug, help: Debug, private: true } - name: new alias: ['n', init] help: Create a new configuration file from a tmeplate diff --git a/src/commands/new.sh b/src/commands/new.sh index 3cb9299..9592a2f 100644 --- a/src/commands/new.sh +++ b/src/commands/new.sh @@ -1,7 +1,7 @@ -search="${args[search]}" -outfile="${args[--use]}" -force="${args[--force]}" -exact="${args[--exact]}" +local search="${args[search]}" +local outfile="${args[--use]}" +local force="${args[--force]}" +local exact="${args[--exact]}" # Do not overwrite unless --force is used if [[ -e "$outfile" && -z "$force" ]]; then @@ -10,37 +10,7 @@ if [[ -e "$outfile" && -z "$force" ]]; then return 1 fi -# Get templates list matching the search -mapfile -d '' templates < <(get_templates_list "$search") - -# No matches -if [[ ${#templates[@]} -eq 0 ]]; then - log error "no matching templates" - return 1 -fi - -if [[ ${#templates[@]} -eq 1 ]]; then - # Exactly one match → auto-select - selected="${templates[0]}" -else - # Show interactive menu - show_templates_list "$search" - - if [[ "$exact" ]]; then - log error "no exact match" - return 1 - fi - - read -rp "Choose a template (1-${#templates[@]}): " choice - - # Validate selection - if ! [[ "$choice" =~ ^[0-9]+$ ]] || ((choice < 1 || choice > ${#templates[@]})); then - log error "invalid selection" - return 1 - fi - - selected="${templates[choice - 1]}" -fi +selected="$(select_template "$search" "$exact")" # Copy file infile="${templates_dir}/${selected}.yaml" diff --git a/src/lib/get_templates_list.sh b/src/lib/get_templates_list.sh index d04f126..febc331 100644 --- a/src/lib/get_templates_list.sh +++ b/src/lib/get_templates_list.sh @@ -1,11 +1,23 @@ get_templates_list() { - local search="${1:-.}" # optional search term + local search="${1:-}" + + while IFS= read -r -d '' rel; do + # Strip .yaml + local name="${rel%.yaml}" + + # If not nested, show only the basename + if [[ "$rel" != */* ]]; then + printf '%s\0' "$name" + else + # Keep the relative directory path + printf '%s\0' "$name" + fi - while IFS= read -r -d '' file; do - basename -s .yaml "$file" done < <( - find "$templates_dir" -maxdepth 1 -type f -name '*.yaml' -print0 | + find "$templates_dir" \ + -type f -name '*.yaml' \ + -printf '%P\0' | # <-- %P = path relative to $templates_dir grep -iz "$search" | sort -zV - ) | tr '\n' '\0' + ) } diff --git a/src/lib/select_template.sh b/src/lib/select_template.sh new file mode 100644 index 0000000..24214ca --- /dev/null +++ b/src/lib/select_template.sh @@ -0,0 +1,37 @@ +select_template() { + local search="$1" + local exact="$2" + + # Get templates list matching the search + mapfile -d '' templates < <(get_templates_list "$search") + + # No matches + if [[ ${#templates[@]} -eq 0 ]]; then + log error "no matching templates" + return 1 + fi + + if [[ ${#templates[@]} -eq 1 ]]; then + # Exactly one match → auto-select + echo "${templates[0]}" + return + else + # Show interactive menu + show_templates_list "$search" >&2 + + if [[ "$exact" ]]; then + log error "no exact match" + return 1 + fi + + read -rp "Choose a template (1-${#templates[@]}): " choice + + # Validate selection + if ! [[ "$choice" =~ ^[0-9]+$ ]] || ((choice < 1 || choice > ${#templates[@]})); then + log error "invalid selection" + return 1 + fi + + echo "${templates[choice - 1]}" + fi +} \ No newline at end of file diff --git a/test/approvals/repli_new_model_exact b/test/approvals/repli_new_model_exact index a8ce0ae..1228c58 100644 --- a/test/approvals/repli_new_model_exact +++ b/test/approvals/repli_new_model_exact @@ -3,4 +3,4 @@ Templates in tmp/templates: 1. model1 2. model2 -• error • repli_new_command → no exact match +• error • select_template → no exact match diff --git a/test/approve b/test/approve index 749652c..ba6afdb 100755 --- a/test/approve +++ b/test/approve @@ -18,8 +18,8 @@ if [[ $# -gt 0 ]]; then done else # No args → run all tests - while IFS= read -r file; do + for file in $(find spec -type f -name '*.sh' | sort); do context "$file" source "$file" - done < <(find spec -type f -name '*.sh' | sort) + done fi