use std::io::Write;
use std::fs;
use dialoguer::{theme::ColorfulTheme, Checkboxes};
use failure::Error;
use serde::{Serialize, Deserialize};
use crate::lint::*;
use directories::ProjectDirs;
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub linters: Option<Vec<LinterConfig>>
}
pub fn get_config() -> Result<Config, Error> {
let proj_dirs = ProjectDirs::from("io", "ragone", "lint-emit").unwrap();
let mut file_path = proj_dirs.config_dir().to_path_buf();
file_path.push("config.toml");
let config_path = if file_path.exists() {
file_path
} else {
let default_config: Config = toml::from_str(include_str!("default_config.toml"))?;
let linters = default_config.linters.unwrap();
let linter_names: Vec<&str> = linters
.iter()
.map(|linter| linter.name.as_str())
.collect();
let selections = Checkboxes::with_theme(&ColorfulTheme::default())
.with_prompt("Choose linters [Press SPACE to select]")
.items(&linter_names)
.interact()
.unwrap();
let selected_names: Vec<&str> = selections
.into_iter()
.filter_map(|selection| linter_names.get(selection))
.map(|selection| *selection)
.collect();
let selected_linters: Vec<LinterConfig> = linters
.clone()
.into_iter()
.filter(|linter| {
selected_names.contains(&linter.name.as_str())
})
.collect();
let new_config = Config {
linters: Some(selected_linters)
};
let mut config_file = fs::File::create(file_path.clone())?;
write!(&mut config_file, "{}", toml::to_string(&new_config)?)?;
println!("Successfully wrote configuration file to {:?}", file_path);
file_path
};
let config_string = fs::read_to_string(config_path).expect("Unable to read file");
Ok(toml::from_str(&config_string)?)
}