bolt/utils/
bolt_utils.rs

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
111
112
113
use std::fs::read_to_string;
use std::path::Path;

use super::fs_utils;

use colored::Colorize;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WorkspaceConfig {
    pub workspace: String,
    pub source: Vec<String>,
    pub registered: Option<Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProjectConfig {
    pub info: Reference,
    pub policies: Vec<Policy>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Reference {
    pub name: String,
    pub alias: String,
    pub priority: usize,
    pub env: Envrionment,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Envrionment {
    pub from: String,
    pub cli: Option<String>,
    pub pkg_mgr: PkgMgr,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Policy {
    pub name: String,
    pub depends_on: String,
    pub map_to: MapCmd,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PkgMgr {
    pub value: String,
    pub cmds: Cmd,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Cmd {
    pub add: String,
    pub list: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MapCmd {
    pub value: String,
    pub cmd: Option<String>,
}

pub fn get_project_config(dir: &Path) -> ProjectConfig {
    let bolt_config_filename = "boltconfig.json";

    let err_msg = format!(
        "Could not find a boltconfig.json for: {:?}",
        Path::new(dir).to_str().unwrap()
    );

    let contents = get_config(
        &dir.to_str().unwrap().to_owned(),
        bolt_config_filename,
        err_msg.as_str(),
    );
    let config: ProjectConfig = serde_json::from_str(contents.as_str()).unwrap();

    config
}

pub fn get_workspace_config() -> WorkspaceConfig {
    let root_config = "bolt.json";

    let err_msg = format!(
        "Could not find a root bolt.json in current dir. Is your project initialized with bolt?",
    );

    let contents = get_config(&".".to_owned(), root_config, err_msg.as_str());

    let config: WorkspaceConfig = serde_json::from_str(contents.as_str()).unwrap();

    config
}

fn get_config(dir: &String, file_name: &str, err_msg: &str) -> String {
    let path = Path::new(dir);
    let children = fs_utils::read_file_tree(path).unwrap();

    let target: &Vec<_> = &children
        .iter()
        .filter(|c| c.to_ascii_lowercase().to_str() == Some(file_name))
        .collect();

    if target.is_empty() {
        println!("{}", err_msg.red());

        std::process::exit(1)
    } else {
        let config_path = path.join(file_name);
        let contents = read_to_string(config_path).unwrap();

        contents
    }
}