Last active
May 10, 2026 21:23
-
-
Save totoCZ/6502dc01da296f90d9509f5e667adb11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ic() { | |
| local INCUS_LOG_DIR="${INCUS_LOG_DIR:-/var/log/incus}" | |
| local lines="${INCUS_CONSOLE_LINES:-50}" | |
| local follow=0 | |
| local query="" | |
| while (( $# )); do | |
| case "$1" in | |
| -f|--follow) follow=1; shift ;; | |
| -n) shift; lines="$1"; shift ;; | |
| *) query="$1"; shift ;; | |
| esac | |
| done | |
| # collect instance names from log dirs | |
| local -a candidates | |
| candidates=( "${INCUS_LOG_DIR}"/*/console.log(N) ) | |
| candidates=( "${(@)candidates:h:t}" ) | |
| if (( ${#candidates} == 0 )); then | |
| echo "incus-console: no instances found in ${INCUS_LOG_DIR}" >&2 | |
| return 1 | |
| fi | |
| local name="" | |
| if [[ -z "$query" ]]; then | |
| if (( ${#candidates} == 1 )); then | |
| name="${candidates[1]}" | |
| else | |
| echo "incus-console: available instances:" >&2 | |
| printf ' %s\n' "${candidates[@]}" >&2 | |
| echo -n "instance: " >&2 | |
| read name | |
| fi | |
| else | |
| # fuzzy: substring match, case-insensitive | |
| local -a matches | |
| local c | |
| for c in "${candidates[@]}"; do | |
| [[ "${c:l}" == *"${query:l}"* ]] && matches+=("$c") | |
| done | |
| if (( ${#matches} == 0 )); then | |
| echo "incus-console: no instance matching '${query}'" >&2 | |
| printf ' available: %s\n' "${candidates[@]}" >&2 | |
| return 1 | |
| elif (( ${#matches} == 1 )); then | |
| name="${matches[1]}" | |
| else | |
| # multiple — pick shortest (closest) match | |
| name="${matches[1]}" | |
| for c in "${matches[@]:1}"; do | |
| (( ${#c} < ${#name} )) && name="$c" | |
| done | |
| echo "incus-console: matched '${name}' (ambiguous: ${matches[*]})" >&2 | |
| fi | |
| fi | |
| local log_path="${INCUS_LOG_DIR}/${name}/console.log" | |
| echo "── ${name} ──────────────────────────────────────────────" | |
| if (( follow )); then | |
| [[ ! -f "$log_path" ]] && { echo "incus-console: not found: ${log_path}" >&2; return 1 } | |
| echo "── following ${log_path} (Ctrl-C to stop)" | |
| tail -f "$log_path" | |
| return | |
| fi | |
| # try ring buffer first | |
| local ring | |
| ring="$(incus console "$name" --show-log 2>/dev/null)" | |
| if [[ -n "$ring" ]]; then | |
| echo "$ring" | tail -n "$lines" | |
| return | |
| fi | |
| # fallback to file | |
| if [[ -f "$log_path" ]]; then | |
| local total=$(wc -l < "$log_path") | |
| echo "── console.log fallback — last ${lines}/${total} lines" | |
| tail -n "$lines" "$log_path" | |
| else | |
| echo "incus-console: no data (ring empty, no log file at ${log_path})" >&2 | |
| return 1 | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment