refactoring, add stub of status and flush commands

cl-status
Ondřej Hruška 4 days ago
parent 4ef329ca4f
commit 04d65a47c3
  1. 51
      src/action_init.rs
  2. 37
      src/main.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(())
}

@ -1,12 +1,10 @@
use crate::action_init::{ClInit, cl_init};
use crate::action_log::cl_log; use crate::action_log::cl_log;
use crate::action_pack::cl_pack; use crate::action_pack::cl_pack;
use crate::config::Config; use crate::config::Config;
use crate::store::Store;
use anyhow::bail; use anyhow::bail;
use clap::builder::NonEmptyStringValueParser; use clap::builder::NonEmptyStringValueParser;
use colored::Colorize; use colored::Colorize;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
@ -17,6 +15,8 @@ mod git;
mod action_log; mod action_log;
mod action_pack; mod action_pack;
mod action_init;
mod store; mod store;
#[derive(Debug)] #[derive(Debug)]
@ -65,6 +65,10 @@ fn main_try() -> anyhow::Result<()> {
.subcommand(clap::Command::new("add") .subcommand(clap::Command::new("add")
.visible_alias("log") .visible_alias("log")
.about("Add a changelog entry on the current branch")) .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) .subcommand_required(false)
.arg(clap::Arg::new("CONFIG") .arg(clap::Arg::new("CONFIG")
.short('c') .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 let config_path = root.join(&config_file_name); // if absolute, it is replaced by it
if let Some(("init", _)) = args.subcommand() { if let Some(("init", _)) = args.subcommand() {
let mut default_config = Config::default(); return cl_init(ClInit {
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 {
binary_name, binary_name,
config: default_config,
root, root,
}; config_path,
let _ = Store::new(&ctx, true)?; });
println!("{}", "Changelog initialized.".green());
return Ok(());
} }
// Load and parse config // Load and parse config
let config: Config = if let Ok(config_file_content) = std::fs::read_to_string(&config_path) { let config: Config = if let Ok(config_file_content) = std::fs::read_to_string(&config_path) {
match toml::from_str(&config_file_content) { match toml::from_str(&config_file_content) {
Ok(config) => config, Ok(config) => config,

Loading…
Cancel
Save