From 12735aa1c503c151ede3cce1ecd381654891ce9e Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 28 Jul 2023 21:31:23 +0200 Subject: [PATCH] feat: update template for cli Signed-off-by: kjuulh --- rust-cli/Cargo.toml | 5 +- .../crates/{service => %%name%%}/.gitignore | 0 .../crates/{service => %%name%%}/Cargo.toml | 1 - rust-cli/crates/%%name%%/src/main.rs | 31 ++++++++++++ rust-cli/crates/service/src/main.rs | 49 ------------------- 5 files changed, 33 insertions(+), 53 deletions(-) rename rust-cli/crates/{service => %%name%%}/.gitignore (100%) rename rust-cli/crates/{service => %%name%%}/Cargo.toml (91%) create mode 100644 rust-cli/crates/%%name%%/src/main.rs delete mode 100644 rust-cli/crates/service/src/main.rs diff --git a/rust-cli/Cargo.toml b/rust-cli/Cargo.toml index d70b248..746b743 100644 --- a/rust-cli/Cargo.toml +++ b/rust-cli/Cargo.toml @@ -1,9 +1,9 @@ [workspace] -members = ["crates/service"] +members = ["crates/%%name%%"] resolver = "2" [workspace.dependencies] -service = { path = "crates/service" } +%%name%% = { path = "crates/%%name%%" } anyhow = { version = "1.0.71" } tokio = { version = "1", features = ["full"] } @@ -11,4 +11,3 @@ tracing = { version = "0.1", features = ["log"] } tracing-subscriber = { version = "0.3.17" } clap = { version = "4.3.4", features = ["derive", "env"] } dotenv = { version = "0.15.0" } -axum = { version = "0.6.18" } diff --git a/rust-cli/crates/service/.gitignore b/rust-cli/crates/%%name%%/.gitignore similarity index 100% rename from rust-cli/crates/service/.gitignore rename to rust-cli/crates/%%name%%/.gitignore diff --git a/rust-cli/crates/service/Cargo.toml b/rust-cli/crates/%%name%%/Cargo.toml similarity index 91% rename from rust-cli/crates/service/Cargo.toml rename to rust-cli/crates/%%name%%/Cargo.toml index b184564..129a2a5 100644 --- a/rust-cli/crates/service/Cargo.toml +++ b/rust-cli/crates/%%name%%/Cargo.toml @@ -10,4 +10,3 @@ tracing.workspace = true tracing-subscriber.workspace = true clap.workspace = true dotenv.workspace = true -axum.workspace = true diff --git a/rust-cli/crates/%%name%%/src/main.rs b/rust-cli/crates/%%name%%/src/main.rs new file mode 100644 index 0000000..ff3b4a3 --- /dev/null +++ b/rust-cli/crates/%%name%%/src/main.rs @@ -0,0 +1,31 @@ +use std::net::SocketAddr; + +use clap::{Parser, Subcommand}; + +#[derive(Parser)] +#[command(author, version, about, long_about = None, subcommand_required = true)] +struct Command { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + Init +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + dotenv::dotenv().ok(); + tracing_subscriber::fmt::init(); + + let cli = Command::parse(); + + match cli.command.unwrap() { + Commands::Init => { + tracing::info!("hello %%name%%"); + } + } + + Ok(()) +} diff --git a/rust-cli/crates/service/src/main.rs b/rust-cli/crates/service/src/main.rs deleted file mode 100644 index 33f7c40..0000000 --- a/rust-cli/crates/service/src/main.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::net::SocketAddr; - -use axum::routing::get; -use axum::Router; -use clap::{Parser, Subcommand}; - -#[derive(Parser)] -#[command(author, version, about, long_about = None, subcommand_required = true)] -struct Command { - #[command(subcommand)] - command: Option, -} - -#[derive(Subcommand)] -enum Commands { - Serve { - #[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")] - host: SocketAddr, - }, -} - -#[tokio::main] -async fn main() -> anyhow::Result<()> { - dotenv::dotenv().ok(); - tracing_subscriber::fmt::init(); - - let cli = Command::parse(); - - match cli.command { - Some(Commands::Serve { host }) => { - tracing::info!("Starting service"); - - let app = Router::new().route("/", get(root)); - - tracing::info!("listening on {}", host); - axum::Server::bind(&host) - .serve(app.into_make_service()) - .await - .unwrap(); - } - None => {} - } - - Ok(()) -} - -async fn root() -> &'static str { - "Hello, Service!" -}