sneakerweb 1.2.0

A parallel web transported by physical media
use clap::Parser;
use std::process::ExitCode;

mod block;
mod commands;
mod domain;
mod export;
mod import;
mod logo;
mod publish;
mod pwd;
mod remove;
mod serve;
mod util;

use commands::*;
use util::*;

use crate::domain::DomainCommands;

fn main() -> ExitCode {
    let cli = Cli::parse();
    let outcome = match &cli.command {
        Commands::Domain(domain_args) => {
            match &domain_args.command {
                Some(DomainCommands::List(list_args)) => {
                    smol::block_on(async { domain::list_domains(list_args).await })
                }
                None => {
                    // Generate a new subspace keypair and present it to the user
                    domain::generate_domain(domain_args);
                    Ok(())
                }
            }
        }
        Commands::Publish(publish_args) => {
            // Verify a domain keypair, then recurse through the given filesystem location,
            // writing entries to that domain for any files encountered.
            //
            // Skip files matching globs in ".nopublish" files, and the ".nopublish" files themselves.
            smol::block_on(async {
                publish::publish(publish_args).await?;
                yay("publishing complete!").await;
                Ok(())
            })
        }
        Commands::Block(block_args) => {
            // Forget the given subspace ID from persistent storage.
            // And add it to a blocklist, which for now could maybe just be a line delimited text file.
            smol::block_on(async {
                block::block(block_args).await?;
                yay("domain blocked").await;
                Ok(())
            })
        }
        Commands::Remove(remove_args) => {
            // Forget the given subspace ID from persistent storage without adding it to a blocklist.
            smol::block_on(async {
                remove::remove(remove_args).await?;
                yay("domain(s) removed").await;
                Ok(())
            })
        }
        Commands::Import(import_args) => {
            // Import the contents of a drop into the persistent store.
            smol::block_on(async {
                import::import_sneak(import_args).await?;
                yay(&format!(
                    "successfully imported from {}",
                    import_args.src.to_string_lossy()
                ))
                .await;
                Ok(())
            })
        }
        Commands::Export(export_args) => {
            // Export the contents of the persistent store into a willow drop.
            smol::block_on(async {
                let dest = export::export_sneak(export_args).await?;
                yay(&format!("sneak exported to {}", dest.to_string_lossy())).await;
                Ok(())
            })
        }
        Commands::Serve(serve_args) => {
            smol::block_on(async { serve::start_server(serve_args).await })
        }
        Commands::Pwd(args) => smol::block_on(async { pwd::pwd(args).await }),
    };

    match outcome {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => smol::block_on(async {
            oh_no(&format!("{}", err)).await;
            ExitCode::FAILURE
        }),
    }
}