Skip to content

Commit fb16503

Browse files
committed
cache nuke, cache rm bugfix to cache add
1 parent beabcbd commit fb16503

File tree

5 files changed

+89
-20
lines changed

5 files changed

+89
-20
lines changed

Diff for: nopkg/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
## TODO
44

55
- Cache
6-
- Implement `nopkg cache show`
7-
- Add `--since` and `--max-age` flags
8-
- Add time zone conversion flag
9-
- Implement `nopkg cache remove`
10-
- Implement `nopkg cache nuke`
116
- Implement `nopkg cache update`
7+
- Add `--since` and `--max-age` flags to relevant commands
8+
- Add TZ conversion to `nopkg cache show`
9+
- Add time zone conversion flag
1210
- Install
1311
- Load manifest
1412
- Load lockfile

Diff for: nopkg/src/cache/index.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anyhow::Result;
55
use chrono::{DateTime, Utc};
66
use refinery::embed_migrations;
77
use rusqlite::{Connection, Row, Statement};
8-
use tracing::trace;
8+
use tracing::debug;
99
use xdg::BaseDirectories;
1010

1111
use crate::cache::id::get_id;
@@ -14,7 +14,7 @@ use crate::cache::id::get_id;
1414
fn place_index(dirs: &BaseDirectories) -> Result<PathBuf> {
1515
let path = dirs.place_cache_file("index.json")?;
1616

17-
trace!("Placing index at {:?}", path);
17+
debug!("Placing index at {:?}", path);
1818

1919
Ok(path)
2020
}
@@ -40,6 +40,7 @@ fn from_timestamp(timestamp: f64) -> DateTime<Utc> {
4040
pub(crate) fn map_entry(row: &Row) -> std::result::Result<Entry, rusqlite::Error> {
4141
let url = row.get(0)?;
4242
let id = row.get(1)?;
43+
debug!("mapping entry");
4344
let modified_at = from_timestamp(row.get(2)?);
4445

4546
Ok(Entry {
@@ -70,13 +71,20 @@ impl Index {
7071

7172
self.db.execute(
7273
"insert into files (url, id, modified_at) values (?1, ?2, ?3) \
73-
on conflict(id) do update set url = ?1, modified_at = ?2",
74+
on conflict(id) do update set url = ?1, modified_at = ?3;",
7475
(url, &id, &modified_at),
7576
)?;
7677

7778
Ok(())
7879
}
7980

81+
pub(crate) fn remove_file(&mut self, url_or_id: &str) -> Result<()> {
82+
self.db
83+
.execute("delete from files where url = ?1 or id = ?1;", (url_or_id,))?;
84+
85+
Ok(())
86+
}
87+
8088
pub(crate) fn entries(&self) -> Result<Statement> {
8189
let stmt = self.db.prepare("select url, id, modified_at from files;")?;
8290
Ok(stmt)

Diff for: nopkg/src/cache/mod.rs

+47-7
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ use crate::cache::download::download_file;
1212
use crate::cache::id::get_id;
1313
use crate::cache::index::Index;
1414

15-
fn file_path(url: &str) -> Result<PathBuf> {
16-
let id = get_id(url)?;
17-
18-
let path = format!("file/{}", id.as_str());
15+
fn id_path(id: &str) -> Result<PathBuf> {
16+
let path = format!("file/{}", id);
1917
let path = Path::new(&path);
2018

2119
Ok(path.to_path_buf())
2220
}
2321

22+
fn url_path(url: &str) -> Result<PathBuf> {
23+
let id = get_id(url)?;
24+
let path = id_path(id.as_str())?;
25+
Ok(path)
26+
}
27+
2428
/*
2529
fn unpacked_file_path(url: &str, path: &str) -> Result<PathBuf> {
2630
let id = get_id(url)?;
@@ -40,7 +44,7 @@ fn repo_path(url: &str) -> Result<PathBuf> {
4044
}
4145

4246
pub(crate) struct Cache {
43-
dirs: BaseDirectories,
47+
pub(crate) dirs: BaseDirectories,
4448
pub(crate) index: Index,
4549
}
4650

@@ -55,16 +59,41 @@ impl Cache {
5559
Ok(Cache { dirs, index })
5660
}
5761

58-
// TODO: Should these take &str?
62+
pub(crate) fn destroy(&self) -> Result<()> {
63+
let path = self.dirs.get_cache_home();
64+
std::fs::remove_dir_all(path)?;
65+
Ok(())
66+
}
67+
5968
pub(crate) fn place_file(&self, url: &str) -> Result<PathBuf> {
60-
let path = file_path(url)?;
69+
let path = url_path(url)?;
6170
let path = self.dirs.place_cache_file(path)?;
6271

6372
trace!("Placing {} at {:?}", url, path);
6473

6574
Ok(path)
6675
}
6776

77+
pub(crate) fn find_file(&self, url_or_id: &str) -> Result<Option<PathBuf>> {
78+
let path = self.find_file_by_id(url_or_id)?;
79+
80+
if let Some(path) = path {
81+
return Ok(Some(path));
82+
}
83+
84+
Ok(self.find_file_by_url(url_or_id)?)
85+
}
86+
87+
pub(crate) fn find_file_by_id(&self, id: &str) -> Result<Option<PathBuf>> {
88+
let path = id_path(id)?;
89+
Ok(self.dirs.find_cache_file(path))
90+
}
91+
92+
pub(crate) fn find_file_by_url(&self, url: &str) -> Result<Option<PathBuf>> {
93+
let path = url_path(url)?;
94+
Ok(self.dirs.find_cache_file(path))
95+
}
96+
6897
/*
6998
pub(crate) fn place_unpacked_file(&self, url: &str, path: &str) -> Result<PathBuf> {
7099
let unpacked_path = unpacked_file_path(url, path)?;
@@ -96,6 +125,17 @@ pub(crate) async fn get_file(cache: &mut Cache, url: &str) -> Result<PathBuf> {
96125
Ok(path)
97126
}
98127

128+
pub(crate) fn remove_file(cache: &mut Cache, url_or_id: &str) -> Result<()> {
129+
let path = cache.find_file(url_or_id)?;
130+
131+
if let Some(path) = path {
132+
std::fs::remove_file(path)?;
133+
}
134+
135+
cache.index.remove_file(url_or_id)?;
136+
Ok(())
137+
}
138+
99139
// TODO: git clone
100140
// TODO: git checkout
101141
// TODO: git pull

Diff for: nopkg/src/commands/cache.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@ use chrono::{DateTime, Local};
33
use tabled::{builder::Builder, settings::Style};
44

55
use crate::cache::index::map_entry;
6-
use crate::cache::{Cache, get_file};
6+
use crate::cache::{Cache, get_file, remove_file};
77

88
pub(crate) async fn cache_add_command(url: &String) -> Result<()> {
99
let mut cache = Cache::new()?;
1010
get_file(&mut cache, url).await?;
1111
Ok(())
1212
}
1313

14-
pub(crate) fn cache_clean_command() -> Result<()> {
14+
pub(crate) fn cache_remove_command(url: &String) -> Result<()> {
15+
let mut cache = Cache::new()?;
16+
remove_file(&mut cache, url)?;
17+
Ok(())
18+
}
19+
20+
pub(crate) fn cache_destroy_command() -> Result<()> {
21+
let cache = Cache::new()?;
22+
cache.destroy()?;
1523
Ok(())
1624
}
1725

@@ -26,7 +34,10 @@ pub(crate) fn cache_show_command() -> Result<()> {
2634

2735
root_builder.push_record(["cache entries"]);
2836

37+
let mut seen = false;
38+
2939
for entry in entries {
40+
seen = true;
3041
let entry = entry?;
3142

3243
// TODO: Flag for when to do this conversion
@@ -45,6 +56,10 @@ pub(crate) fn cache_show_command() -> Result<()> {
4556
root_builder.push_record([format!("{}", entry_table)]);
4657
}
4758

59+
if !seen {
60+
root_builder.push_record(["(the cache is empty)"]);
61+
}
62+
4863
let mut root_table = root_builder.build();
4964
root_table.with(Style::rounded());
5065

Diff for: nopkg/src/main.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ mod manifest;
1111
mod solver;
1212

1313
use crate::commands::add::add_command;
14-
use crate::commands::cache::{cache_add_command, cache_clean_command, cache_show_command};
14+
use crate::commands::cache::{
15+
cache_add_command, cache_destroy_command, cache_remove_command, cache_show_command,
16+
};
1517
use crate::commands::completion::completion_command;
1618
use crate::commands::init::init_command;
1719
use crate::commands::install::install_command;
@@ -83,9 +85,14 @@ enum CacheCommand {
8385
url: String,
8486
},
8587

88+
/// Remove a url from the cache
89+
Remove {
90+
url: String,
91+
},
92+
8693
// Clear the cache
8794
#[clap(alias = "nuke")]
88-
Clear,
95+
Destroy,
8996

9097
// Show the state of the cache
9198
Show,
@@ -133,7 +140,8 @@ async fn main() -> Result<()> {
133140
} => add_command(url, file, unpack, manifest_path),
134141
Commands::Cache { command } => match &command {
135142
CacheCommand::Add { url } => cache_add_command(url).await,
136-
CacheCommand::Clear => cache_clean_command(),
143+
CacheCommand::Remove { url } => cache_remove_command(url),
144+
CacheCommand::Destroy => cache_destroy_command(),
137145
CacheCommand::Show => cache_show_command(),
138146
},
139147
Commands::Completion { shell } => {

0 commit comments

Comments
 (0)