From a2f2e303a0cdef7a72adf12b37a71fd38c2488e9 Mon Sep 17 00:00:00 2001 From: Noah Knegt Date: Thu, 8 May 2025 14:09:29 +0200 Subject: [PATCH] Create libs for common code --- Cargo.lock | 24 ++++++++++++++- Cargo.toml | 6 ++-- command-with-spinner/Cargo.toml | 13 +++++++++ command-with-spinner/src/lib.rs | 41 ++++++++++++++++++++++++++ create-worktree/Cargo.toml | 1 + create-worktree/src/main.rs | 52 ++------------------------------- git-ssh-bitwarden/Cargo.toml | 10 +++++++ git-ssh-bitwarden/src/main.rs | 3 ++ git/Cargo.toml | 11 +++++++ git/src/error.rs | 12 ++++++++ git/src/lib.rs | 1 + setup-repo/Cargo.toml | 2 +- setup-repo/src/main.rs | 52 ++------------------------------- 13 files changed, 126 insertions(+), 102 deletions(-) create mode 100644 command-with-spinner/Cargo.toml create mode 100644 command-with-spinner/src/lib.rs create mode 100644 git-ssh-bitwarden/Cargo.toml create mode 100644 git-ssh-bitwarden/src/main.rs create mode 100644 git/Cargo.toml create mode 100644 git/src/error.rs create mode 100644 git/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index fc2da55..321a57b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,6 +131,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "command-with-spinner" +version = "0.1.0" +dependencies = [ + "anyhow", + "colored", + "git", + "indicatif", +] + [[package]] name = "console" version = "0.15.11" @@ -151,6 +161,7 @@ dependencies = [ "anyhow", "clap", "colored", + "command-with-spinner", "dirs", "indicatif", "thiserror", @@ -194,6 +205,17 @@ dependencies = [ "wasi", ] +[[package]] +name = "git" +version = "0.1.0" +dependencies = [ + "thiserror", +] + +[[package]] +name = "git-ssh-bitwarden" +version = "0.1.0" + [[package]] name = "heck" version = "0.5.0" @@ -311,8 +333,8 @@ dependencies = [ "anyhow", "clap", "colored", + "command-with-spinner", "indicatif", - "thiserror", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index e950ec3..f1e1be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,9 @@ [workspace] resolver = "3" -members = [ - "create-worktree", +members = [ + "command-with-spinner", + "create-worktree", "git", + "git-ssh-bitwarden", "setup-repo", ] diff --git a/command-with-spinner/Cargo.toml b/command-with-spinner/Cargo.toml new file mode 100644 index 0000000..132ae34 --- /dev/null +++ b/command-with-spinner/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "command-with-spinner" +version = "0.1.0" +edition.workspace = true +authors.workspace = true +license-file.workspace = true +repository.workspace = true + +[dependencies] +anyhow = "1.0.98" +colored = "3.0.0" +git = { version = "0.1.0", path = "../git" } +indicatif = "0.17.11" diff --git a/command-with-spinner/src/lib.rs b/command-with-spinner/src/lib.rs new file mode 100644 index 0000000..61948e4 --- /dev/null +++ b/command-with-spinner/src/lib.rs @@ -0,0 +1,41 @@ +use std::process::{Command, Stdio}; + +use anyhow::{Context, Result}; +use colored::*; +use indicatif::{ProgressBar, ProgressStyle}; + +use git::error::GitError; + +/// Runs a git command with a progress spinner +pub fn run_command(command: &mut Command, message: &str) -> Result<()> { + let spinner = ProgressBar::new_spinner(); + spinner.set_style( + ProgressStyle::default_spinner() + .tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷") + .template("{spinner:.green} {msg}") + .expect("Invalid template format"), + ); + spinner.set_message(message.to_string()); + + // Configure the command to not show output + command.stdout(Stdio::null()).stderr(Stdio::null()); + + // Execute the command and wait for it to complete + spinner.enable_steady_tick(std::time::Duration::from_millis(100)); + + let status = command.status().context("Failed to execute command")?; + spinner.finish_and_clear(); + + if status.success() { + println!("{message} {}", "Done.".green()); + Ok(()) + } else { + println!("{message} {}", "FAILED.".red()); + + let code = status.code(); + match code { + Some(code) => Err(GitError::Failed(code).into()), + None => Err(GitError::FailedNoCode.into()), + } + } +} diff --git a/create-worktree/Cargo.toml b/create-worktree/Cargo.toml index 11b51fb..40291d9 100644 --- a/create-worktree/Cargo.toml +++ b/create-worktree/Cargo.toml @@ -15,3 +15,4 @@ colored = "3.0" indicatif = "0.17" dirs = "6.0" thiserror = { version = "2.0" } +command-with-spinner = { version = "0.1.0", path = "../command-with-spinner" } diff --git a/create-worktree/src/main.rs b/create-worktree/src/main.rs index c299158..0e33816 100644 --- a/create-worktree/src/main.rs +++ b/create-worktree/src/main.rs @@ -1,11 +1,12 @@ use std::env; use std::path::Path; -use std::process::{Command, Stdio}; +use std::process::Command; use anyhow::{Context, Result}; use clap::{ArgAction, Parser}; use colored::*; -use indicatif::{ProgressBar, ProgressStyle}; + +use command_with_spinner::run_command; /// Tool to create git worktrees with convenient branch management #[derive(Parser, Debug)] @@ -39,53 +40,6 @@ struct Args { no_color: bool, } -/// Git operations error type -#[derive(Debug, thiserror::Error)] -enum GitError { - #[error("Command failed with exit code: {0}")] - Failed(i32), - - #[error("Command failed without exit code")] - FailedNoCode, - - #[error("Failed to execute command: {0}")] - ExecutionError(#[from] std::io::Error), -} - -/// Runs a git command with a progress spinner -fn run_command(command: &mut Command, message: &str) -> Result<()> { - let spinner = ProgressBar::new_spinner(); - spinner.set_style( - ProgressStyle::default_spinner() - .tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷") - .template("{spinner:.green} {msg}") - .expect("Invalid template format"), - ); - spinner.set_message(message.to_string()); - - // Configure the command to not show output - command.stdout(Stdio::null()).stderr(Stdio::null()); - - // Execute the command and wait for it to complete - spinner.enable_steady_tick(std::time::Duration::from_millis(100)); - - let status = command.status().context("Failed to execute command")?; - spinner.finish_and_clear(); - - if status.success() { - println!("{message} {}", "Done.".green()); - Ok(()) - } else { - println!("{message} {}", "FAILED.".red()); - - let code = status.code(); - match code { - Some(code) => Err(GitError::Failed(code).into()), - None => Err(GitError::FailedNoCode.into()), - } - } -} - /// Git command wrapper struct Git; diff --git a/git-ssh-bitwarden/Cargo.toml b/git-ssh-bitwarden/Cargo.toml new file mode 100644 index 0000000..3878f46 --- /dev/null +++ b/git-ssh-bitwarden/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "git-ssh-bitwarden" +version = "0.1.0" + +edition.workspace = true +authors.workspace = true +license-file.workspace = true +repository.workspace = true + +[dependencies] diff --git a/git-ssh-bitwarden/src/main.rs b/git-ssh-bitwarden/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/git-ssh-bitwarden/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/git/Cargo.toml b/git/Cargo.toml new file mode 100644 index 0000000..694768e --- /dev/null +++ b/git/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "git" +version = "0.1.0" + +edition.workspace = true +authors.workspace = true +license-file.workspace = true +repository.workspace = true + +[dependencies] +thiserror = "2.0" diff --git a/git/src/error.rs b/git/src/error.rs new file mode 100644 index 0000000..7685bc8 --- /dev/null +++ b/git/src/error.rs @@ -0,0 +1,12 @@ +/// Git operations error type +#[derive(Debug, thiserror::Error)] +pub enum GitError { + #[error("Command failed with exit code: {0}")] + Failed(i32), + + #[error("Command failed without exit code")] + FailedNoCode, + + #[error("Failed to execute command: {0}")] + ExecutionError(#[from] std::io::Error), +} diff --git a/git/src/lib.rs b/git/src/lib.rs new file mode 100644 index 0000000..a91e735 --- /dev/null +++ b/git/src/lib.rs @@ -0,0 +1 @@ +pub mod error; diff --git a/setup-repo/Cargo.toml b/setup-repo/Cargo.toml index 6b41a30..bcc1220 100644 --- a/setup-repo/Cargo.toml +++ b/setup-repo/Cargo.toml @@ -12,5 +12,5 @@ repository.workspace = true anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } colored = "3.0" +command-with-spinner = { version = "0.1.0", path = "../command-with-spinner" } indicatif = "0.17" -thiserror = "2.0" diff --git a/setup-repo/src/main.rs b/setup-repo/src/main.rs index 5df8572..47d8be9 100644 --- a/setup-repo/src/main.rs +++ b/setup-repo/src/main.rs @@ -1,10 +1,11 @@ use std::path::Path; -use std::process::{Command, Stdio}; +use std::process::Command; use anyhow::{Context, Result}; use clap::Parser; use colored::*; -use indicatif::{ProgressBar, ProgressStyle}; + +use command_with_spinner::run_command; /// Tool to set up Git repositories for worktree development #[derive(Parser, Debug)] @@ -27,53 +28,6 @@ struct Args { no_color: bool, } -/// Git operations error type -#[derive(Debug, thiserror::Error)] -enum GitError { - #[error("Command failed with exit code: {0}")] - Failed(i32), - - #[error("Command failed without exit code")] - FailedNoCode, - - #[error("Failed to execute command: {0}")] - ExecutionError(#[from] std::io::Error), -} - -/// Runs a command with a progress spinner -fn run_command(command: &mut Command, message: &str) -> Result<()> { - let spinner = ProgressBar::new_spinner(); - spinner.set_style( - ProgressStyle::default_spinner() - .tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷") - .template("{spinner:.green} {msg}") - .expect("Invalid template format"), - ); - spinner.set_message(message.to_string()); - - // Configure the command to not show output - command.stdout(Stdio::null()).stderr(Stdio::null()); - - // Execute the command and wait for it to complete - spinner.enable_steady_tick(std::time::Duration::from_millis(100)); - - let status = command.status().context("Failed to execute command")?; - spinner.finish_and_clear(); - - if status.success() { - println!("{message} {}", "Done.".green()); - Ok(()) - } else { - println!("{message} {}", "FAILED.".red()); - - let code = status.code(); - match code { - Some(code) => Err(GitError::Failed(code).into()), - None => Err(GitError::FailedNoCode.into()), - } - } -} - /// Git command wrapper struct Git;