use crate::{
registry::Registry,
result::{Error, Result},
};
use etc::{Etc, FileSystem, Write};
use std::{path::PathBuf, process::Command};
pub fn exec(edit: bool, git: String) -> Result<()> {
let mut registry = Registry::new()?;
let mut should_rewrite = false;
let cur_registry = PathBuf::from(®istry.dir);
let home = cur_registry
.parent()
.expect("Could not find the home of sup");
if edit {
Command::new("vi")
.arg(home.to_string_lossy().to_string())
.status()?;
return Ok(());
}
if !git.is_empty() && git.ne(®istry.config.node.registry) {
if !git.ends_with(".git") {
return Err(Error::Sup(format!("Wrong git url: {}", git)));
}
should_rewrite = true;
registry.config.node.registry = git;
}
if should_rewrite {
Etc::from(&home)
.open("config.toml")?
.write(toml::to_string(®istry.config)?)?;
return Ok(());
}
println!("{:#?}", registry.config);
Ok(())
}