1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::database::DbContext;
use crate::errors::Error;
use crate::github::GitHubAPI;
use alfred::Item;
pub struct Workflow<'a> {
api_key: &'a str,
db: DbContext,
}
impl<'a> Workflow<'a> {
#[inline]
pub fn new(api_key: &'a str, database_url: &str) -> Result<Self, Error> {
let db = DbContext::new(database_url)?;
Ok(Workflow { api_key, db })
}
#[inline]
pub fn refresh_cache(&mut self) -> Result<(), Error> {
self.db.run_migrations()?;
let api = GitHubAPI::new(self.api_key);
self.db.delete_repositories()?;
for v in api.accessible_repositories() {
self.db.insert_repositories(&v)?;
}
self.db.optimize()?;
Ok(())
}
#[inline]
pub fn query<'items>(&self, repo_name: &str) -> Result<Vec<Item<'items>>, Error> {
self.db
.find_repositories(repo_name, 10)?
.into_iter()
.map(|repo| {
Ok(alfred::ItemBuilder::new(repo.name_with_owner)
.subtitle(repo.name.clone())
.autocomplete(repo.name)
.arg(format!("open {}", repo.url))
.into_item())
})
.collect::<Result<Vec<_>, _>>()
}
}