Skip to content

Commit

Permalink
feat: upgrade is now a cargo feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 6, 2024
1 parent eca33d4 commit 95607a1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 29 deletions.
30 changes: 22 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "grip-grab"
version = "0.5.4"
version = "0.5.5"
edition = "2021"
authors = ["Alexandre Pasmantier <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -18,19 +18,28 @@ categories = [


[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.9", features = ["derive"] }
devicons = "0.6.7"
grep = "0.3.1"
ignore = "0.4.22"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
termcolor = "1.4.1"
thiserror = "1.0.64"

[[bin]]
name = "gg"
path = "src/main.rs"


[features]
default = []
upgrade = []

[profile.dev]
opt-level = 0
debug = true

[profile.release]
opt-level = 3
debug = "none"
Expand Down
7 changes: 6 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use crate::{printer::PrintMode, utils};
use clap::{ArgAction, Parser, Subcommand};
use thiserror::Error;

#[derive(Parser, Debug)]
#[command(name = "grip-grab")]
Expand Down Expand Up @@ -148,7 +149,11 @@ impl Default for PostProcessedCli {
}
}

pub fn process_cli_args(mut cli: Cli) -> anyhow::Result<PostProcessedCli> {
#[derive(Error, Debug)]
#[error("Error processing CLI arguments")]
pub struct CliProcessingError {}

pub fn process_cli_args(mut cli: Cli) -> Result<PostProcessedCli, CliProcessingError> {
cli.validate();

if cli.paths.is_empty() {
Expand Down
5 changes: 2 additions & 3 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use ignore::{types::TypesBuilder, WalkBuilder};
use ignore::{types::TypesBuilder, Error, WalkBuilder};

use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -43,7 +42,7 @@ pub fn walk_builder(
builder
}

fn add_custom_filetypes(types_builder: &mut TypesBuilder) -> Result<()> {
fn add_custom_filetypes(types_builder: &mut TypesBuilder) -> Result<(), Error> {
Ok(types_builder.add("pystrict", "*.py")?)
}

Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::io::{stdin, Read};
use std::io::{self, stdin, Read};
use std::path::PathBuf;
use std::sync::{mpsc, Arc};

use clap::Parser;

use cli::Commands;
use cli::{CliProcessingError, Commands};
use fs::is_readable_stdin;
use grep::regex::RegexMatcher;
use grep::regex::{self, RegexMatcher};
use ignore::DirEntry;
use printer::PrinterConfig;
use search::{build_searcher, search_reader};
use thiserror::Error;
use upgrade::upgrade_gg;

use crate::cli::{process_cli_args, Cli};
Expand All @@ -24,7 +25,17 @@ mod search;
mod upgrade;
mod utils;

pub fn main() -> anyhow::Result<()> {
#[derive(Error, Debug)]
pub enum GGError {
#[error("Erorr processing CLI arguments")]
Cli(#[from] CliProcessingError),
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Regex(#[from] regex::Error),
}

pub fn main() -> Result<(), GGError> {
let cli_args = process_cli_args(Cli::parse())?;

if let Some(subcommand) = cli_args.sub_command {
Expand Down
13 changes: 8 additions & 5 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fmt;
use std::{fmt, io};
use std::{path::PathBuf, slice::Iter};

use grep::{
matcher::{Match, Matcher},
regex::{RegexMatcher, RegexMatcherBuilder},
regex::{self, RegexMatcher, RegexMatcherBuilder},
searcher::{sinks::UTF8, Searcher, SearcherBuilder},
};
use serde::Serialize;
Expand Down Expand Up @@ -185,7 +185,7 @@ pub fn search_file<'a>(
path: PathBuf,
matcher: &RegexMatcher,
searcher: &mut Searcher,
) -> anyhow::Result<FileResults> {
) -> Result<FileResults, io::Error> {
let mut partial_results: Vec<PartialSearchResult> = Vec::new();

searcher.search_path(
Expand Down Expand Up @@ -236,11 +236,13 @@ pub fn search_file<'a>(
Ok(FileResults { path, results })
}

// io::Error
// std::fmt::Display
pub fn search_reader(
reader: impl std::io::BufRead,
matcher: &RegexMatcher,
searcher: &mut Searcher,
) -> anyhow::Result<Vec<SearchResult>> {
) -> Result<Vec<SearchResult>, io::Error> {
let mut results = Vec::new();
let mut line_number = 0;
searcher.search_reader(
Expand All @@ -266,8 +268,9 @@ pub fn search_reader(
Ok(results)
}

pub fn build_matcher(patterns: &Vec<String>) -> anyhow::Result<RegexMatcher> {
pub fn build_matcher(patterns: &Vec<String>) -> Result<RegexMatcher, regex::Error> {
let builder = RegexMatcherBuilder::new();
// matcher Error
Ok(builder.build_many(patterns)?)
}

Expand Down
31 changes: 25 additions & 6 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
use std::{
io::{BufRead, BufReader},
process::Command,
thread::{self},
};

use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

#[cfg(not(feature = "upgrade"))]
pub fn upgrade_gg(_force: bool) {
let mut colored_stdout = StandardStream::stdout(ColorChoice::Always);
colored_stdout
.set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_italic(true))
.expect("Failed to set color");

println!("\n┌────────────────────────────────────────────┐");
println!("│ Upgrade feature is not enabled. │");
println!("│ Please recompile with `upgrade` feature │");
println!("│ enabled to use this feature: │");
println!("│ │");
println!("│ cargo install grip-grab --features=upgrade │");
println!("└────────────────────────────────────────────┘\n");

colored_stdout.reset().expect("Failed to reset color");
}

#[cfg(feature = "upgrade")]
pub fn upgrade_gg(force: bool) {
use std::{
io::{BufRead, BufReader},
process::Command,
thread::{self},
};

let mut colored_stdout = StandardStream::stdout(ColorChoice::Always);
colored_stdout
.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_italic(true))
Expand Down

0 comments on commit 95607a1

Please sign in to comment.