webcomment/src/cli.rs

74 lines
1.3 KiB
Rust

use clap::Parser;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};
#[derive(Clone, Debug)]
pub struct ConfigPath(pub PathBuf);
impl Default for ConfigPath {
fn default() -> ConfigPath {
ConfigPath(
directories::ProjectDirs::from("tk", "txmn", "webcomment").map_or_else(
|| Path::new(".config").to_path_buf(),
|o| o.config_dir().into(),
),
)
}
}
impl From<&OsStr> for ConfigPath {
fn from(v: &OsStr) -> ConfigPath {
ConfigPath(PathBuf::from(v))
}
}
impl ToString for ConfigPath {
fn to_string(&self) -> String {
String::from(
self.0
.as_path()
.to_str()
.expect("Error: Config dir is not UTF-8!"),
)
}
}
#[derive(Clone, Debug, Parser)]
#[clap(name = "webcomment")]
pub struct MainOpt {
#[clap(flatten)]
pub opt: MainCommonOpt,
/// Subcommand
#[clap(subcommand)]
pub cmd: MainSubcommand,
}
#[derive(Clone, Debug, Parser)]
pub enum MainSubcommand {
/// Initialize config & db
Init,
/// Start server
Start(StartOpt),
/// Add admin password
Psw,
}
#[derive(Clone, Debug, Parser)]
pub struct MainCommonOpt {
/// Directory
#[clap(short, long, default_value_t=ConfigPath::default())]
pub dir: ConfigPath,
}
#[derive(Clone, Debug, Parser)]
pub struct StartOpt {
/// Temporary database
#[clap(short, long)]
pub tmp: bool,
}