feat: make cli look nice
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
b78631d6ac
commit
b167b3ebfa
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -159,7 +159,7 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kignore"
|
name = "kignore"
|
||||||
version = "0.2.0"
|
version = "0.2.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"console",
|
"console",
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use clap::{Arg, Command};
|
use clap::{Arg, Command};
|
||||||
|
use console::style;
|
||||||
use eyre::{Context, ContextCompat};
|
use eyre::{Context, ContextCompat};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::{env::current_dir, io::Read, path::PathBuf};
|
use std::{env::current_dir, io::Read, path::PathBuf};
|
||||||
@ -55,7 +56,7 @@ enum GitActions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<()> {
|
fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<()> {
|
||||||
term.write_line("git ignore: Add pattern")?;
|
println!("git ignore: Add pattern");
|
||||||
let curdir = current_dir().context(
|
let curdir = current_dir().context(
|
||||||
"could not find current_dir, you may not have permission to access that directory",
|
"could not find current_dir, you may not have permission to access that directory",
|
||||||
)?;
|
)?;
|
||||||
@ -75,7 +76,7 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
|
|||||||
|
|
||||||
match actions {
|
match actions {
|
||||||
GitActions::AddPattern { gitignore_path } => {
|
GitActions::AddPattern { gitignore_path } => {
|
||||||
term.write_line("Found existing .gitignore")?;
|
println!("Found existing {}", style(".gitignore").green());
|
||||||
let mut gitignore_file = open_gitignore_file(&gitignore_path)?;
|
let mut gitignore_file = open_gitignore_file(&gitignore_path)?;
|
||||||
let mut gitignore_content = String::new();
|
let mut gitignore_content = String::new();
|
||||||
gitignore_file
|
gitignore_file
|
||||||
@ -85,22 +86,26 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
|
|||||||
gitignore_path.to_string_lossy()
|
gitignore_path.to_string_lossy()
|
||||||
))?;
|
))?;
|
||||||
if gitignore_content.contains(pattern) {
|
if gitignore_content.contains(pattern) {
|
||||||
term.write_line(".gitignore already contains pattern, skipping")?;
|
println!(
|
||||||
|
".gitignore already contains pattern, {}",
|
||||||
|
style("skipping...").blue()
|
||||||
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
term.write_line("adding pattern to file")?;
|
println!("adding pattern to file");
|
||||||
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
|
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
|
||||||
gitignore_file
|
gitignore_file
|
||||||
.sync_all()
|
.sync_all()
|
||||||
.context("failed to write data to disk")?;
|
.context("failed to write data to disk")?;
|
||||||
}
|
}
|
||||||
GitActions::CreateIgnoreAndAddPattern { git_path } => {
|
GitActions::CreateIgnoreAndAddPattern { git_path } => {
|
||||||
term.write_line(
|
println!(
|
||||||
"could not find .gitignore file, creating one in the root of the git repository",
|
"could not find {} file, creating one in the root of the git repository",
|
||||||
)?;
|
style(".gitignore").yellow()
|
||||||
|
);
|
||||||
let mut gitignore_file = create_gitignore_file(git_path)?;
|
let mut gitignore_file = create_gitignore_file(git_path)?;
|
||||||
term.write_line("adding pattern to file")?;
|
println!("adding pattern to file");
|
||||||
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
|
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
|
||||||
gitignore_file
|
gitignore_file
|
||||||
.sync_all()
|
.sync_all()
|
||||||
@ -121,8 +126,19 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
|
|||||||
String::from_utf8(output.stdout)?
|
String::from_utf8(output.stdout)?
|
||||||
.lines()
|
.lines()
|
||||||
.chain(String::from_utf8(output.stderr)?.lines())
|
.chain(String::from_utf8(output.stderr)?.lines())
|
||||||
.try_for_each(|l| term.write_line(l))
|
.map(|l| {
|
||||||
.context("could not print all output to terminal")?;
|
// make rm 'path' look nice
|
||||||
|
if l.contains("rm") {
|
||||||
|
if let Some((_, pruned_first)) = l.split_once("'") {
|
||||||
|
if let Some((content, _)) = pruned_first.rsplit_once("'") {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
l
|
||||||
|
})
|
||||||
|
.for_each(|l| println!("removed from git history: {}", style(l).yellow()));
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
return Err(eyre::anyhow!("failed to run git index command"));
|
return Err(eyre::anyhow!("failed to run git index command"));
|
||||||
|
Loading…
Reference in New Issue
Block a user