feat: add version command with JSON output support

This commit is contained in:
2026-01-08 21:22:50 +02:00
parent 263dd40d89
commit 20526ca42c

71
ollama
View File

@@ -9,6 +9,7 @@ DEFAULT_MODEL="llama3:8b"
CONTEXT_FILE="${OLLAMA_CONTEXT_FILE:-$HOME/.ollama/context.json}"
PROFILE_DIR="${OLLAMA_PROFILE_DIR:-$HOME/.ollama/profiles}"
OUTPUT_FORMAT="text" # Can be "text" or "json"
SCRIPT_VERSION="1.0.0"
# Check for required dependencies
check_dependencies() {
@@ -941,6 +942,7 @@ Commands:
embed <text> [options] Generate embeddings for text (supports stdin)
model <subcommand> Manage models
profile <subcommand> Manage profiles
version Show script and Ollama server version
help [command] Show this help message or help for a specific command
For detailed help on a command, run:
@@ -1078,6 +1080,69 @@ Examples:
EOF
}
help_version() {
cat << 'EOF'
Usage: ollama version [options]
Display the CLI script version and the Ollama server version.
Options:
-o, --output FORMAT Output format: text (default) or json
Examples:
ollama version
ollama version -o json
ollama version -o json | jq '.ollama_version'
EOF
}
# Get version information
version() {
local output_format="$OUTPUT_FORMAT"
# Parse output format flag
while [[ $# -gt 0 ]]; do
case "$1" in
-o|--output)
output_format="$2"
shift 2
;;
*)
shift
;;
esac
done
local password
password=$(get_password)
# Get Ollama server version
local api_response
api_response=$(curl -s -u "$OLLAMA_USER:$password" \
-X GET "$OLLAMA_API_URL/version")
if ! echo "$api_response" | jq . &>/dev/null; then
echo "Error: Invalid response from API" >&2
return 1
fi
if echo "$api_response" | jq -e '.error' &>/dev/null; then
echo "Error: $(echo "$api_response" | jq -r '.error')" >&2
return 1
fi
local ollama_version
ollama_version=$(echo "$api_response" | jq -r '.version // "unknown"')
if [[ "$output_format" == "json" ]]; then
echo "$api_response" | jq --arg cli_version "$SCRIPT_VERSION" '{cli_version: $cli_version, ollama_version: .version}'
else
echo "ollama CLI version $SCRIPT_VERSION"
echo ""
echo "Ollama version $ollama_version"
fi
}
# Main
main() {
local command="${1:-help}"
@@ -1098,6 +1163,9 @@ main() {
profile)
profile "${@:2}"
;;
version)
version "${@:2}"
;;
help|--help|-h)
if [[ $# -gt 1 ]]; then
local help_topic="$2"
@@ -1117,6 +1185,9 @@ main() {
model|models)
help_model
;;
version)
help_version
;;
*)
echo "Error: unknown command '$help_topic'" >&2
return 1