Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion docs/guides/bins-and-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Format: `name:service[;key=value…]`.
|---|---|
| `cmd` | Command inside the service. Default: the bin's name. May contain spaces; it is tokenized at run time. |
| `use` | `exec` (default) attaches to the running container; `run` starts a fresh one with `run --rm`. |
| `desc` | Shown by `xcind-run --list`. |
| `desc` | Labels the entry in `xcind-run --list`. |

Run a bin with `xcind-run <name> [args…]`. Args always append:

Expand Down Expand Up @@ -94,6 +94,35 @@ xcind-run test --group fast
# lint gets no args; phpunit gets --group fast
```

## Listing what is runnable

`xcind-run --list` prints every visible bin and script. Each bin carries
the tail of the Compose command it runs, so you can see where it lands
without opening `.xcind.sh`:

```
bins:
npm (… exec app) Node package manager
composer (… exec app /usr/bin/composer)
phpunit (… run --rm app vendor/bin/phpunit) Run the test suite

scripts:
fresh Reinstall node modules from scratch
-rm -rf node_modules
@npm install
```

- `exec` attaches to the running container. `run --rm` starts a fresh one.
- The command appears only when it differs from the bin's name, so a bin
whose `cmd` is its own name stays short.
- `-T` never appears. The runner decides that at run time from the TTY
state (see [TTY behavior](#tty-behavior)).
- Scripts list their steps verbatim, including any leading `-`.

`--list --names` prints bare names, one per line, with no service, no
description, and no steps. That is the form to parse in scripts and
completions.

## One namespace

Bins and scripts share one namespace: the same name in both is a load-time
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ xcind-run -T <name> [args…] # force -T (also automatic when not a termin
xcind-run @exec <svc> [cmd…] # docker compose exec (no cmd: bash)
xcind-run @run <svc> <cmd…> # docker compose run --rm
xcind-run @compose <args…> # docker compose, args verbatim
xcind-run --list [--names] # list runnable names (leading _ hides an entry)
xcind-run --list # list bins with their compose tail, and scripts with their steps
xcind-run --list --names # bare names only (leading _ hides an entry)
xcind-run --init-shell [--prefix PREFIX] # print x-<name>() wrapper functions
xcind-run --version
```
Expand Down
81 changes: 74 additions & 7 deletions lib/xcind/xcind-run-lib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,35 @@ __xcind-runner-dispatch() {
fi
}

# Build the "(… exec app php artisan)" descriptor for one bin: the tail of
# the docker compose command the bin actually runs. `cmd` is appended only
# when it differs from the bin name, so the common case stays short. `-T`
# is deliberately absent — the runner decides that at run time from the
# TTY state.
# $1 = bin index
__xcind-runner-bin-tail() {
local idx=$1
local name="${__XCIND_RUNNER_BIN_NAMES[$idx]}"
local service="${__XCIND_RUNNER_BIN_SERVICES[$idx]}"
local use="${__XCIND_RUNNER_BIN_USES[$idx]}"
local cmd="${__XCIND_RUNNER_BIN_CMDS[$idx]}"

local verb="exec"
[[ $use == "run" ]] && verb="run --rm"

if [[ $cmd == "$name" ]]; then
printf '(… %s %s)' "$verb" "$service"
else
printf '(… %s %s %s)' "$verb" "$service" "$cmd"
fi
}

