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.
This commit is contained in:
Kang committed 2026-05-12 13:17:19 +08:00
1 parent d6ca443013
commit 312d53a6f5
5 files changed
+664 -307

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$InputJson,
[string]$DatabasePath,
[switch]$Replace,
[switch]$DryRun,
[switch]$NoBackup
)
$ErrorActionPreference = "Stop"
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
$manifestPath = Join-Path $repoRoot "src-tauri\Cargo.toml"
$cargoArgs = @(
"run",
"--manifest-path",
$manifestPath,
"--example",
"import_quick_commands",
"--",
$InputJson
)
if ($DatabasePath) {
$cargoArgs += @("--db", $DatabasePath)
}
if ($Replace) {
$cargoArgs += "--replace"
}
if ($DryRun) {
$cargoArgs += "--dry-run"
}
if ($NoBackup) {
$cargoArgs += "--no-backup"
}
& cargo @cargoArgs
exit $LASTEXITCODE
+94
View File
@@ -0,0 +1,94 @@
#!/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[@]}"
@@ -0,0 +1,34 @@
{
"categories": [
{
"id": "general",
"name": "General"
},
{
"id": "k8s",
"name": "Kubernetes"
}
],
"commands": [
{
"id": "cmd-list-files",
"label": "List files",
"command": "ls -la",
"category_id": "general",
"description": "List files with details",
"color_tag": "blue",
"icon_tag": "terminal",
"pinned": true,
"execution_mode": "execute",
"source": "manual",
"risk_level": "low"
},
{
"label": "Kubernetes pods",
"command": "kubectl get pods -A",
"category": "Kubernetes",
"execution_mode": "append",
"risk_level": "low"
}
]
}