chore: make variant for the other shell commands
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-12-22 14:52:05 +01:00
parent 55a5a9a1e1
commit 39dc4ae15c
3 changed files with 95 additions and 55 deletions

2
Cargo.lock generated
View File

@ -197,7 +197,7 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]] [[package]]
name = "kignore" name = "kignore"
version = "0.2.4" version = "0.3.0"
dependencies = [ dependencies = [
"clap", "clap",
"console", "console",

View File

@ -27,24 +27,22 @@ Cargo will only pull the `kignore` command and won't add a subcommand to `git.
```bash ```bash
$ cargo install kignore $ cargo install kignore
$ cargo binstall kignore # binstall will warn that git.front.kjuulh.io isn't a valid repo, it is still installable though
``` ```
#### Post install #### Post install
To get the `git ignore` subcommand working you will need to have the file To get the `git ignore` subcommand working you will need to have the file
git-ignore available on your path, either add it yourself using git-ignore available on your path
`git-alias/git-ignore` as a template or:
``` ```
git clone https://github.com/kjuulh/gitignore # zsh
./scripts/install-git-alias.sh # only tested on mac and linux eval "kignore init zsh"
```
# shell
### Homebrew eval "kignore init sh"
Added in HomebrewFormula # bash
eval "kignore init bash"
```bash
$ brew tap kjuulh/gitignore https://github.com/kjuulh/gitignore
$ brew install kjuulh/gitignore/kignore-bin
``` ```

View File

@ -7,29 +7,55 @@ use std::{env::current_dir, io::Read, path::PathBuf};
use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::util::SubscriberInitExt;
const ZSH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env zsh
set -e
kignore $@
";
const SH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env sh const SH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env sh
set -e set -e
kignore $@ kignore $@
"; ";
const BASH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env bash
set -e
kignore $@
";
pub fn main() -> eyre::Result<()> { pub fn main() -> eyre::Result<()> {
let matches = Command::new("gitignore") let matches = Command::new("gitignore")
.version("0.1") .version("0.1")
.author("Kasper J. Hermansen <contact@kjuulh.io>") .author("Kasper J. Hermansen <contact@kjuulh.io>")
.about("Easily ignore items and remove from git state") .about("Easily ignore items and remove from git state")
.long_about("git ignore is a utility tool for easily adding patterns to your .gitignore file. .long_about(
Easily add patterns using `git ignore <pattern>` this will by default also help you remove committed code violating these patterns "git ignore is a utility tool for easily adding patterns to your .gitignore file.
") Easily add patterns using `git ignore <pattern>` this will by default
also help you remove committed code violating these patterns
",
)
.propagate_version(true) .propagate_version(true)
.arg( .arg(
Arg::new("pattern") Arg::new("pattern")
.help("the pattern you want to ignore") .help("the pattern you want to ignore")
.long_help("the pattern you want to ignore in the nearest .gitignore file") .long_help("the pattern you want to ignore in the nearest .gitignore file"),
).arg( )
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]") .arg(
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"))
.subcommand(Command::new("sh"))
.subcommand(Command::new("bash")),
) )
.subcommand(clap::Command::new("init").subcommand_required(true).subcommand(Command::new("zsh")))
.get_matches(); .get_matches();
match matches.subcommand() { match matches.subcommand() {
@ -37,40 +63,9 @@ Easily add patterns using `git ignore <pattern>` this will by default also help
.subcommand() .subcommand()
.expect("should never be able to call on init") .expect("should never be able to call on init")
{ {
("zsh", _) => { ("zsh", _) => init_script(ShellType::Zsh),
let bin_dir = dirs::executable_dir().ok_or_eyre("failed to find executable dir")?; ("bash", _) => init_script(ShellType::Bash),
("sh", _) => init_script(ShellType::Shell),
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 mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&alias_script)?;
file.write_all(SH_FILE_CONTENTS)?;
file.flush()?;
// 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, _) => { (subcommand, _) => {
panic!("cannot call on subcommand: {}", subcommand); panic!("cannot call on subcommand: {}", subcommand);
} }
@ -305,3 +300,50 @@ fn search_for_dotgitignore(path: &PathBuf) -> eyre::Result<GitSearchResult> {
search_for_dotgitignore(&upwards_par) search_for_dotgitignore(&upwards_par)
} }
enum ShellType {
Bash,
Shell,
Zsh,
}
fn init_script(shell: ShellType) -> eyre::Result<()> {
let bin_dir = dirs::executable_dir().ok_or_eyre("failed to find executable dir")?;
let script = match shell {
ShellType::Bash => BASH_FILE_CONTENTS,
ShellType::Shell => SH_FILE_CONTENTS,
ShellType::Zsh => ZSH_FILE_CONTENTS,
};
let alias_script = bin_dir.join("git-ignore");
if let Ok(existing_file) = std::fs::read(&alias_script) {
if existing_file == script {
return Ok(());
}
} else {
std::fs::create_dir_all(&bin_dir).context("failed to create bin dir")?;
}
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&alias_script)?;
file.write_all(script)?;
file.flush()?;
// 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(())
}