1pub mod config;
2
3pub use anyhow::{Context, Error, anyhow, bail};
4pub use clap::arg;
5pub use clap::{AppSettings, Arg, ArgAction, ArgMatches, Command};
6
7use std::fs::{OpenOptions};
8use std::io::{Write};
9use std::path::PathBuf;
10
11pub use config::{Config, ChronicleConfig};
12pub use config::{dir, backup_dir, config_path, draft_path};
13pub use config::{read_config, write_config};
14
15pub type Cli = clap::Command<'static>;
16pub type CliErr = Error;
17pub type CliRes = Result<(), CliErr>;
18
19pub fn cmd(name: &'static str) -> Cli {
20 Cli::new(name)
21 .dont_collapse_args_in_usage(true)
22 .setting(AppSettings::DeriveDisplayOrder)
23}
24
25pub fn try_get_arg<'a>(args: &'a ArgMatches, name: &str) -> anyhow::Result<&'a String> {
26 args.get_one::<String>(name)
27 .ok_or_else(|| anyhow!("could not get arg `{name}`"))
28}
29
30pub fn append(path: &PathBuf, line: &str) -> CliRes {
31 let mut fd = OpenOptions::new()
32 .create(true)
33 .append(true)
34 .open(path)?;
35
36 let ln = line.to_string() + "\n";
37 fd.write_all(ln.as_bytes())?;
38 Ok(())
39}