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
use crate::*;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Config {
    pub version: String,
    pub git_repos: Vec<String>,
    pub cargo_repos: Vec<String>,
    pub pubspec_repos: Vec<String>,
}
impl Config {
    // fn ok(self) -> std::result::Result<Self, ()> {
    //     Ok(self)
    // }
    pub fn get(path: impl AsRef<Path>) -> Self {
        let content = path.as_ref().to_str().unwrap().get_content();
        toml::from_str(&content).unwrap()
    }
    pub fn set(&self, search_path: PathBuf) -> std::result::Result<(), std::io::Error> {
        std::fs::write(search_path, toml::to_string(self).unwrap())
    }
}
pub fn init(opt: &InitOption) -> std::result::Result<(), std::io::Error> {
    let search_path = opt.base.base_dir.to_config_path();
    let list_opts = ListOption {
        base: opt.base.clone(),
        filter: BaseFilterOptions {
            // kind: FilterKind::Directory,
            name: ".git".to_owned(),
        },
    };
    let git_vec = find_git(&list_opts).unwrap();
    let list_opts = ListOption {
        base: opt.base.clone(),
        filter: BaseFilterOptions {
            // kind: FilterKind::File,
            name: "Cargo.toml".to_owned(),
        },
    };
    let cargo_vec = find_file(&list_opts).unwrap();
    let list_opts = ListOption {
        base: opt.base.clone(),
        filter: BaseFilterOptions {
            // kind: FilterKind::File,
            name: "pubspec.yaml".to_owned(),
        },
    };
    let pubspec_vec = find_file(&list_opts).unwrap();
    let version = get_max_version(&cargo_vec).unwrap_or(Version::parse("0.0.1").unwrap());
    Config {
        version: (&opt)
            .version_opts
            .commit_version
            .to_owned()
            .or(Some(version.to_string()))
            .unwrap(),
        git_repos: git_vec.to_string_vec(),
        cargo_repos: cargo_vec.to_string_vec(),
        pubspec_repos: pubspec_vec.to_string_vec(),
    }
    .set(search_path)
}

pub fn get_max_version(cargo_repos: &Vec<PathBuf>) -> Option<Version> {
    cargo_repos
        .iter()
        .map(|path| {
            let p = path.to_str().unwrap();
            p.cargo_version().unwrap()
        })
        .max()
    // .unwrap()
}
#[test]
fn run_init() {
    let cli = RepoCli::parse_from([KEY_COMMAND, "init", "-d", "d:\\rust\\backend\\sdk1018"]);
    // println!("cli: {:#?}", cli);
    if let Some(Command::Init(opt)) = cli.command {
        init(&opt).unwrap();
    }
    let content = ("d:\\rust\\backend\\sdk1018\\".to_string() + KEY_CONFIG_PATH).get_content();

    insta::assert_debug_snapshot!(content)
}