use bookstack_exporter::ExportType;
use clap::Parser;
use confique::{Config, File, FileFormat, Partial};
const AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
Load all config values from a custom config file path,
and override export type to use the command line argument instead
<dim>$</dim> <bold>bookstack-exporter --config-path settings.toml --export-type pdf</bold>
<bold>settings.toml:</bold>
bookstack_host = "https://docs.example.com"
output_dir = "export"
export_type = "html"
bookstack_api_token_id = "<<token_id>>"
bookstack_api_token_secret = "<<token_secret>>"
"#
);
#[derive(Parser)]
#[command(author, version, about, long_about = None, after_long_help = AFTER_LONG_HELP)]
pub struct Args {
#[arg(long = "host")]
pub bookstack_host: Option<String>,
#[arg(short, long)]
pub export_type: Option<ExportType>,
#[arg(short, long)]
pub output_dir: Option<String>,
#[arg(short = 'i', long = "api-id")]
pub bookstack_api_token_id: Option<String>,
#[arg(short = 's', long = "api-secret")]
pub bookstack_api_token_secret: Option<String>,
#[arg(short, long)]
pub config_path: Option<String>,
}
#[derive(Config)]
pub struct Conf {
pub bookstack_host: String,
pub export_type: ExportType,
pub output_dir: String,
#[config(env = "BOOKSTACK_API_TOKEN_ID")]
pub bookstack_api_token_id: String,
#[config(env = "BOOKSTACK_API_TOKEN_SECRET")]
pub bookstack_api_token_secret: String,
pub config_path: Option<String>,
}
type PartialConf = <Conf as Config>::Partial;
pub fn load() -> Result<Conf, confique::Error> {
let args = Args::parse();
let mut merged = PartialConf {
bookstack_host: args.bookstack_host,
output_dir: args.output_dir,
bookstack_api_token_id: args.bookstack_api_token_id,
bookstack_api_token_secret: args.bookstack_api_token_secret,
export_type: args.export_type,
config_path: args.config_path.clone(),
};
merged = merged.with_fallback(PartialConf::from_env()?);
if let Some(config_path) = args.config_path {
let from_file = File::with_format(config_path, FileFormat::Toml)
.required()
.load()?;
merged = merged.with_fallback(from_file);
}
Conf::from_partial(merged)
}