use crate::changelog::TemplateOption;
use crate::version_file_sync::VersionSyncFile;
use crate::EXPECTED_CONFIG_FILE_NAME;
use colored::Colorize;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Config {
pub version: String,
pub commands_that_release: Vec<String>,
pub branch_for_release: bool,
pub changelog_output_selections: Vec<ChangelogOutputOption>,
pub project_repo: Option<String>,
pub version_sync_files: Option<Vec<VersionSyncFile>>,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ChangelogOutputOption {
pub template_option: TemplateOption,
pub output_filepath: String,
}
impl Config {
pub fn create_default() -> Self {
Config {
version: "0.0.1".to_string(),
branch_for_release: false,
commands_that_release: [].to_vec(),
project_repo: None,
changelog_output_selections: vec![ChangelogOutputOption {
template_option: TemplateOption::Markdown,
output_filepath: "CHANGELOG.md".to_string(),
}],
version_sync_files: None,
}
}
}
pub fn load_config() -> Option<Config> {
let config_file = std::fs::read_to_string(EXPECTED_CONFIG_FILE_NAME);
match config_file {
Ok(config_file) => match serde_json::from_str::<Config>(&config_file) {
Ok(config) => Some(config),
Err(msg) => {
println!("🤬Failed to parse config file: {}", msg.to_string().red());
None
}
},
Err(_) => {
println!(
"🤬Failed to read config file: {}. Please run `gitscribe init` to create a new config file.",
EXPECTED_CONFIG_FILE_NAME.red()
);
None
}
}
}