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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use colored::Colorize;
use term_table::row::Row;
use term_table::table_cell::{Alignment, TableCell};
use term_table::{Table, TableStyle};

use support_common::config::{Config, Names};
use support_terminal::output::{self, OutputFormat};

use crate::command::types::{RegistryOpt, RegistryType};
use crate::config::BridgerConfig;

/// Handle registry command
pub fn handle_registry(opt: RegistryOpt) -> color_eyre::Result<()> {
    match opt {
        RegistryOpt::Set { type_, path } => handle_set(type_, path),
        RegistryOpt::Get { output } => handle_get(output),
        RegistryOpt::Version { value, bundle } => handle_version(value, bundle),
    }
}

fn handle_version(value: Option<String>, bundle: bool) -> color_eyre::Result<()> {
    let bundle_version = env!("CARGO_PKG_VERSION");
    if bundle {
        let mut config: BridgerConfig = Config::restore(Names::Bridger)?;
        config.registry.version = Some(bundle_version.to_string());
        Config::store(Names::Bridger, config)?;
        output::output_text(bundle_version);
        return Ok(());
    }

    if value.is_none() {
        let config: BridgerConfig = Config::restore(Names::Bridger)?;
        output::output_text(
            config
                .registry
                .version
                .unwrap_or_else(|| bundle_version.to_string()),
        );
        return Ok(());
    }

    let mut config: BridgerConfig = Config::restore(Names::Bridger)?;
    config.registry.version = value.clone();
    Config::store(Names::Bridger, config)?;
    output::output_text(value.expect("Unreachable"));
    Ok(())
}

fn handle_set(type_: RegistryType, mut path: Option<String>) -> color_eyre::Result<()> {
    if type_ == RegistryType::Github && path.is_none() {
        path = Some("https://github.com/darwinia-network/bridger".to_string());
    }
    if type_ != RegistryType::Local && path.is_none() {
        output::output_err_and_exit("Please provide `--path <path>`");
    }
    let mut config: BridgerConfig = Config::restore(Names::Bridger)?;
    tracing::trace!(
        target: "bridger",
        "Set registry [{}]{}",
        format!("{:?}", type_).cyan(),
        if let Some(v) = &path { format!(": {}", v) } else { Default::default() }
    );
    config.registry.type_ = type_;
    config.registry.path = path;
    Config::store(Names::Bridger, config)?;
    output::output_ok();
    Ok(())
}

fn handle_get(out: OutputFormat) -> color_eyre::Result<()> {
    let config: BridgerConfig = Config::restore(Names::Bridger)?;
    match out {
        OutputFormat::Raw => {
            let type_ = config.registry.type_;
            output::output_text(format!("{}: {:?}", "TYPE".bold(), &type_));
            if type_ != RegistryType::Local {
                output::output_text(format!(
                    "{}: {}",
                    "PATH".bold(),
                    config.registry.path.unwrap_or_default()
                ));
            }
        }
        OutputFormat::Json => {
            let json = serde_json::to_string_pretty(&config.registry)?;
            output::output_text(json);
        }
        OutputFormat::Table => {
            let registry = config.registry;
            let mut table = Table::new();
            table.max_column_width = 40;
            table.separate_rows = false;
            table.style = TableStyle::empty();
            table.add_row(Row::new(vec![
                TableCell::new("Type".bold()),
                TableCell::new("Path".bold()),
            ]));
            table.add_row(Row::new(vec![
                TableCell::new_with_alignment(format!("{:?}", registry.type_), 2, Alignment::Left),
                TableCell::new_with_alignment(
                    registry.path.unwrap_or_default(),
                    2,
                    Alignment::Left,
                ),
            ]));
            output::output_text(table.render());
        }
    }
    Ok(())
}