Brut, the Brutal Router for Unix Tools

Brut, the Brutal Router for Unix Tools

Brut,Unix 工具的“残酷”路由程序

Brut is a lightweight, shell-based router designed to manage and dispatch commands for Unix-style toolsets. It acts as a central entry point that dynamically resolves subcommands by searching through specific directories, allowing for a modular and extensible command-line interface.

Brut 是一个基于 Shell 的轻量级路由程序,旨在管理和分发 Unix 风格工具集的命令。它作为一个中央入口点,通过搜索特定目录来动态解析子命令,从而实现模块化且可扩展的命令行界面。

#!/bin/sh
set -eu
# Brutal Router for Unix Tools
# https://brut.sh/60d2b6
# Copyright (c) Sam Stephenson

The script begins with standard boilerplate, including the MIT license, which permits free use, modification, and distribution of the software. It emphasizes that the software is provided “as is,” without any warranty, which is standard practice for open-source utility scripts.

该脚本以标准的样板文件开头,包括 MIT 许可证,允许免费使用、修改和分发该软件。它强调该软件按“原样”提供,不提供任何形式的保证,这是开源工具脚本的惯例。

# Get our bearings
OLDENV="$(unset OLDENV; export -p)"
SELF="$(realpath "$0")"
NAME="${0##*/}"
PROGRAM_NAME="${SELF##*/}"
ROOT="${SELF%/*/*}"

# Set up the environment
LIB="$ROOT/lib/$NAME"
LIBEXEC="$ROOT/libexec"
SHARE="$ROOT/share/$PROGRAM_NAME"
VENDOR="$SHARE/vendor"
PATH="$LIB:$LIBEXEC:$VENDOR:$PATH"
export SELF
USAGE=""
unset VERSION

The initialization section sets up the environment variables. It determines the root directory of the toolset and modifies the PATH to include specific lib, libexec, and vendor directories. This ensures that when a command is invoked, the router knows exactly where to look for the corresponding executable files.

初始化部分设置了环境变量。它确定了工具集的根目录,并修改了 PATH 以包含特定的 lib、libexec 和 vendor 目录。这确保了当调用命令时,路由程序能够准确地找到相应的可执行文件。

# ---
# Define shared helpers
error() { ... }
usage() { ... }
# ---
awka() { awk "BEGIN { ARGC = 1; a = ARGV[1] } $2" "$1" }
searchpath() { ... }
namespaces() { ... }
executables() { ... }
commands() { ... }
resolve() { ... }

The core logic relies on several helper functions. searchpath uses the find command to locate executables within the defined paths, while namespaces and executables filter these results to match the current command structure. The resolve function is the heart of the router, mapping a user-provided command string to a specific file path on the system.

核心逻辑依赖于几个辅助函数。searchpath 使用 find 命令在定义的路径中定位可执行文件,而 namespaces 和 executables 则对这些结果进行过滤,以匹配当前的命令结构。resolve 函数是路由程序的核心,它将用户提供的命令字符串映射到系统上的特定文件路径。

# Scan the argument list to find the command name
COMMAND=
args=
index=0
for arg; do
  # ... (argument parsing logic)
done

# Match the command name to an executable in the current namespace;
# if one exists, exec it
if executable="$(resolve "$COMMAND")"; then
  eval "set --$args"
  exec "$executable" "$@"
fi

Finally, the script parses the command-line arguments. It iterates through the input to identify the intended command, attempts to resolve it using the previously defined logic, and if found, uses exec to replace the current shell process with the target executable. If no command is found, it triggers an error or displays the usage instructions.

最后,脚本解析命令行参数。它遍历输入以识别目标命令,尝试使用之前定义的逻辑进行解析;如果找到,则使用 exec 将当前的 Shell 进程替换为目标可执行文件。如果没有找到命令,它会触发错误或显示用法说明。