Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make env vars a shareable crate #171

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

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

22 changes: 17 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
[workspace]
members = ["lib/*"]

[workspace.package]
edition = "2021"

[workspace.dependencies]
constants = { path = "lib/constants" }

[package]
name = "prefligit"
version = "0.0.8"
authors = ["j178 <hi@j178.dev>"]
description = "pre-commit implemented in Rust"
repository = "https://github.com/j178/prefligit"
homepage = "https://github.com/j178/prefligit"
edition = "2021"
edition.workspace = true
license-file = "LICENSE"
# `cargo publish` failed because of this, disabled for now.
# include = ["licenses/.*"]

[features]
default = ["docker"]
Expand All @@ -17,6 +24,8 @@ profiler-flamegraph = ["pprof/flamegraph"]
docker = []

[dependencies]
constants = { workspace = true }

anstream = "0.6.15"
anyhow = "1.0.86"
assert_cmd = { version = "2.0.16", features = ["color"] }
Expand Down Expand Up @@ -72,10 +81,13 @@ regex = "1.11.0"
[build-dependencies]
fs-err = "3.1.0"

[lints.rust]
[lints]
workspace = true

[workspace.lints.rust]
dead_code = "allow"

[lints.clippy]
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -2 }
# Allowed pedantic lints
char_lit_as_u8 = "allow"
Expand Down
7 changes: 7 additions & 0 deletions lib/constants/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "constants"
version = "0.0.1"
edition.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions src/env_vars.rs → lib/constants/src/env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ impl EnvVars {

pub const PREFLIGIT_HOME: &'static str = "PREFLIGIT_HOME";

pub const PREFLIGIT_INTERNAL__TEST_DIR: &'static str = "PREFLIGIT_INTERNAL__TEST_DIR";
pub const PREFLIGIT_INTERNAL__SORT_FILENAMES: &'static str =
"PREFLIGIT_INTERNAL__SORT_FILENAMES";

Expand Down
1 change: 1 addition & 0 deletions lib/constants/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod env_vars;
6 changes: 4 additions & 2 deletions src/cli/hook_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use anyhow::Result;
use std::ffi::OsString;
use std::path::PathBuf;

use anstream::eprintln;

use constants::env_vars::EnvVars;

use crate::cli::{self, ExitStatus, RunArgs};
use crate::config::HookType;
use crate::env_vars::EnvVars;
use crate::printer::Printer;
use anstream::eprintln;

pub(crate) async fn hook_impl(
config: Option<PathBuf>,
Expand Down
3 changes: 2 additions & 1 deletion src/cli/run/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use fancy_regex::Regex;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use tracing::{debug, error};

use constants::env_vars::EnvVars;

use crate::config::Stage;
use crate::env_vars::EnvVars;
use crate::fs::normalize_path;
use crate::git;
use crate::hook::Hook;
Expand Down
3 changes: 2 additions & 1 deletion src/cli/run/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use rand::SeedableRng;
use tracing::{debug, trace};
use unicode_width::UnicodeWidthStr;

use constants::env_vars::EnvVars;

use crate::cli::reporter::{HookInitReporter, HookInstallReporter};
use crate::cli::run::keeper::WorkTreeKeeper;
use crate::cli::run::{get_filenames, FileFilter, FileOptions};
use crate::cli::{ExitStatus, RunExtraArgs};
use crate::config::Stage;
use crate::env_vars::EnvVars;
use crate::fs::Simplified;
use crate::git;
use crate::git::{get_diff, git_cmd};
Expand Down
3 changes: 2 additions & 1 deletion src/languages/python/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use constants::env_vars::EnvVars;

use crate::config::LanguageVersion;
use crate::env_vars::EnvVars;
use crate::hook::Hook;
use crate::languages::python::uv::UvInstaller;
use crate::languages::LanguageImpl;
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ mod builtin;
mod cleanup;
mod cli;
mod config;
mod env_vars;
mod fs;
mod git;
mod hook;
Expand Down
3 changes: 2 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::sync::{Arc, LazyLock};
use futures::StreamExt;
use tracing::trace;

use crate::env_vars::EnvVars;
use constants::env_vars::EnvVars;

use crate::hook::Hook;

pub static CONCURRENCY: LazyLock<usize> = LazyLock::new(|| {
Expand Down
3 changes: 2 additions & 1 deletion src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use rusqlite::Connection;
use thiserror::Error;
use tracing::debug;

use constants::env_vars::EnvVars;

use crate::config::RemoteRepo;
use crate::env_vars::EnvVars;
use crate::fs::{copy_dir_all, LockedFile};
use crate::git::clone_repo;
use crate::hook::{Hook, Repo};
Expand Down
8 changes: 5 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use assert_cmd::assert::OutputAssertExt;
use assert_fs::fixture::{ChildPath, FileWriteStr, PathChild};
use etcetera::BaseStrategy;

use constants::env_vars::EnvVars;

pub struct TestContext {
temp_dir: ChildPath,
home_dir: ChildPath,
Expand Down Expand Up @@ -63,7 +65,7 @@ impl TestContext {
}

pub fn test_bucket_dir() -> PathBuf {
env::var("PREFLIGIT_INTERNAL__TEST_DIR")
env::var(EnvVars::PREFLIGIT_INTERNAL__TEST_DIR)
.map(PathBuf::from)
.unwrap_or_else(|_| {
etcetera::base_strategy::choose_base_strategy()
Expand Down Expand Up @@ -115,8 +117,8 @@ impl TestContext {
let bin = assert_cmd::cargo::cargo_bin("prefligit");
let mut cmd = Command::new(bin);
cmd.current_dir(self.workdir());
cmd.env("PREFLIGIT_HOME", &*self.home_dir);
cmd.env("PREFLIGIT_INTERNAL__SORT_FILENAMES", "1");
cmd.env(EnvVars::PREFLIGIT_HOME, &*self.home_dir);
cmd.env(EnvVars::PREFLIGIT_INTERNAL__SORT_FILENAMES, "1");
cmd
}

Expand Down
Loading