# Print the declared bins and scripts. Names with a leading '_' are
# hidden (but stay runnable).
# $1 = 1 to print bare names only
__xcind-runner-list() {
local names_only=${1:-0}
local i count name desc service
local i count name desc step steps tail

if [[ $names_only == "1" ]]; then
for name in ${__XCIND_RUNNER_BIN_NAMES[@]+"${__XCIND_RUNNER_BIN_NAMES[@]}"}; do
Expand All @@ -793,23 +816,60 @@ __xcind-runner-list() {
return 0
fi

local printed_bins=0
# Bins, pass 1: collect the visible entries and their tails, and measure
# the widest name and tail so the description column lines up. Padding is
# emitted as literal spaces, not through printf's field width: printf
# counts that width in bytes while ${#tail} counts characters, and the
# multibyte ellipsis makes the two disagree. Building the pad from
# ${#tail} alone keeps every tail consistent in either locale, because
# they all carry the same "(… " prefix.
local visible_idx=() visible_tail=()
local name_w=20 tail_w=0
i=0
count=${#__XCIND_RUNNER_BIN_NAMES[@]}
while [ "$i" -lt "$count" ]; do
name="${__XCIND_RUNNER_BIN_NAMES[$i]}"
service="${__XCIND_RUNNER_BIN_SERVICES[$i]}"
desc="${__XCIND_RUNNER_BIN_DESCS[$i]}"
if [[ $name != _* ]]; then
tail=$(__xcind-runner-bin-tail "$i")
visible_idx+=("$i")
visible_tail+=("$tail")
[ ${#name} -gt "$name_w" ] && name_w=${#name}
[ ${#tail} -gt "$tail_w" ] && tail_w=${#tail}
fi
i=$((i + 1))
done

# Bins, pass 2: print.
local printed_bins=0
local idx name_pad pad n
i=0
count=${#visible_idx[@]}
while [ "$i" -lt "$count" ]; do
idx="${visible_idx[$i]}"
name="${__XCIND_RUNNER_BIN_NAMES[$idx]}"
desc="${__XCIND_RUNNER_BIN_DESCS[$idx]}"
tail="${visible_tail[$i]}"
i=$((i + 1))
[[ $name == _* ]] && continue
if [[ $printed_bins -eq 0 ]]; then
echo "bins:"
printed_bins=1
fi
name_pad=""
n=$((name_w - ${#name}))
while [ "$n" -gt 0 ]; do
name_pad="$name_pad "
n=$((n - 1))
done
if [[ -n $desc ]]; then
printf ' %-20s %-12s %s\n' "$name" "($service)" "$desc"
pad=""
n=$((tail_w - ${#tail}))
while [ "$n" -gt 0 ]; do
pad="$pad "
n=$((n - 1))
done
printf ' %s%s %s%s %s\n' "$name" "$name_pad" "$tail" "$pad" "$desc"
else
printf ' %-20s %s\n' "$name" "($service)"
printf ' %s%s %s\n' "$name" "$name_pad" "$tail"
fi
done

Expand All @@ -819,6 +879,7 @@ __xcind-runner-list() {
while [ "$i" -lt "$count" ]; do
name="${__XCIND_RUNNER_SCRIPT_NAMES[$i]}"
desc="${__XCIND_RUNNER_SCRIPT_DESCS[$i]}"
steps="${__XCIND_RUNNER_SCRIPT_STEPS[$i]}"
i=$((i + 1))
[[ $name == _* ]] && continue
if [[ $printed_scripts -eq 0 ]]; then
Expand All @@ -833,6 +894,12 @@ __xcind-runner-list() {
else
printf ' %s\n' "$name"
fi
# A script's steps are the only record of where it runs; print them
# verbatim, keeping any leading '-' (ignore-failure).
while IFS= read -r step; do
[[ -z $step ]] && continue
printf ' %s\n' "$step"
done <<<"$steps"
done
return 0
}
Expand Down
47 changes: 47 additions & 0 deletions test/test-xcind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4374,6 +4374,53 @@ names_out=$(__xcind-runner-list 1)
assert_eq "list --names bare" "npm
fresh" "$names_out"

# 17b. --list: bins show the compose tail; cmd only when it differs
runner_setup
XCIND_BINS=(
"npm:app;desc=Node package manager"
"artisan:app;cmd=php artisan;desc=Laravel console"
"psql:db;use=run;desc=Postgres shell"
"frontend-dependency-check:app;desc=Frontend dependency check"
)
# shellcheck disable=SC2034 # read by __xcind-runner-load
XCIND_SCRIPTS=()
__xcind-runner-load
tail_out=$(__xcind-runner-list 0)
assert_contains "list shows exec tail" "(… exec app)" "$tail_out"
assert_contains "list shows cmd when it differs" "(… exec app php artisan)" "$tail_out"
assert_contains "list shows run --rm tail" "(… run --rm db)" "$tail_out"
assert_eq "list omits cmd equal to the name" "false" \
"$(echo "$tail_out" | grep -qF '(… exec app npm)' && echo true || echo false)"
assert_eq "list never prints -T" "false" \
"$(echo "$tail_out" | grep -qF -- '-T' && echo true || echo false)"

# Every description starts in the same column, despite a name longer than
# the 20-character minimum and the multibyte ellipsis in each tail.
assert_eq "list aligns the desc column" "1" \
"$(echo "$tail_out" | awk '
{ for (w = 1; w <= NF; w++)
if ($w == "Node" || $w == "Laravel" || $w == "Postgres" || $w == "Frontend")
print index($0, $w) }' | sort -u | wc -l | tr -d ' ')"
assert_eq "list --names stays bare with a long bin" "npm
artisan
psql
frontend-dependency-check" "$(__xcind-runner-list 1)"

# 17c. --list: script steps print under the script, '-' prefix kept
runner_setup
XCIND_BINS=()
# shellcheck disable=SC2034 # read by __xcind-runner-load
XCIND_SCRIPTS=("fresh:
# Rebuild everything
@exec app composer install
-@exec app php artisan migrate")
__xcind-runner-load
steps_out=$(__xcind-runner-list 0)
assert_contains "list shows script step" "@exec app composer install" "$steps_out"
assert_contains "list keeps the - prefix" "-@exec app php artisan migrate" "$steps_out"
assert_eq "list indents script steps" "true" \
"$(echo "$steps_out" | grep -q '^ @exec app composer install$' && echo true || echo false)"

# 18. Hidden names stay runnable
runner_setup
# shellcheck disable=SC2034 # read by __xcind-runner-load
Expand Down
Loading