diff --git a/src/action_init.rs b/src/action_init.rs new file mode 100644 index 0000000..9a0b7df --- /dev/null +++ b/src/action_init.rs @@ -0,0 +1,51 @@ +use crate::config::Config; +use crate::store::Store; +use colored::Colorize; +use std::fs::OpenOptions; +use std::io::Write; +use std::path::PathBuf; + +/// Args for cl_init() +pub struct ClInit { + /// name of the binary, detected from argv/system/env at startup - shown in messages + pub binary_name: String, + /// Root of the project + pub root: PathBuf, + /// Path to the config file to try to read, or to create + pub config_path: PathBuf, +} + +/// Init the changelog system +pub fn cl_init(opts: ClInit) -> anyhow::Result<()> { + let mut default_config = Config::default(); + + if !opts.config_path.exists() { + let mut file = OpenOptions::new() + .write(true) + .create(true) + .open(&opts.config_path)?; + + println!( + "Creating clpack config file: {}", + opts.config_path.display() + ); + file.write_all(toml::to_string_pretty(&default_config)?.as_bytes())?; + } else { + println!( + "Loading existing config file: {}", + opts.config_path.display() + ); + let file_text = std::fs::read_to_string(&opts.config_path)?; + default_config = toml::from_str(&file_text)?; + } + + let ctx = crate::AppContext { + binary_name: opts.binary_name, + config: default_config, + root: opts.root, + }; + let _ = Store::new(&ctx, true)?; + + println!("{}", "Changelog initialized.".green()); + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index a3beacd..9eba35b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,10 @@ +use crate::action_init::{ClInit, cl_init}; use crate::action_log::cl_log; use crate::action_pack::cl_pack; use crate::config::Config; -use crate::store::Store; use anyhow::bail; use clap::builder::NonEmptyStringValueParser; use colored::Colorize; -use std::fs::OpenOptions; -use std::io::Write; use std::path::PathBuf; use std::process::exit; @@ -17,6 +15,8 @@ mod git; mod action_log; mod action_pack; +mod action_init; + mod store; #[derive(Debug)] @@ -65,6 +65,10 @@ fn main_try() -> anyhow::Result<()> { .subcommand(clap::Command::new("add") .visible_alias("log") .about("Add a changelog entry on the current branch")) + .subcommand(clap::Command::new("flush") + .about("Remove all changelog entries that were already released on all channels - clean up the changelog dir. Use e.g. when making a major release where all channel branches are merged.")) + .subcommand(clap::Command::new("status") + .about("Show changelog entries currently waiting for release on the current channel")) .subcommand_required(false) .arg(clap::Arg::new("CONFIG") .short('c') @@ -89,35 +93,14 @@ fn main_try() -> anyhow::Result<()> { let config_path = root.join(&config_file_name); // if absolute, it is replaced by it if let Some(("init", _)) = args.subcommand() { - let mut default_config = Config::default(); - - if !config_path.exists() { - let mut file = OpenOptions::new() - .write(true) - .create(true) - .open(&config_path)?; - - println!("Creating clpack config file"); - file.write_all(toml::to_string_pretty(&default_config)?.as_bytes())?; - } else { - println!("Loading existing config file: {}", config_path.display()); - let file_text = std::fs::read_to_string(&config_path)?; - default_config = toml::from_str(&file_text)?; - } - - let ctx = AppContext { + return cl_init(ClInit { binary_name, - config: default_config, root, - }; - let _ = Store::new(&ctx, true)?; - - println!("{}", "Changelog initialized.".green()); - return Ok(()); + config_path, + }); } // Load and parse config - let config: Config = if let Ok(config_file_content) = std::fs::read_to_string(&config_path) { match toml::from_str(&config_file_content) { Ok(config) => config,