From 55a5a9a1e105bf74b367980a4539a6ed766eea69 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 22 Dec 2024 13:33:41 +0100 Subject: [PATCH] feat: make init zsh Signed-off-by: kjuulh --- Cargo.lock | 182 ++++++++++++++++++++++++++++++++++--- crates/kignore/Cargo.toml | 1 + crates/kignore/src/main.rs | 100 +++++++++++++++----- 3 files changed, 248 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5608761..e7ca88e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,7 +47,7 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" dependencies = [ - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -57,9 +57,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.59.0", ] +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + [[package]] name = "cfg-if" version = "1.0.0" @@ -111,7 +117,7 @@ dependencies = [ "libc", "once_cell", "unicode-width", - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -123,6 +129,27 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "encode_unicode" version = "1.0.0" @@ -139,6 +166,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "indenter" version = "0.3.3" @@ -159,10 +197,11 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "kignore" -version = "0.2.3" +version = "0.2.4" dependencies = [ "clap", "console", + "dirs", "eyre", "tracing", "tracing-subscriber", @@ -180,6 +219,16 @@ version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", +] + [[package]] name = "log" version = "0.4.22" @@ -232,6 +281,12 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "overload" version = "0.1.1" @@ -268,6 +323,17 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.11.1" @@ -364,6 +430,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -500,6 +586,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" @@ -522,13 +614,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -537,28 +653,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -571,24 +705,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/crates/kignore/Cargo.toml b/crates/kignore/Cargo.toml index 85a1f67..bc63eb3 100644 --- a/crates/kignore/Cargo.toml +++ b/crates/kignore/Cargo.toml @@ -18,6 +18,7 @@ edition = "2021" [dependencies] clap = { version = "4.5.4", features = ["env", "unicode", "string"] } console = "0.15.8" +dirs = "5.0.1" eyre = "0.6.12" tracing = { version = "0.1.40", features = ["log"] } tracing-subscriber = { version = "0.3.18", features = [ diff --git a/crates/kignore/src/main.rs b/crates/kignore/src/main.rs index 3842227..902d7bc 100644 --- a/crates/kignore/src/main.rs +++ b/crates/kignore/src/main.rs @@ -1,11 +1,18 @@ use clap::{Arg, Command}; use console::style; -use eyre::{Context, ContextCompat}; +use eyre::{Context, ContextCompat, OptionExt}; use std::io::prelude::*; +use std::os::unix::fs::PermissionsExt; use std::{env::current_dir, io::Read, path::PathBuf}; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; +const SH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env sh +set -e + +kignore $@ +"; + pub fn main() -> eyre::Result<()> { let matches = Command::new("gitignore") .version("0.1") @@ -19,35 +26,82 @@ Easily add patterns using `git ignore ` this will by default also help Arg::new("pattern") .help("the pattern you want to ignore") .long_help("the pattern you want to ignore in the nearest .gitignore file") - .required(true), ).arg( - Arg::new("log-level").long("log-level").help("choose a log level and get more messages").long_help("Choose a log level and get more message, defaults to [fatal]")) + Arg::new("log-level").long("log-level").default_value("warn").help("choose a log level and get more messages").long_help("Choose a log level and get more message, defaults to [warn]") + ) + .subcommand(clap::Command::new("init").subcommand_required(true).subcommand(Command::new("zsh"))) .get_matches(); - let log_level = match matches.get_one::("log-level").map(|f| f.as_str()) { - Some("off") => "off", - Some("info") => "info", - Some("debug") => "debug", - Some("warn") => "warn", - Some("error") => "error", - _ => "error", - }; + match matches.subcommand() { + Some(("init", args)) => match args + .subcommand() + .expect("should never be able to call on init") + { + ("zsh", _) => { + let bin_dir = dirs::executable_dir().ok_or_eyre("failed to find executable dir")?; - tracing_subscriber::registry() - .with(tracing_subscriber::EnvFilter::new(format!( - "gitignore={}", - log_level - ))) - .with(tracing_subscriber::fmt::layer()) - .init(); + let alias_script = bin_dir.join("git-ignore"); + if let Ok(existing_file) = std::fs::read(&alias_script) { + if existing_file == SH_FILE_CONTENTS { + return Ok(()); + } + } else { + std::fs::create_dir_all(&bin_dir).context("failed to create bin dir")?; + } - let term = console::Term::stdout(); + let mut file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&alias_script)?; - let pattern = matches - .get_one::("pattern") - .context("missing [pattern]")?; + file.write_all(SH_FILE_CONTENTS)?; + file.flush()?; - add_gitignore_pattern(term, pattern) + // Set the file to be executable + let metadata = file.metadata()?; + let mut permissions = metadata.permissions(); + permissions.set_mode(0o755); // rwxr-xr-x + file.set_permissions(permissions)?; + + println!( + "successfully wrote alias to {}", + style(alias_script.display()).green() + ); + + Ok(()) + } + (subcommand, _) => { + panic!("cannot call on subcommand: {}", subcommand); + } + }, + _ => { + let log_level = match matches.get_one::("log-level").map(|f| f.as_str()) { + Some("off") => "off", + Some("info") => "info", + Some("debug") => "debug", + Some("warn") => "warn", + Some("error") => "error", + _ => "error", + }; + + tracing_subscriber::registry() + .with(tracing_subscriber::EnvFilter::new(format!( + "gitignore={}", + log_level + ))) + .with(tracing_subscriber::fmt::layer()) + .init(); + + let term = console::Term::stdout(); + + let pattern = matches + .get_one::("pattern") + .context("missing [pattern]")?; + + add_gitignore_pattern(term, pattern) + } + } } enum GitActions {