-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oma-ubuntu-cmd-not-found): init for ubuntu command-not-found query
- Loading branch information
Showing
6 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ oma-fetch = { path = "./oma-fetch", default-features = false } | |
oma-topics = { path = "./oma-topics", optional = true, default-features = false } | ||
oma-history = { path = "./oma-history" } | ||
oma-repo-verify = { path = "./oma-repo-verify" } | ||
oma-ubuntu-cmd-not-found = { path = "./oma-ubuntu-cmd-not-found", optional = true } | ||
|
||
# i18n | ||
i18n-embed = { version = "0.15.0", features = ["fluent-system", "desktop-requester"]} | ||
|
@@ -70,13 +71,14 @@ sequoia-openssl-backend = ["oma-refresh/sequoia-openssl-backend"] | |
sequoia-nettle-backend = ["oma-refresh/sequoia-nettle-backend"] | ||
egg = ["dep:colored", "dep:image"] | ||
tokio-console = ["dep:console-subscriber"] | ||
ubuntu = ["dep:oma-ubuntu-cmd-not-found"] | ||
rustls = ["reqwest/rustls-tls", "oma-fetch/rustls", "oma-refresh/rustls", "oma-topics/rustls"] | ||
openssl = ["reqwest/native-tls", "oma-fetch/native-tls", "oma-refresh/native-tls", "oma-topics/native-tls"] | ||
generic = ["sequoia-nettle-backend", "rustls"] | ||
default = ["aosc", "generic"] | ||
|
||
[workspace] | ||
members = ["oma-contents", "oma-console", "oma-topics", "oma-fetch", "oma-refresh", "oma-utils", "oma-pm", "oma-history", "oma-pm-operation-type", "oma-repo-verify"] | ||
members = ["oma-contents", "oma-console", "oma-topics", "oma-fetch", "oma-refresh", "oma-utils", "oma-pm", "oma-history", "oma-pm-operation-type", "oma-repo-verify", "oma-ubuntu-cmd-not-found"] | ||
|
||
[package.metadata.deb] | ||
copyright = "2024, AOSC Dev <[email protected]>" | ||
|
@@ -104,3 +106,4 @@ default-features = false | |
lto = "thin" | ||
opt-level = 3 | ||
codegen-units = 1 | ||
debug = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "oma-ubuntu-cmd-not-found" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "Ubuntu command-not-found database handling library" | ||
|
||
[dependencies] | ||
rusqlite = { version = "0.32", features = ["bundled"] } | ||
tracing = "0.1" | ||
thiserror = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use rusqlite::Connection; | ||
pub use rusqlite::Error; | ||
use std::path::Path; | ||
|
||
pub struct UbuntuCmdNotFound { | ||
db: Connection, | ||
} | ||
|
||
impl UbuntuCmdNotFound { | ||
const DEFAULT_DB_PATH: &str = "/var/lib/command-not-found/commands.db"; | ||
pub fn new(db: impl AsRef<Path>) -> Result<Self, rusqlite::Error> { | ||
let db = Connection::open(db)?; | ||
|
||
Ok(Self { db }) | ||
} | ||
|
||
pub fn default_new() -> Result<Self, rusqlite::Error> { | ||
Self::new(Self::DEFAULT_DB_PATH) | ||
} | ||
|
||
pub fn query_where_command_like(&self, query: &str) -> Result<Vec<(String, String)>, Error> { | ||
let mut stmt = self | ||
.db | ||
.prepare("SELECT pkgID, command FROM commands WHERE command LIKE ?1")?; | ||
let query_str = format!("{}%", query); | ||
let res_iter = stmt.query_map([query_str], |row| { | ||
let pkg_id: i64 = row.get(0)?; | ||
let cmd: String = row.get(1)?; | ||
|
||
Ok((pkg_id, cmd)) | ||
})?; | ||
let mut res = vec![]; | ||
for i in res_iter { | ||
let (pkg_id, cmd) = i?; | ||
let pkgs = self.get_pkg_from_from_pkg_id(pkg_id)?; | ||
|
||
for pkg in pkgs { | ||
res.push((pkg, cmd.clone())); | ||
} | ||
} | ||
Ok(res) | ||
} | ||
|
||
pub fn query_where_command_count(&self, query: &str) -> Result<i64, Error> { | ||
let mut stmt = self | ||
.db | ||
.prepare("SELECT COUNT(command) AS count FROM commands WHERE command = ?1")?; | ||
let mut res_iter = stmt.query_map([query], |row| row.get(0))?; | ||
|
||
if let Some(Ok(n)) = res_iter.next() { | ||
return Ok(n); | ||
} | ||
|
||
Ok(0) | ||
} | ||
|
||
pub fn query_where_command_like_count(&self, query: &str) -> Result<i64, Error> { | ||
let mut stmt = self | ||
.db | ||
.prepare("SELECT COUNT(command) AS count FROM commands WHERE command LIKE ?")?; | ||
let query_str = format!("{}%", query); | ||
let mut res_iter = stmt.query_map([query_str], |row| row.get(0))?; | ||
|
||
if let Some(Ok(n)) = res_iter.next() { | ||
return Ok(n); | ||
} | ||
|
||
Ok(0) | ||
} | ||
|
||
pub fn query_where_command(&self, query: &str) -> Result<Vec<(String, String)>, Error> { | ||
let mut stmt = self | ||
.db | ||
.prepare("SELECT pkgID, command FROM commands WHERE command = ?1")?; | ||
let res_iter = stmt.query_map([query], |row| { | ||
let pkg_id: i64 = row.get(0)?; | ||
let cmd: String = row.get(1)?; | ||
|
||
Ok((pkg_id, cmd)) | ||
})?; | ||
let mut res = vec![]; | ||
for i in res_iter { | ||
let (pkg_id, cmd) = i?; | ||
let pkgs = self.get_pkg_from_from_pkg_id(pkg_id)?; | ||
|
||
for pkg in pkgs { | ||
res.push((pkg, cmd.clone())); | ||
} | ||
} | ||
Ok(res) | ||
} | ||
|
||
fn get_pkg_from_from_pkg_id(&self, pkg_id: i64) -> Result<Vec<String>, rusqlite::Error> { | ||
let mut res = vec![]; | ||
let mut stmt = self | ||
.db | ||
.prepare("SELECT name FROM packages where pkgID = ?1")?; | ||
let pkgs = stmt.query_map([pkg_id], |row| row.get(0))?; | ||
|
||
for pkg in pkgs { | ||
let pkg = pkg?; | ||
res.push(pkg); | ||
} | ||
|
||
Ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters