Files
spacedrive/packages/swift-client/Examples/LibraryManagementExample.swift
Jamie Pine d78bd7b5ea feat: Add Library Management Example and enhance SpacedriveClient with library operations
- Introduced a new example file `LibraryManagementExample.swift` demonstrating library management features such as switching, creating, and clearing libraries.
- Enhanced `SpacedriveClient` with methods for managing the current library, including `getCurrentLibraryId`, `setCurrentLibrary`, `clearCurrentLibrary`, and improved error handling for library operations.
- Added functionality to switch libraries by ID and name, retrieve current library info, and get jobs associated with the current library, improving usability and functionality of the client.
2025-09-23 20:15:55 -07:00

56 lines
2.2 KiB
Swift

import Foundation
import SpacedriveClient
/// Example demonstrating the new library management features
@main
struct LibraryManagementExample {
static func main() async {
let client = SpacedriveClient(socketPath: "/tmp/spacedrive.sock")
do {
// Check initial state
print("📚 Initial library status: \(client.getCurrentLibraryStatus())")
print("📚 Has active library: \(client.hasActiveLibrary())")
// Get list of available libraries
let libraries = try await client.getLibraries()
print("📚 Available libraries: \(libraries.map { "\($0.name) (\($0.id))" })")
if let firstLibrary = libraries.first {
// Switch to the first library by ID
try await client.switchToLibrary(firstLibrary.id)
print("📚 Switched to library: \(client.getCurrentLibraryStatus())")
// Get jobs for the current library
let jobs = try await client.getCurrentLibraryJobs()
print("📚 Jobs in current library: \(jobs.jobs.count)")
// Get current library info
if let currentInfo = try await client.getCurrentLibraryInfo() {
print("📚 Current library details: \(currentInfo.name) at \(currentInfo.path)")
}
}
// Create a new library and automatically switch to it
let newLibrary = try await client.createAndSwitchToLibrary(
name: "Example Library",
path: "/tmp/example-library"
)
print("📚 Created and switched to new library: \(newLibrary.name) (\(newLibrary.libraryId))")
// Switch to a library by name
if libraries.count > 1 {
try await client.switchToLibrary(named: libraries[1].name)
print("📚 Switched to library by name: \(client.getCurrentLibraryStatus())")
}
// Clear the current library
client.clearCurrentLibrary()
print("📚 Cleared current library: \(client.getCurrentLibraryStatus())")
} catch {
print("❌ Error: \(error)")
}
}
}