Files
nyaterm/scripts/import-quick-commands.sh
T
Kang 312d53a6f5 feat: Implement quick commands import functionality with scripts and example JSON
This commit introduces two new scripts, `import-quick-commands.ps1` and `import-quick-commands.sh`, for importing quick commands into the NyaTerm database. It also adds an example JSON file, `quick-commands.import.example.json`, demonstrating the expected format for quick commands and categories. Additionally, a new Rust example, `import_quick_commands.rs`, is added to handle the import logic, including options for database path, replacement, dry run, and backup. This enhancement streamlines the process of managing quick commands within the application.
2026-05-12 13:17:19 +08:00

95 lines
1.9 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage:
./scripts/import-quick-commands.sh <commands.json> [--db <nyaterm.redb>] [--replace] [--dry-run] [--no-backup]
Options:
--db, --database-path, -d Path to nyaterm.redb. Defaults to ~/.nyaterm/nyaterm.redb.
--replace Replace existing quick commands instead of merging by id.
--dry-run Validate and print the planned result without writing.
--no-backup Do not create a .bak-* copy before writing.
-h, --help Show this help.
Close NyaTerm before writing the database.
USAGE
}
if [[ $# -eq 0 ]]; then
usage >&2
exit 2
fi
case "${1:-}" in
-h|--help)
usage
exit 0
;;
esac
input_json="$1"
shift
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd -- "$script_dir/.." && pwd)"
manifest_path="$repo_root/src-tauri/Cargo.toml"
cargo_args=(
run
--manifest-path "$manifest_path"
--example import_quick_commands
--
"$input_json"
)
while [[ $# -gt 0 ]]; do
case "$1" in
--db|--database-path|-d)
shift
if [[ $# -eq 0 ]]; then
echo "error: --db requires a path" >&2
exit 2
fi
cargo_args+=(--db "$1")
shift
;;
--replace)
cargo_args+=(--replace)
shift
;;
--dry-run)
cargo_args+=(--dry-run)
shift
;;
--no-backup)
cargo_args+=(--no-backup)
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "error: unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
cargo_bin="${CARGO:-}"
if [[ -z "$cargo_bin" ]]; then
if command -v cargo >/dev/null 2>&1; then
cargo_bin="cargo"
fi
fi
if [[ -z "$cargo_bin" ]] || ! command -v "$cargo_bin" >/dev/null 2>&1; then
echo "error: cargo not found. Install Rust/Cargo or set CARGO=/path/to/cargo." >&2
exit 127
fi
exec "$cargo_bin" "${cargo_args[@]}"