From 697968e4104ec3ea4e1e3b09b9d3d8850212cdb0 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Tue, 28 Oct 2025 16:31:26 -0400 Subject: [PATCH] feat: Implemented `scan`. --- README.md | 2 +- flake.nix | 1 + mod.nu | 47 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index aceacc0..744a19b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ ## TODOS - [ ] `envr sync` - Restore missing .env files, and update backed up ones. -- [ ] `envr scan` - Search for missing / moved .env files. +- [x] `envr scan` - Search for missing / moved .env files. diff --git a/flake.nix b/flake.nix index 85c1174..19f0ef6 100644 --- a/flake.nix +++ b/flake.nix @@ -70,6 +70,7 @@ { buildInputs = with pkgs; [ age + fd nushell sqlite diff --git a/mod.nu b/mod.nu index 17b2104..9197378 100755 --- a/mod.nu +++ b/mod.nu @@ -178,11 +178,11 @@ export def "envr init" [ priv_key: $identity pub_key: $'($identity).pub' # TODO: scan settings - # scan: { - # matcher: '\.env' - # exclude: '*.envrc' - # include: '~' - # } + scan: { + matcher: '\.env' + exclude: '*.envrc' + include: '~' + } } | tee { save $source; open db @@ -217,9 +217,42 @@ export def "envr sync" [] { 'TODO: Need to implement' } -# Search for .env files +# Search for .env files and select ones to back up. export def "envr scan" [] { - 'TODO:' + let config = (envr config show).scan; + + let ignored_env_files = ( + find env-files $config.matcher $config.exclude $config.include + ); + + let tracked_file_paths = (files).path; + + let scanned = $ignored_env_files | where { |it| + not ($it in $tracked_file_paths) + } + + ( + $scanned + | input list -m "Select files to back up" + | each { envr backup $in } + ) +} + +# Search for hidden env files +def "find env-files" [ + match: string = '\.env' + exclude: string = '*.envrc' + search: string = '~/' +]: nothing -> list { + let search = ($search | path expand); + + let unignored = (fd -a $match -E $exclude -H $search | lines) + let all = (fd -a $match -E $exclude -HI $search | lines) + let ignored = $all | where { |it| + not ($it in $unignored) + } + + $ignored | sort } # Edit your config