commit 667808a6d22fbb1a323695e7361b3e89e72ddae4 Author: Grimmauld Date: Tue May 14 10:52:31 2024 +0200 implement confwhich diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f0fe67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/target +*~ +.*~undo-tree~* +result + +# Added by cargo +# +# already existing elements were commented out + +#/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/confwhich.iml b/.idea/confwhich.iml new file mode 100644 index 0000000..c254557 --- /dev/null +++ b/.idea/confwhich.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8a7f953 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..8843855 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,267 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap-stdin" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc126d12a0930c94c3548581294d5f19360ac02e408600b4d7619d7234e8b505" +dependencies = [ + "thiserror", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "confwhich" +version = "0.1.0" +dependencies = [ + "clap", + "clap-stdin", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7ab8100 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "confwhich" +version = "0.1.0" +edition = "2021" +authors = [ "Grimmauld " ] +description = "Search for config files in XDG_CONFIG_DIRS" +license-file = "LICENSE" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = {version="4.5.4", features = ["derive"]} +clap-stdin = {version="0.4.0"} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0ede32b --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2024 Grimmauld + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8eff6c6 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# What's this? +Confwhich is an attempt to make working with `XDG_CONFIG_DIRS` and `XDG_CONFIG_HOME` easier by providing some nice utility functions. + +# Useage +``` +Usage: confwhich [OPTIONS] + +Arguments: + The config prefix to search for + +Options: + --skip-home Skip directories in XDG_CONFIG_DIRS that start with the home directory + -a, --all Print all matches in XDG_CONFIG_DIRS, not just the first + --from-process Read the environment from a process by pid instead of regular environment [default: -1] + --show-tilde Output a tilde for HOME directory + -h, --help Print help + -V, --version Print version +``` + +Confwhich allows reading `XDG_CONFIG_DIRS` and `XDG_CONFIG_HOME` from a running process by provided process id, assuming the `confwhich`process is elevated enough to read the environ file at `/proc//environ`. Potential useage might be e.g. `confwhich --show-tilde --from-process $(pgrep alacritty | head -n 1) alacritty` which will return `~/.config/alacritty.toml` on my system. + +# Installing +Any system that can build rust applications can have confwhich. + +```bash +git clone https://git.grimmauld.de/Grimmauld/confwhich.git +cd confwhich +cargo build --release +``` + +Alternatively, this project supports nix tooling. The included flake provides a module installing `confwhich` to system packages, but also a default package for `nix run git+https://git.grimmauld.de/Grimmauld/confwhich`. diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ca470b4 --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1715499532, + "narHash": "sha256-9UJLb8rdi2VokYcfOBQHUzP3iNxOPNWcbK++ENElpk0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "af8b9db5c00f1a8e4b83578acc578ff7d823b786", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixpkgs-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d1a7039 --- /dev/null +++ b/flake.nix @@ -0,0 +1,98 @@ +{ + description = "confwhich: easily obtain info about XDG_CONFIG useage"; + + inputs.nixpkgs.url = "nixpkgs/nixpkgs-unstable"; + + outputs = + { nixpkgs, ... }: + let + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system); + in + { + devShells = forAllSystems ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + overrides = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)); + libPath = + with pkgs; + lib.makeLibraryPath [ + # load external libraries that you need in your rust project here + glib + gtk3 + libsoup_3 + networkmanager + webkitgtk_4_1 + ]; + in + { + default = pkgs.mkShell { + buildInputs = with pkgs; [ + llvmPackages.bintools + rustup + ]; + RUSTC_VERSION = overrides.toolchain.channel; + # https://github.com/rust-lang/rust-bindgen#environment-variables + LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ]; + shellHook = '' + export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin + export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/ + ''; + + # Add precompiled library to rustc search path + RUSTFLAGS = ( + builtins.map (a: ''-L ${a}/lib'') [ + # add libraries here (e.g. pkgs.libvmi) + ] + ); + LD_LIBRARY_PATH = libPath; + # Add glibc, clang, glib, and other headers to bindgen search path + BINDGEN_EXTRA_CLANG_ARGS = + # Includes normal include path + (builtins.map (a: ''-I"${a}/include"'') [ + # add dev libraries here (e.g. pkgs.libvmi.dev) + pkgs.glibc.dev + ]) + # Includes with special directory paths + ++ [ + ''-I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"'' + ''-I"${pkgs.glib.dev}/include/glib-2.0"'' + ''-I${pkgs.glib.out}/lib/glib-2.0/include/'' + ]; + packages = with pkgs; [ + # base toolchain + pkg-config + rustup + nil + ]; + }; + } + ); + + packages = forAllSystems ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + confwhich-pkg = (pkgs.callPackage ./nix/package.nix { }); + in + { + confwhich = confwhich-pkg; + default = confwhich-pkg; + } + ); + + nixosModules = + let + confwhich-module = (import ./nix/rmenu.nix); + in + { + rmenu = confwhich-module; + default = confwhich-module; + }; + }; +} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..27c4955 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,17 @@ +{ lib, rustPlatform }: +rustPlatform.buildRustPackage { + pname = "confwhich"; + version = "unstable-2024-05-14"; + src = lib.cleanSource ../.; + + cargoHash = "sha256-cn9vtRO+negpIVs0rnp2y5q7L4w554dfBK9MtbWd8FA="; + + meta = { + description = "tool to find the path of xdg config files"; + homepage = "https://git.grimmauld.de/Grimmauld/confwhich"; + license = lib.licenses.bsd3; + mainProgram = "confwhich"; + maintainers = with lib.maintainers; [ grimmauld ]; + platforms = lib.platforms.linux; + }; +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..292fe49 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "stable" diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..dea4cbc --- /dev/null +++ b/src/args.rs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: BSD-3-Clause + +use clap::Parser; +use clap_stdin::MaybeStdin; + +#[derive(Debug, Parser)] +#[clap(name="confwhich", version=env!("CARGO_PKG_VERSION"),about=env!("CARGO_PKG_DESCRIPTION"), author=env!("CARGO_PKG_AUTHORS"))] +pub struct Cli { + /// The config prefix to search for + pub search: MaybeStdin, + + /// Skip directories in XDG_CONFIG_DIRS that start with the home directory + #[arg(long, default_value_t = false)] + pub skip_home: bool, + + /// Print all matches in XDG_CONFIG_DIRS, not just the first + #[arg(short, long, default_value_t = false)] + pub all: bool, + + /// Read the environment from a process by pid instead of regular environment + #[arg(long, default_value_t=-1)] + pub from_process: i32, // max pid is 2^22 so 2^31 is sufficient, we can default to -1 as magic + // value + /// Output a tilde for HOME directory + #[arg(long, default_value_t = false)] + pub show_tilde: bool, +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..72784cd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: BSD-3-Clause + +mod args; +use crate::args::Cli; +use clap::Parser; +use std::fs; +use std::process::exit; +use std::string::String; + +static XDG_CONFIG_HOME: &str = "XDG_CONFIG_HOME"; +static XDG_CONFIG_DIRS: &str = "XDG_CONFIG_DIRS"; +static HOME: &str = "HOME"; + +fn main() { + let cli = Cli::parse(); + + let pid = cli.from_process; + + let mut config_dirs = Vec::new(); + let mut user_home: Option = None; + + if pid > 0 { + let path = format!("/proc/{pid}/environ"); + let environ = match fs::read_to_string(path.clone()) { + Ok(contents) => contents, + Err(err) => { + eprintln!("Could not read file {path}: {err}"); + exit(1); + } + }; + + let process_env_vars = environ.split("\0"); + let config_home_prefix = format!("{XDG_CONFIG_HOME}="); + let config_dirs_prefix = format!("{XDG_CONFIG_DIRS}="); + let home_prefix = format!("{HOME}="); + + for var in process_env_vars { + let tr = var.trim(); + + if tr.starts_with(&config_home_prefix) && !cli.skip_home { + config_dirs.insert(0, tr.strip_prefix(&config_home_prefix).unwrap().to_string()); + } else if tr.starts_with(&config_dirs_prefix) { + config_dirs.extend( + tr.strip_prefix(&config_dirs_prefix) + .unwrap() + .split(":") + .map(String::from), + ); + } else if tr.starts_with(&home_prefix) { + user_home = Some(tr.strip_prefix(&home_prefix).unwrap().trim().to_string()); + } + } + } else { + if let Ok(config_home) = std::env::var(XDG_CONFIG_HOME) { + if !cli.skip_home { + config_dirs.insert(0, config_home.to_string()); + } + } + if let Ok(home) = std::env::var(HOME) { + user_home = Some(home); + } + config_dirs.extend( + std::env::var(XDG_CONFIG_DIRS) + .unwrap_or(String::new()) + .split(":") + .map(String::from), + ); + } + + let mut found = false; + for path in config_dirs + .iter() + .map(|s| s.trim()) + .filter(|s| !s.is_empty()) + { + if cli.skip_home + && user_home.clone().is_some() + && path.starts_with(&user_home.clone().unwrap()) + { + continue; + } + + let files = match fs::read_dir(path) { + Ok(file) => file, + Err(_) => continue, + }; + + for file in files { + let file_name = format!( + "{}", + file.as_ref().unwrap().file_name().into_string().unwrap() + ); + if file_name.contains(&cli.search.to_string()) { + let path = file.as_ref().unwrap().path(); + let out = if cli.show_tilde && user_home.clone().is_some() { + format!( + "{}", + path.display() + .to_string() + .replace(&user_home.clone().unwrap(), "~") + ) + } else { + path.display().to_string() + }; + + println!("{}", out); + found = true; + if !cli.all { + return; + } + } + } + } + + if !found { + eprintln!("No {} in ({})", cli.search, config_dirs.join(":")); + exit(1); + } +} diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..b7d39c2 --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,9 @@ +[formatter.nix] +command = "nixfmt" +includes = ["*.nix"] +excludes = ["tests/**"] + +[formatter.rs] +command = "rustfmt" +includes = ["*.rs"] +excludes = ["tests/**"]