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

read cache from env #39

Merged
merged 3 commits into from
Oct 26, 2023
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
9 changes: 6 additions & 3 deletions nu-git-manager/fs/store.nu
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export def get-repo-store-path []: nothing -> path {
}

export def get-repo-store-cache-path []: nothing -> path {
$env.XDG_CACHE_HOME?
| default ($nu.home-path | path join ".cache")
| path join "nu-git-manager/cache.nuon"
$env.GIT_REPOS_CACHE?
| default (
$env.XDG_CACHE_HOME?
| default ($nu.home-path | path join ".cache")
| path join "nu-git-manager/cache.nuon"
)
| path expand
| path sanitize
}
Expand Down
25 changes: 20 additions & 5 deletions nu-git-manager/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def "nu-complete git-protocols" []: nothing -> table<value: string, description:
# - `~/.local/share/repos`
#
# `nu-git-manager` will look for a cache in the following places, in order:
# - `$env.GIT_REPOS_CACHE`
# - `$env.XDG_CACHE_HOME | path join "nu-git-manager/cache.nuon"
# - `~/.cache/nu-git-manager/cache.nuon`
#
Expand Down Expand Up @@ -95,7 +96,13 @@ export def "gm clone" [
check-cache-file $cache_file

print --no-newline "updating cache... "
open $cache_file | append $local_path | uniq | sort | save --force $cache_file
open --raw $cache_file
| from nuon
| append $local_path
| uniq
| sort
| to nuon
| save --force $cache_file
print "done"

null
Expand All @@ -118,7 +125,7 @@ export def "gm list" [
let cache_file = get-repo-store-cache-path
check-cache-file $cache_file

let repos = open $cache_file
let repos = open --raw $cache_file | from nuon
if $full_path {
$repos
} else {
Expand Down Expand Up @@ -170,7 +177,7 @@ export def "gm status" []: nothing -> record<root: record<path: path, exists: bo
let cache_exists = ($cache | path type) == "file"

let missing = if $cache_exists {
open $cache | where ($it | path type) != "dir"
open --raw $cache | from nuon | where ($it | path type) != "dir"
} else {
null
}
Expand Down Expand Up @@ -200,7 +207,10 @@ export def "gm update-cache" []: nothing -> nothing {
mkdir ($cache_file | path dirname)

print --no-newline "updating cache... "
list-repos-in-store | save --force $cache_file
let foo = list-repos-in-store
print ($foo | describe)
print $foo
$foo | to nuon | save --force $cache_file
print "done"

null
Expand Down Expand Up @@ -271,7 +281,12 @@ export def "gm remove" [
check-cache-file $cache_file

print --no-newline "updating cache... "
open $cache_file | where $it != ($root | path join $repo_to_remove) | save --force $cache_file
open --raw $cache_file
| from nuon
| where $it != ($root
| path join $repo_to_remove)
| to nuon
| save --force $cache_file
print "done"

null
Expand Down
8 changes: 5 additions & 3 deletions tests/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ export def get-store-root [] {

export def get-repo-cache [] {
let cases = [
[env, expected];
[env, expected];

[{XDG_CACHE_HOME: null}, "~/.cache/nu-git-manager/cache.nuon"],
[{XDG_CACHE_HOME: "~/xdg"}, "~/xdg/nu-git-manager/cache.nuon"],
[{GIT_REPOS_CACHE: null, XDG_CACHE_HOME: null}, "~/.cache/nu-git-manager/cache.nuon"],
[{GIT_REPOS_CACHE: "~/my_cache", XDG_CACHE_HOME: null}, "~/my_cache"],
[{GIT_REPOS_CACHE: null, XDG_CACHE_HOME: "~/xdg"}, "~/xdg/nu-git-manager/cache.nuon"],
[{GIT_REPOS_CACHE: "~/my_cache", XDG_CACHE_HOME: "~/xdg"}, "~/my_cache"],
]

for case in $cases {
Expand Down