memvid_cli/commands/
binding.rs

1//! Commands for managing memory bindings to dashboard
2
3use std::io::{self, Write};
4use std::path::PathBuf;
5
6use anyhow::Result;
7use clap::Args;
8use memvid_core::Memvid;
9
10/// Arguments for the unbind command
11#[derive(Args)]
12pub struct UnbindArgs {
13    /// Path to the MV2 file
14    pub file: PathBuf,
15
16    /// Skip confirmation prompt
17    #[arg(long)]
18    pub yes: bool,
19}
20
21/// Arguments for the binding command
22#[derive(Args)]
23pub struct BindingArgs {
24    /// Path to the MV2 file
25    pub file: PathBuf,
26
27    /// Output as JSON
28    #[arg(long)]
29    pub json: bool,
30}
31
32/// Handle the unbind command
33pub fn handle_unbind(args: UnbindArgs) -> Result<()> {
34    let mut mem = Memvid::open(&args.file)?;
35
36    if let Some(binding) = mem.get_memory_binding() {
37        let memory_name = binding.memory_name.clone();
38
39        if !args.yes {
40            println!("This will unbind the file from memory '{}'", memory_name);
41            println!("The file will revert to free tier capacity (1 GB).");
42            print!("Continue? [y/N] ");
43            io::stdout().flush()?;
44
45            let mut input = String::new();
46            io::stdin().read_line(&mut input)?;
47
48            if !input.trim().eq_ignore_ascii_case("y") {
49                println!("Aborted.");
50                return Ok(());
51            }
52        }
53
54        mem.unbind_memory()?;
55        mem.commit()?;
56        println!("Successfully unbound from memory '{}'", memory_name);
57    } else {
58        println!("This file is not bound to any memory.");
59    }
60
61    Ok(())
62}
63
64/// Handle the binding command
65pub fn handle_binding(args: BindingArgs) -> Result<()> {
66    let mem = Memvid::open(&args.file)?;
67
68    if let Some(binding) = mem.get_memory_binding() {
69        if args.json {
70            let json = serde_json::json!({
71                "memory_id": binding.memory_id.to_string(),
72                "memory_name": binding.memory_name,
73                "bound_at": binding.bound_at.to_rfc3339(),
74                "api_url": binding.api_url,
75            });
76            println!("{}", serde_json::to_string_pretty(&json)?);
77        } else {
78            println!("Memory Binding:");
79            println!("  Memory ID:   {}", binding.memory_id);
80            println!("  Memory Name: {}", binding.memory_name);
81            println!("  Bound At:    {}", binding.bound_at.to_rfc3339());
82            println!("  API URL:     {}", binding.api_url);
83        }
84    } else if args.json {
85        println!("null");
86    } else {
87        println!("This file is not bound to any memory.");
88    }
89
90    Ok(())
91}