From 715b578bb9a5fe260bb812a9e368c29df0b11e2e Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Thu, 16 Mar 2023 00:44:51 -0400 Subject: [PATCH] feat: Basic cli sketch --- Cargo.toml | 4 +--- src/bin/cli/init.rs | 22 ++++++++++++++++++ src/bin/cli/mod.rs | 44 ++++++++++++++++++++++++++++++++++++ src/bin/stranger_site_gen.rs | 16 +++++++++++-- 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/bin/cli/init.rs create mode 100644 src/bin/cli/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 400b11d..e698621 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,8 @@ description = "Djot based static site generator" readme = "README.md" license = "MIT" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -clap = { version = "4.1.8", features = ["derive"] } +clap = { version = "4.1.8", features = ["derive", "env"] } color-eyre = "0.6.2" jotdown = "0.1.0" serde = { version = "1.0.156", features = ["derive"] } diff --git a/src/bin/cli/init.rs b/src/bin/cli/init.rs new file mode 100644 index 0000000..7aaf36a --- /dev/null +++ b/src/bin/cli/init.rs @@ -0,0 +1,22 @@ +use clap::Args; +use color_eyre::eyre::Result; +use tracing::instrument; + +use super::GlobalArgs; + +/// Arguments for the init subcommand +#[derive(Args, Debug)] +pub struct InitArgs { + /// Domain name to use + #[arg(long, short)] + pub doman_name: Option, +} + +impl InitArgs { + /// Run the init command + #[instrument] + pub fn run(self, _globals: GlobalArgs) -> Result<()> { + println!("Hello world!"); + Ok(()) + } +} diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs new file mode 100644 index 0000000..b8fb238 --- /dev/null +++ b/src/bin/cli/mod.rs @@ -0,0 +1,44 @@ +use clap::{Args, Parser, Subcommand}; +use color_eyre::eyre::Result; +use std::path::PathBuf; + +mod init; + +pub use init::InitArgs; + +/// Djot based static site generator +#[derive(Parser, Debug)] +#[command(author, version, about, long_about=None)] +pub struct Cli { + #[command(flatten)] + #[command(next_help_heading = "Global Options")] + pub global_args: GlobalArgs, + /// Command to run + #[command(subcommand)] + pub command: Command, +} + +/// Global arguments to the command +#[derive(Args, Debug)] +pub struct GlobalArgs { + /// Path of the site source repository + #[arg(long, value_name = "SITE_PATH", env = "STRANGER_SITE_PATH")] + #[clap(global = true)] + pub site_path: Option, +} + +/// Command to run +#[derive(Subcommand, Debug)] +pub enum Command { + /// Initialize a site + Init(InitArgs), +} + +impl Command { + /// Run the init command + pub fn run(self, globals: GlobalArgs) -> Result<()> { + match self { + Command::Init(args) => args.run(globals), + } + } +} diff --git a/src/bin/stranger_site_gen.rs b/src/bin/stranger_site_gen.rs index e0b5e27..4fd9054 100644 --- a/src/bin/stranger_site_gen.rs +++ b/src/bin/stranger_site_gen.rs @@ -1,3 +1,15 @@ -fn main() { - println!("Hello world!"); +mod cli; + +use cli::Cli; + +use clap::Parser; +use color_eyre::eyre::Result; + +fn main() -> Result<()> { + // Setup color eyre and logging + color_eyre::install()?; + tracing_subscriber::fmt::init(); + + let args = Cli::parse(); + args.command.run(args.global_args) }