#!/usr/bin/env bash # spb — SPB Private Cloud CLI # Usage: spb [options] # # Install: copy this file to ~/bin/spb and run: chmod +x ~/bin/spb # Config: spb config (set server URL and API key) set -e CONFIG_DIR="$HOME/.spb" CONFIG_FILE="$CONFIG_DIR/config" DEFAULT_URL="https://cloud.spboucher.ai" # ─── Load config ─────────────────────────────────────────────────────────────── load_config() { if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE" fi } save_config() { mkdir -p "$CONFIG_DIR" chmod 700 "$CONFIG_DIR" cat > "$CONFIG_FILE" < [folder_id]" exit 1 fi if [ ! -f "$file" ]; then echo "❌ File not found: $file" exit 1 fi local filename filename=$(basename "$file") echo " ⬆️ Uploading: $filename" local args=(-s -X POST -H "Authorization: Bearer $SPB_KEY" -F "file=@$file") [ -n "$folder_id" ] && args+=(-F "folderId=$folder_id") local response response=$(curl "${args[@]}" "$SPB_URL/api/v1/files") local name name=$(echo "$response" | grep -o '"name":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -n "$name" ]; then echo " ✅ Uploaded: $name" else echo " ❌ Upload failed:" echo " $response" exit 1 fi } cmd_upload_dir() { require_config local dir="$1" local folder_id="$2" if [ -z "$dir" ] || [ ! -d "$dir" ]; then echo "Usage: spb upload-dir [folder_id]" exit 1 fi # Auto-create a folder with the directory name if no folder_id provided if [ -z "$folder_id" ]; then local dir_name dir_name=$(basename "$dir") echo " 📁 Creating folder: $dir_name" local response response=$(curl -s -X POST \ -H "Authorization: Bearer $SPB_KEY" \ -H "Content-Type: application/json" \ -d "{\"name\":\"$dir_name\"}" \ "$SPB_URL/api/v1/folders") folder_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$folder_id" ]; then echo " ❌ Failed to create folder: $response" exit 1 fi echo " ✅ Folder created (id: $folder_id)" fi local count=0 while IFS= read -r -d '' file; do cmd_upload "$file" "$folder_id" count=$((count + 1)) done < <(find "$dir" -maxdepth 1 -type f -print0) echo "" echo " ✅ Done: $count file(s) uploaded into folder" } cmd_ls() { require_config local folder_id="${1:-}" local url="$SPB_URL/api/v1/files" [ -n "$folder_id" ] && url="$url?folderId=$folder_id" local response response=$(curl -s -H "Authorization: Bearer $SPB_KEY" "$url") echo "" echo " 📁 Files:" echo "$response" | grep -o '"name":"[^"]*"' | cut -d'"' -f4 | while read -r name; do echo " • $name" done echo "" } cmd_folders() { require_config local response response=$(curl -s -H "Authorization: Bearer $SPB_KEY" "$SPB_URL/api/v1/folders") echo "" echo " 📂 Folders:" # Parse id and name pairs echo "$response" | python3 -c " import sys, json data = json.load(sys.stdin) for f in data.get('folders', []): parent = f' (parent: {f[\"parentId\"]})' if f.get('parentId') else '' print(f' {f[\"id\"]} → {f[\"name\"]}{parent}') " 2>/dev/null || echo "$response" | grep -o '"name":"[^"]*"' | cut -d'"' -f4 | while read -r name; do echo " • $name" done echo "" } cmd_help() { cat <<'HELP' spb — SPB Private Cloud CLI Commands: spb config Configure server URL and API key spb upload [folder_id] Upload a file spb upload-dir [folder_id] Upload all files in a directory spb ls [folder_id] List files (optionally in a folder) spb folders List all folders with their IDs spb help Show this help Examples: spb config # URL par défaut: https://cloud.spboucher.ai spb upload rapport.pdf spb upload photo.jpg abc123def spb upload-dir ~/Documents/reports spb ls spb folders HELP } # ─── Main ───────────────────────────────────────────────────────────────────── case "${1:-help}" in config) cmd_config ;; upload) cmd_upload "$2" "$3" ;; upload-dir) cmd_upload_dir "$2" "$3" ;; ls) cmd_ls "$2" ;; folders) cmd_folders ;; help|--help) cmd_help ;; *) echo "Unknown command: $1" cmd_help exit 1 ;; esac