Skip to main content

hexz_cli/cmd/data/
remote.rs

1//! Manage remote endpoints for push/pull operations.
2
3use anyhow::{Context, Result};
4use colored::Colorize;
5use super::workspace::Workspace;
6use crate::args::RemoteCommand;
7
8/// Execute the `hexz remote` command to manage remote endpoints.
9pub fn run(action: RemoteCommand) -> Result<()> {
10    let mut ws = Workspace::find(&std::env::current_dir()?)?
11        .context("Not in a hexz workspace (no .hexz found)")?;
12
13    match action {
14        RemoteCommand::Add { name, url } => {
15            let _ = ws.config.remotes.insert(name.clone(), url.clone());
16            ws.save()?;
17            println!("  {} Added remote {} {}", "✓".green(), name.magenta(), format!("({url})").bright_black());
18        }
19        RemoteCommand::Remove { name } => {
20            if ws.config.remotes.remove(&name).is_some() {
21                ws.save()?;
22                println!("  {} Removed remote {}", "✓".green(), name.magenta());
23            } else {
24                anyhow::bail!("Remote '{name}' not found");
25            }
26        }
27        RemoteCommand::List => {
28            if ws.config.remotes.is_empty() {
29                println!("  {} No remotes configured.", "→".yellow());
30            } else {
31                println!("{} Remotes", "╭".dimmed());
32                let count = ws.config.remotes.len();
33                for (i, (name, url)) in ws.config.remotes.iter().enumerate() {
34                    let prefix = if i == count - 1 { "╰".dimmed() } else { "│".dimmed() };
35                    println!("{} {} {}", prefix, name.magenta(), url.bright_black());
36                }
37            }
38        }
39    }
40
41    Ok(())
42}