libkelp/lib/cli/
init.rs

1use crate::lib::{
2    config::autoconfig::{get_home_files, get_root_files},
3    fsutil::paths::get_root,
4    structs::{
5        config::KelpDotConfig,
6        packages::{PackageInfo, Packages},
7    },
8};
9use anyhow::Context;
10use kelpdot_macros::*;
11use std::path::Path;
12/// Init and autconfig
13pub fn init() -> anyhow::Result<()> {
14    let root = get_root()?;
15    debug_print!("Root: {}", root);
16    if Path::new(&format!("{}/kelp.yaml", root)).exists() {
17        red_print!("{}/kelp.yaml already exists!", root);
18        std::process::exit(1);
19    }
20    let rootfiles = get_root_files()?;
21    let homefiles = get_home_files()?;
22    let cfg = KelpDotConfig {
23        homefiles: Some(homefiles),
24        rootfiles: Some(rootfiles),
25        postrun: Some(vec![]),
26        prerun: Some(vec![]),
27        packages: Some(Packages {
28            gentoo: Some(PackageInfo {
29                with_file: None,
30                packages: Some(vec![]),
31            }),
32            arch: Some(PackageInfo {
33                with_file: None,
34                packages: Some(vec![]),
35            }),
36            debian: Some(PackageInfo {
37                with_file: None,
38                packages: Some(vec![]),
39            }),
40            fedora: Some(PackageInfo {
41                with_file: None,
42                packages: Some(vec![]),
43            }),
44            gems: Some(PackageInfo {
45                with_file: None,
46                packages: Some(vec![]),
47            }),
48            npm: Some(PackageInfo {
49                with_file: None,
50                packages: Some(vec![]),
51            }),
52        }),
53        postsave: Some(vec![]),
54    };
55    let conf_path = format!("{}/kelp.yaml", root);
56    magenta_print!("[INFO] Config file {} created!", conf_path);
57    std::fs::write(conf_path, serde_yaml::to_string(&cfg)?)
58        .with_context(|| red!("Unable to write new config file!"))?;
59    Ok(())
60}