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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Command Config
use crate::{
    registry::Registry,
    result::{Error, Result},
};
use etc::{Etc, Meta};
use std::{path::PathBuf, process::Command};
use structopt::StructOpt;

/// Command Config
#[derive(StructOpt, Debug)]
pub enum Config {
    /// Sets config field
    Set {
        /// Substrate registry
        #[structopt(short)]
        registry: String,
    },
    /// Lists the current config
    List,
    /// Edits the current config
    Edit,
}

/// Exec `config` command
pub fn exec(mut r: Registry, config: Config) -> Result<()> {
    let cur_registry = PathBuf::from(&r.dir);
    let home = Etc::from(
        cur_registry
            .parent()
            .expect("Could not find home dir of sup")
            .parent()
            .expect("Could not find home dir of sup"),
    );

    match config {
        Config::List => {
            println!("{:#?}", &r.config);
            return Ok(());
        }
        Config::Edit => {
            Command::new("vi")
                .arg(if let Some(ref path) = r.config.dir {
                    Etc::from(path).name()?
                } else {
                    home.name()?
                })
                .status()?;
        }
        Config::Set { registry } => {
            if !registry.ends_with(".git") {
                return Err(Error::Sup(format!("Wrong git url: {}", registry)));
            }

            r.config.node.registry = registry;
            r.config.flush()?;
        }
    }

    println!("ok!");
    Ok(())
}