refactor: remove digest fields from db and item struct
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
894a893536
commit
a8c363333c
50
README.org
50
README.org
@@ -1,10 +1,8 @@
|
|||||||
#+TITLE: Keep
|
#+TITLE: Keep
|
||||||
#+AUTHOR: Andrew Phillips
|
#+AUTHOR: Andrew Phillips
|
||||||
#+EMAIL: andrew@gt0.ca
|
|
||||||
#+DATE: 2024-01-01
|
|
||||||
|
|
||||||
* Introduction
|
* Introduction
|
||||||
Keep is a command-line utility designed to manage temporary files created on the command line. Instead of redirecting output to a temporary file (e.g., *command > ~/whatever.tmp*), you can use *keep* to handle the temporary files for you (e.g., *command | keep*).
|
Keep is a command-line utility designed to manage temporary files created on the command line. Instead of redirecting output to a temporary file (e.g., =command > ~/whatever.tmp=), you can use =keep= to handle the temporary files for you (e.g., =command | keep=).
|
||||||
|
|
||||||
* Installation
|
* Installation
|
||||||
To install Keep, you need to have Rust and Cargo installed on your system. You can then build and install Keep using the following commands:
|
To install Keep, you need to have Rust and Cargo installed on your system. You can then build and install Keep using the following commands:
|
||||||
@@ -18,82 +16,84 @@ cargo install --path .
|
|||||||
Keep provides several subcommands to manage temporary files. Below are some examples of how to use Keep.
|
Keep provides several subcommands to manage temporary files. Below are some examples of how to use Keep.
|
||||||
|
|
||||||
** Saving an Item
|
** Saving an Item
|
||||||
To save an item with tags and metadata, you can use the *--save* option:
|
To save an item with tags and metadata, you can use the =--save= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
echo "Hello, world!" | keep --save --tag example --meta key=value
|
echo "Hello, world!" | keep --save example --meta key=value
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Getting an Item
|
** Getting an Item
|
||||||
To retrieve an item by its ID or by matching tags and metadata, you can use the *--get* option:
|
To retrieve an item by its ID or by matching tags and metadata, you can use the =--get= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --get 1
|
keep --get 1
|
||||||
keep --get --tag example
|
keep --get example
|
||||||
keep --get --meta key=value
|
keep --get --meta key=value
|
||||||
|
keep 1
|
||||||
|
keep example
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Listing Items
|
** Listing Items
|
||||||
To list all items or filter them by tags and metadata, you can use the *--list* option:
|
To list all items or filter them by tags and metadata, you can use the =--list= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --list
|
keep --list
|
||||||
keep --list --tag example
|
keep --list example
|
||||||
keep --list --meta key=value
|
keep --list --meta key=value
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Updating an Item
|
** Updating an Item
|
||||||
To update an item's tags and metadata, you can use the *--update* option:
|
To update an item's tags and metadata, you can use the =--update= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --update 1 --tag newtag --meta key=newvalue
|
keep --update 1 newtag --meta key=newvalue
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Deleting an Item
|
** Deleting an Item
|
||||||
To delete an item by its ID or by matching tags, you can use the *--delete* option:
|
To delete an item by its ID or by matching tags, you can use the =--delete= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --delete 1
|
keep --delete 1
|
||||||
keep --delete --tag example
|
keep --delete example
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Showing Status
|
** Showing Status
|
||||||
To show the status of directories and supported compression algorithms, you can use the *--status* option:
|
To show the status of directories and supported compression algorithms, you can use the =--status= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --status
|
keep --status
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Diffing Items
|
** Diffing Items
|
||||||
To show a diff between two items by ID, you can use the *--diff* option:
|
To show a diff between two items by ID, you can use the =--diff= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --diff 1 2
|
keep --diff 1 2
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Getting Information About an Item
|
** Getting Information About an Item
|
||||||
To get detailed information about an item by its ID or by matching tags and metadata, you can use the *--info* option:
|
To get detailed information about an item by its ID or by matching tags and metadata, you can use the =--info= option:
|
||||||
|
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --info 1
|
keep --info 1
|
||||||
keep --info --tag example
|
keep --info example
|
||||||
keep --info --meta key=value
|
keep --info --meta key=value
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
* Configuration
|
* Configuration
|
||||||
Keep can be configured using environment variables and command-line options. The following environment variables are supported:
|
Keep can be configured using environment variables and command-line options. The following environment variables are supported:
|
||||||
|
|
||||||
- *KEEP_DIR*: Specify the directory to use for storage.
|
- =KEEP_DIR=: Specify the directory to use for storage.
|
||||||
- *KEEP_LIST_FORMAT*: A comma-separated list of columns to display with *--list*.
|
- =KEEP_LIST_FORMAT=: A comma-separated list of columns to display with =--list=.
|
||||||
- *KEEP_DIGEST*: Digest algorithm to use when saving items.
|
- =KEEP_DIGEST=: Digest algorithm to use when saving items.
|
||||||
- *KEEP_COMPRESSION*: Compression algorithm to use when saving items.
|
- =KEEP_COMPRESSION=: Compression algorithm to use when saving items.
|
||||||
|
|
||||||
* Examples
|
* Examples
|
||||||
Here are some examples of how to use Keep with different options:
|
Here are some examples of how to use Keep with different options:
|
||||||
|
|
||||||
** Saving an Item with Compression and Digest
|
** Saving an Item with Compression and Digest
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
echo "Hello, world!" | keep --save --tag example --meta key=value --compression gzip --digest sha256
|
echo "Hello, world!" | keep --save example --meta key=value --compression gzip --digest sha256
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Getting an Item with Human-Readable Sizes
|
** Getting an Item with Human-Readable Sizes
|
||||||
@@ -108,12 +108,12 @@ keep --list --list-format "id,time,size,tags,meta:hostname"
|
|||||||
|
|
||||||
** Updating an Item with New Tags and Metadata
|
** Updating an Item with New Tags and Metadata
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --update 1 --tag newtag --meta key=newvalue
|
keep --update 1 newtag --meta key=newvalue
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Deleting an Item by Tag
|
** Deleting an Item by Tag
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --delete --tag example
|
keep --delete example
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Showing Status with Verbose Output
|
** Showing Status with Verbose Output
|
||||||
@@ -128,7 +128,7 @@ keep --diff 1 2
|
|||||||
|
|
||||||
** Getting Information About an Item with Metadata
|
** Getting Information About an Item with Metadata
|
||||||
#+BEGIN_SRC sh
|
#+BEGIN_SRC sh
|
||||||
keep --info 1 --meta key=value
|
keep --info 1
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
* License
|
* License
|
||||||
|
|||||||
32
src/db.rs
32
src/db.rs
@@ -32,8 +32,6 @@ lazy_static! {
|
|||||||
FOREIGN KEY(id) REFERENCES items(id) ON DELETE CASCADE,
|
FOREIGN KEY(id) REFERENCES items(id) ON DELETE CASCADE,
|
||||||
PRIMARY KEY(id, name));"
|
PRIMARY KEY(id, name));"
|
||||||
),
|
),
|
||||||
M::up("ALTER TABLE items ADD COLUMN digest_type TEXT NOT NULL DEFAULT 'none';"),
|
|
||||||
M::up("ALTER TABLE items ADD COLUMN digest_value TEXT NULL;"),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,8 +41,6 @@ pub struct Item {
|
|||||||
pub ts: DateTime<Utc>,
|
pub ts: DateTime<Utc>,
|
||||||
pub size: Option<i64>,
|
pub size: Option<i64>,
|
||||||
pub compression: String,
|
pub compression: String,
|
||||||
pub digest_type: String,
|
|
||||||
pub digest_value: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -83,8 +79,8 @@ pub fn open(path: PathBuf) -> Result<Connection, Error> {
|
|||||||
pub fn insert_item(conn: &Connection, item: Item) -> Result<i64> {
|
pub fn insert_item(conn: &Connection, item: Item) -> Result<i64> {
|
||||||
debug!("DB: Inserting item: {:?}", item);
|
debug!("DB: Inserting item: {:?}", item);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO items (ts, size, compression, digest_type, digest_value) VALUES (?1, ?2, ?3, ?4, ?5)",
|
"INSERT INTO items (ts, size, compression) VALUES (?1, ?2, ?3)",
|
||||||
params![item.ts, item.size, item.compression, item.digest_type, item.digest_value],
|
params![item.ts, item.size, item.compression],
|
||||||
)?;
|
)?;
|
||||||
Ok(conn.last_insert_rowid())
|
Ok(conn.last_insert_rowid())
|
||||||
}
|
}
|
||||||
@@ -92,13 +88,11 @@ pub fn insert_item(conn: &Connection, item: Item) -> Result<i64> {
|
|||||||
pub fn update_item(conn: &Connection, item: Item) -> Result<()> {
|
pub fn update_item(conn: &Connection, item: Item) -> Result<()> {
|
||||||
debug!("DB: Updating item: {:?}", item);
|
debug!("DB: Updating item: {:?}", item);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"UPDATE items SET size=?2, compression=?3, digest_type=?4, digest_value=?5 WHERE id=?1",
|
"UPDATE items SET size=?2, compression=?3 WHERE id=?1",
|
||||||
params![
|
params![
|
||||||
item.id,
|
item.id,
|
||||||
item.size,
|
item.size,
|
||||||
item.compression,
|
item.compression,
|
||||||
item.digest_type,
|
|
||||||
item.digest_value
|
|
||||||
],
|
],
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -174,7 +168,7 @@ pub fn set_item_tags(conn: &Connection, item: Item, tags: &Vec<String>) -> Resul
|
|||||||
pub fn query_all_items(conn: &Connection) -> Result<Vec<Item>> {
|
pub fn query_all_items(conn: &Connection) -> Result<Vec<Item>> {
|
||||||
debug!("DB: Querying all items");
|
debug!("DB: Querying all items");
|
||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare("SELECT id, ts, size, compression, digest_type, digest_value FROM items ORDER BY id ASC")
|
.prepare("SELECT id, ts, size, compression FROM items ORDER BY id ASC")
|
||||||
.context("Problem preparing SQL statement")?;
|
.context("Problem preparing SQL statement")?;
|
||||||
let mut rows = statement.query(params![])?;
|
let mut rows = statement.query(params![])?;
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
@@ -185,8 +179,6 @@ pub fn query_all_items(conn: &Connection) -> Result<Vec<Item>> {
|
|||||||
ts: row.get(1)?,
|
ts: row.get(1)?,
|
||||||
size: row.get(2)?,
|
size: row.get(2)?,
|
||||||
compression: row.get(3)?,
|
compression: row.get(3)?,
|
||||||
digest_type: row.get(4)?,
|
|
||||||
digest_value: row.get(5)?,
|
|
||||||
};
|
};
|
||||||
items.push(item);
|
items.push(item);
|
||||||
}
|
}
|
||||||
@@ -203,8 +195,6 @@ pub fn query_tagged_items<'a>(conn: &'a Connection, tags: &'a Vec<String>) -> Re
|
|||||||
items.ts,
|
items.ts,
|
||||||
items.size,
|
items.size,
|
||||||
items.compression,
|
items.compression,
|
||||||
items.digest_type,
|
|
||||||
items.digest_value,
|
|
||||||
count(tags_match.id) as tags_score
|
count(tags_match.id) as tags_score
|
||||||
FROM items,
|
FROM items,
|
||||||
(SELECT tags.id FROM tags WHERE tags.name IN rarray(?1)) as tags_match
|
(SELECT tags.id FROM tags WHERE tags.name IN rarray(?1)) as tags_match
|
||||||
@@ -231,8 +221,6 @@ pub fn query_tagged_items<'a>(conn: &'a Connection, tags: &'a Vec<String>) -> Re
|
|||||||
ts: row.get(1)?,
|
ts: row.get(1)?,
|
||||||
size: row.get(2)?,
|
size: row.get(2)?,
|
||||||
compression: row.get(3)?,
|
compression: row.get(3)?,
|
||||||
digest_type: row.get(4)?,
|
|
||||||
digest_value: row.get(5)?,
|
|
||||||
};
|
};
|
||||||
items.push(item);
|
items.push(item);
|
||||||
}
|
}
|
||||||
@@ -307,8 +295,6 @@ pub fn get_item_matching(
|
|||||||
items.ts,
|
items.ts,
|
||||||
items.size,
|
items.size,
|
||||||
items.compression,
|
items.compression,
|
||||||
items.digest_type,
|
|
||||||
items.digest_value,
|
|
||||||
count(sel.id) as score
|
count(sel.id) as score
|
||||||
FROM items,
|
FROM items,
|
||||||
(SELECT tags.id FROM tags WHERE tags.name IN rarray(?1)) as sel
|
(SELECT tags.id FROM tags WHERE tags.name IN rarray(?1)) as sel
|
||||||
@@ -335,8 +321,6 @@ pub fn get_item_matching(
|
|||||||
ts: row.get(1)?,
|
ts: row.get(1)?,
|
||||||
size: row.get(2)?,
|
size: row.get(2)?,
|
||||||
compression: row.get(3)?,
|
compression: row.get(3)?,
|
||||||
digest_type: row.get(4)?,
|
|
||||||
digest_value: row.get(5)?,
|
|
||||||
})),
|
})),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
@@ -347,7 +331,7 @@ pub fn get_item(conn: &Connection, item_id: i64) -> Result<Option<Item>> {
|
|||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare_cached(
|
.prepare_cached(
|
||||||
"
|
"
|
||||||
SELECT id, ts, size, compression, digest_type, digest_value
|
SELECT id, ts, size, compression
|
||||||
FROM items
|
FROM items
|
||||||
WHERE items.id = ?1",
|
WHERE items.id = ?1",
|
||||||
)
|
)
|
||||||
@@ -361,8 +345,6 @@ pub fn get_item(conn: &Connection, item_id: i64) -> Result<Option<Item>> {
|
|||||||
ts: row.get(1)?,
|
ts: row.get(1)?,
|
||||||
size: row.get(2)?,
|
size: row.get(2)?,
|
||||||
compression: row.get(3)?,
|
compression: row.get(3)?,
|
||||||
digest_type: row.get(4)?,
|
|
||||||
digest_value: row.get(5)?,
|
|
||||||
})),
|
})),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
@@ -373,7 +355,7 @@ pub fn get_item_last(conn: &Connection) -> Result<Option<Item>> {
|
|||||||
let mut statement = conn
|
let mut statement = conn
|
||||||
.prepare_cached(
|
.prepare_cached(
|
||||||
"
|
"
|
||||||
SELECT id, ts, size, compression, digest_type, digest_value
|
SELECT id, ts, size, compression
|
||||||
FROM items
|
FROM items
|
||||||
ORDER BY id DESC
|
ORDER BY id DESC
|
||||||
LIMIT 1",
|
LIMIT 1",
|
||||||
@@ -388,8 +370,6 @@ pub fn get_item_last(conn: &Connection) -> Result<Option<Item>> {
|
|||||||
ts: row.get(1)?,
|
ts: row.get(1)?,
|
||||||
size: row.get(2)?,
|
size: row.get(2)?,
|
||||||
compression: row.get(3)?,
|
compression: row.get(3)?,
|
||||||
digest_type: row.get(4)?,
|
|
||||||
digest_value: row.get(5)?,
|
|
||||||
})),
|
})),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,8 +52,6 @@ pub fn mode_save(
|
|||||||
ts: Utc::now(),
|
ts: Utc::now(),
|
||||||
size: None,
|
size: None,
|
||||||
compression: compression_type.to_string(),
|
compression: compression_type.to_string(),
|
||||||
digest_type: digest_type.to_string(),
|
|
||||||
digest_value: Some(String::new()),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let id = db::insert_item(conn, item.clone())?;
|
let id = db::insert_item(conn, item.clone())?;
|
||||||
|
|||||||
Reference in New Issue
Block a user