1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Command Config
use crate::{
    registry::Registry,
    result::{Error, Result},
};
use etc::{Etc, FileSystem, Write};
use std::{path::PathBuf, process::Command};

/// Exec `config` command
pub fn exec(edit: bool, git: String) -> Result<()> {
    let mut registry = Registry::new()?;
    let mut should_rewrite = false;
    let cur_registry = PathBuf::from(&registry.dir);
    let home = cur_registry
        .parent()
        .expect("Could not find the home of sup");

    // If with `-e` flag, just edit the config file.
    if edit {
        Command::new("vi")
            .arg(home.to_string_lossy().to_string())
            .status()?;
        return Ok(());
    }

    // If with registry flag, reset the registry
    if !git.is_empty() && git.ne(&registry.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;
    }

    // Re-write the config file
    if should_rewrite {
        Etc::from(&home)
            .open("config.toml")?
            .write(toml::to_string(&registry.config)?)?;

        return Ok(());
    }

    println!("{:#?}", registry.config);
    Ok(())
}