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
use crate::configs::dirs_and_files;
use serde::{Deserialize, Serialize};
use std::{
    fs::{self, File},
    io::prelude::*,
    path::{Path, PathBuf},
};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Config {
    pub profiles_folder: String,
    pub create_new_profile_from: String,
    pub vs_code_path: String,
    #[serde(default = "bool::default")]
    pub default_current_folder: bool,
    #[serde(default = "bool::default")]
    pub shared_profiles_configs: bool,
}

impl Config {
    fn new() -> Self {
        let bin_name = dirs_and_files::get_bin_or_cmd_name();
        let bin_code_path = dirs_and_files::find_it(bin_name).unwrap_or_default();

        let profiles_dir = dirs_and_files::create_or_get_ena_home_folder()
            .unwrap()
            .join("vs-code-profiles");

        Self {
            create_new_profile_from: "Default".into(),
            profiles_folder: dirs_and_files::path_to_string(profiles_dir),
            vs_code_path: dirs_and_files::path_to_string(bin_code_path),
            default_current_folder: false,
            shared_profiles_configs: false,
        }
    }

    fn get_config_raw() -> Self {
        let mut config = Self::new();

        let config_file_path = get_ena_config_path();

        if let Ok(config_string) = fs::read_to_string(config_file_path) {
            let config_obj = serde_yaml::from_str::<Config>(&config_string[..]);

            if let Ok(config_obj) = config_obj {
                config = config_obj;
            }
        }

        config
    }

    fn create_config() {
        let config_file_path = get_ena_config_path();

        match File::create(config_file_path) {
            Ok(mut file) => {
                let obj = Self::new();

                if let Ok(yml) = serde_yaml::to_string(&obj) {
                    if let Err(why) = file.write_all(yml.as_bytes()) {
                        println!("Config couldn't be written, {:?}", why);
                    }
                }
            }
            Err(why) => println!("Error creating config.yml, {:?}", why),
        }
    }

    pub fn get_config(verbose: bool) -> Self {
        if !Path::new(&get_ena_config_path()).exists() {
            Self::create_config();
        }
        let config = Self::get_config_raw();
        if verbose {
            println!("{:?}", &config)
        }
        config
    }
}

fn get_ena_config_path() -> PathBuf {
    dirs_and_files::create_or_get_ena_home_folder()
        .unwrap()
        .join("config.yml")
}