soph_console/support/commands/
publish.rs

1use super::stub_path;
2use crate::{
3    async_trait,
4    traits::{ApplicationTrait, CommandTrait},
5    Error, OwoColorize, Result,
6};
7use clap::{ArgMatches, Command};
8use soph_core::support::copy;
9use std::{path::Path, str::FromStr};
10
11#[derive(Debug)]
12pub enum Publish {
13    Config,
14    Deploy,
15}
16
17impl FromStr for Publish {
18    type Err = Error;
19
20    fn from_str(str: &str) -> Result<Self, Self::Err> {
21        match str {
22            "config" => Ok(Self::Config),
23            "deploy" => Ok(Self::Deploy),
24            _ => panic!("Invalid publish command"),
25        }
26    }
27}
28
29#[async_trait]
30impl CommandTrait for Publish {
31    fn command() -> Command {
32        Command::new("publish")
33            .about("Publish stub files from crate")
34            .subcommand(Command::new("config").about("Publish config file from crate"))
35            .subcommand(Command::new("deploy").about("Publish deploy file from crate"))
36    }
37
38    async fn handle<A: ApplicationTrait>(arg_matches: ArgMatches) -> Result<()> {
39        match arg_matches.subcommand() {
40            Some((name, _arg_matches)) => match Self::from_str(name)? {
41                Self::Config => publish("config/.config.toml.example", ".config.toml")?,
42                Self::Deploy => publish("deploy/docker", "./")?,
43            },
44            None => Self::command().print_help()?,
45        }
46
47        Ok(())
48    }
49}
50
51fn publish(from: &str, to: &str) -> Result<()> {
52    let to = Path::new(to);
53
54    copy(&stub_path(from), to)?;
55
56    println!(
57        "{} files from {:?} to {:?} successfully!",
58        "Published".green(),
59        from,
60        to
61    );
62
63    Ok(())
64}