捐血一袋救人一命

2025年9月21日 星期日

安裝 GEMINI CLI

簡介

Gemini CLI 是一個互動式命令列代理程式,專為軟體工程任務設計。它能夠理解自然語言指令,並透過執行程式碼、讀取/寫入檔案、執行 shell 命令等方式協助您完成工作。

核心功能

1. 程式碼理解與修改
  • 讀取檔案 (read_file, read_many_files): 讀取指定檔案或多個檔案的內容,以理解程式碼邏輯、配置等。
  • 搜尋檔案內容 (search_file_content): 在檔案內容中搜尋特定的正則表達式模式,用於快速定位相關程式碼。
  • 列出目錄 (list_directory): 列出指定目錄下的檔案和子目錄,幫助您了解專案結構。
  • 修改檔案 (replace, write_file): 替換檔案中的特定文字或寫入新內容到檔案中。
2. Shell 命令執行
  • 執行各種 shell 命令,例如:
  • 安裝依賴:npm install, pip install
  • 執行測試:npm test, pytest
  • 版本控制:git status, git diff
  • 啟動服務:node server.js &
  • 安全性提示: 在執行會修改檔案系統或系統狀態的命令前,Gemini 會提供簡要說明,請務必仔細閱讀。
3. 網路搜尋
  • Google 搜尋: 透過 Google 搜尋獲取網路資訊。
  • 網頁內容擷取: 擷取指定 URL 的內容,並可進行摘要或提取特定資料。
4. 記憶功能:儲存使用者提供的特定事實或偏好,以便在未來的互動中提供更個人化的協助。

軟體需求

  • 需要 Nodejs v20 以上版本
  • 需要 x64 系統(因為 Nodejs v20 以上,只支援 x64)

安裝步驟

下載 Node Setup Script setup_lts.x,並且透過 pipeline 執行

curl -fsSl https://deb.nodesource.com/setup_lts.x | sudo -E bash -

以下是 script 內容(請參考)

#!/bin/bash

# Logger Function
log() {
  local message="$1"
  local type="$2"
  local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
  local color
  local endcolor="\033[0m"

  case "$type" in
    "info") color="\033[38;5;79m" ;;
    "success") color="\033[1;32m" ;;
    "error") color="\033[1;31m" ;;
    *) color="\033[1;34m" ;;
  esac

  echo -e "${color}${timestamp} - ${message}${endcolor}"
}

# Error handler function
handle_error() {
  local exit_code=$1
  local error_message="$2"
  log "Error: $error_message (Exit Code: $exit_code)" "error"
  exit $exit_code
}

# Function to check for command availability
command_exists() {
  command -v "$1" &> /dev/null
}

check_os() {
    if ! [ -f "/etc/debian_version" ]; then
        echo "Error: This script is only supported on Debian-based systems."
        exit 1
    fi
}

# Function to Install the script pre-requisites
install_pre_reqs() {
    log "Installing pre-requisites" "info"

    # Run 'apt-get update'
    if ! apt-get update -y; then
        handle_error "$?" "Failed to run 'apt-get update'"
    fi

    # Run 'apt-get install'
    if ! apt-get install -y apt-transport-https ca-certificates curl gnupg; then
        handle_error "$?" "Failed to install packages"
    fi

    if ! mkdir -p /usr/share/keyrings; then
      handle_error "$?" "Makes sure the path /usr/share/keyrings exist or run ' mkdir -p /usr/share/keyrings' with sudo"
    fi

    rm -f /usr/share/keyrings/nodesource.gpg || true
    rm -f /etc/apt/sources.list.d/nodesource.list || true

    # Run 'curl' and 'gpg' to download and import the NodeSource signing key
    if ! curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /usr/share/keyrings/nodesource.gpg; then
      handle_error "$?" "Failed to download and import the NodeSource signing key"
    fi

    # Explicitly set the permissions to ensure the file is readable by all
    if ! chmod 644 /usr/share/keyrings/nodesource.gpg; then
        handle_error "$?" "Failed to set correct permissions on /usr/share/keyrings/nodesource.gpg"
    fi
}

# Function to configure the Repo
configure_repo() {
    local node_version=$1

    arch=$(dpkg --print-architecture)
    if [ "$arch" != "amd64" ] && [ "$arch" != "arm64" ] && [ "$arch" != "armhf" ]; then
      handle_error "1" "Unsupported architecture: $arch. Only amd64, arm64, and armhf are supported."
    fi

    echo "deb [arch=$arch signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$node_version nodistro main" | tee /etc/apt/sources.list.d/nodesource.list > /dev/null

    # N|solid Config
    echo "Package: nsolid" | tee /etc/apt/preferences.d/nsolid > /dev/null
    echo "Pin: origin deb.nodesource.com" | tee -a /etc/apt/preferences.d/nsolid > /dev/null
    echo "Pin-Priority: 600" | tee -a /etc/apt/preferences.d/nsolid > /dev/null

    # Nodejs Config
    echo "Package: nodejs" | tee /etc/apt/preferences.d/nodejs > /dev/null
    echo "Pin: origin deb.nodesource.com" | tee -a /etc/apt/preferences.d/nodejs > /dev/null
    echo "Pin-Priority: 600" | tee -a /etc/apt/preferences.d/nodejs > /dev/null

    # Run 'apt-get update'
    if ! apt-get update -y; then
        handle_error "$?" "Failed to run 'apt-get update'"
    else
        log "Repository configured successfully."
        log "To install Node.js, run: apt-get install nodejs -y" "info"
        log "You can use N|solid Runtime as a node.js alternative" "info"
        log "To install N|solid Runtime, run: apt-get install nsolid -y \n" "success"
    fi
}

# Define Node.js version
NODE_VERSION="22.x"

# Check OS
check_os

# Main execution
install_pre_reqs || handle_error $? "Failed installing pre-requisites"
configure_repo "$NODE_VERSION" || handle_error $? "Failed configuring repository"

安裝 Nodejs

sudo apt install nodejs -y
# Install gemini-cli of node package
npm install -g @google/gemini-cli

執行

export GEMINI_API_KEY=
gemini-cli -m gemini-2.5-flash

0 意見: