Create libs for common code

This commit is contained in:
Noah Knegt
2025-05-08 14:09:29 +02:00
parent 5d372f87cb
commit a2f2e303a0
13 changed files with 126 additions and 102 deletions

View File

@@ -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"

View File

@@ -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;