ayun_console/commands/
publish.rsuse crate::{
traits::{ApplicationTrait, CommandTrait},
STYLES,
};
use ayun_core::{support::copy, Error, Result};
use clap::{ArgMatches, Command};
use std::{path::PathBuf, str::FromStr};
pub enum Publish {
Config,
Deploy,
}
impl FromStr for Publish {
type Err = Error;
fn from_str(str: &str) -> std::result::Result<Self, Self::Err> {
match str {
"config" => Ok(Self::Config),
"deploy" => Ok(Self::Deploy),
_ => Err(Error::Message("Invalid publish command".to_string())),
}
}
}
impl CommandTrait for Publish {
fn command() -> Command {
Command::new("publish")
.styles(STYLES)
.about("Publish stub files from crate")
.subcommand(Command::new("config").about("Publish config file from crate"))
.subcommand(Command::new("deploy").about("Publish deploy file from crate"))
}
fn handle<A: ApplicationTrait>(arg_matches: ArgMatches) -> Result<()> {
match arg_matches.subcommand() {
Some((name, _arg_matches)) => match Self::from_str(name)? {
Self::Config => publish("stubs/config/.config.toml.example", ".config.toml")?,
Self::Deploy => publish("stubs/deploy/docker", "./")?,
},
None => Self::command().print_help()?,
}
Ok(())
}
}
fn stub_path(str: &str) -> Result<PathBuf> {
Ok(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(str))
}
fn publish(from: &str, to: &str) -> Result<()> {
let to = PathBuf::from(to);
tracing::info!("publishing from {:?} to {:?}", from, to);
copy(&stub_path(from)?, &to)?;
tracing::info!("published success");
Ok(())
}