use std::{env, path::Path};
use clap::Parser;
use roguewave::Session;
#[derive(Debug, Parser)]
struct Command {
destination: String,
#[command(subcommand)]
subcommand: Subcommand,
}
#[derive(Debug, Parser)]
enum Subcommand {
Setup,
Stop,
Start,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info")
}
env_logger::builder()
.format_target(false)
.format_module_path(false)
.init();
env::set_current_dir(Path::new(&env::var("CARGO_MANIFEST_DIR")?).join("examples"))?;
let command: Command = Command::parse();
let mut session = Session::connect(&command.destination).await?;
match command.subcommand {
Subcommand::Setup => {
setup(&mut session).await?;
}
Subcommand::Stop => {
session.command(["pkill", "nginx"]).run().await?;
}
Subcommand::Start => {
session.command(["/usr/sbin/nginx"]).run().await?;
}
}
Ok(())
}
async fn setup(session: &mut Session) -> anyhow::Result<()> {
session.apt().update_package_list().await?;
session.apt().install(&["nginx", "rsync"]).await?;
if session
.path_exists("/etc/nginx/sites-enabled/default")
.await?
{
session
.fs()
.remove_file("/etc/nginx/sites-enabled/default")
.await?;
}
session
.upload(["http_server.conf"], "/etc/nginx/sites-enabled", None)
.await?;
session.upload(["files"], "/var/www", None).await?;
session.command(["/usr/sbin/nginx"]).run().await?;
Ok(())
}