-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactions.rs
More file actions
114 lines (83 loc) · 2.21 KB
/
actions.rs
File metadata and controls
114 lines (83 loc) · 2.21 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::path::Path;
use seahorse::{ActionError, ActionResult, Context};
use crate::config::get_db_path;
use crate::error::{invalid, to_action_error};
use crate::storage::sqlite::SQLiteStore;
use crate::storage::{self, Store, StoreValue};
pub fn init_action(_c: &Context) -> ActionResult {
let config_path = get_db_path();
let path = Path::new(&config_path);
if path.exists() {
println!("config file already exists");
}
SQLiteStore::from_path(path);
Ok(())
}
pub fn list_action(_c: &Context) -> ActionResult {
let store = storage::load_storage();
for (key, value) in store.all().map_err(to_action_error)?.iter() {
println!("{}\t{}", key, value);
}
Ok(())
}
pub fn clear_action(_c: &Context) -> ActionResult {
let mut store = storage::load_storage();
let count = store.clear().map_err(to_action_error)?;
println!("removed {} keys from store", count);
Ok(())
}
pub fn get_action(c: &Context) -> ActionResult {
if c.args.len() != 1 {
return Err(invalid("command"));
}
let key = c.args.get(0).to_owned();
let Some(key) = key else {
return Err(invalid("key"));
};
let store = storage::load_storage();
let value = store.get(key).map_err(to_action_error)?;
match value {
Some(v) => {
println!("{}", v)
}
None => {
if c.bool_flag("ignore_null") {
println!();
} else {
return Err(ActionError {
message: format!("could not find key '{}'", key),
});
}
}
}
Ok(())
}
pub fn set_action(c: &Context) -> ActionResult {
if c.args.len() != 2 {
return Err(invalid("command"));
}
let Some(key) = c.args.get(0) else {
return Err(invalid("key"));
};
let Some(value_str) = c.args.get(1) else {
return Err(invalid("value"));
};
let mut store = storage::load_storage();
let value = StoreValue::Value(value_str.to_owned());
store.set(key, value.clone()).map_err(to_action_error)?;
println!("'{}' -> '{}'", key, value);
Ok(())
}
pub fn remove_action(c: &Context) -> ActionResult {
let Some(key) = c.args.get(0) else {
return Err(invalid("key"));
};
let mut store = storage::load_storage();
match store.remove(key).map_err(to_action_error)? {
Some(value) => println!("{}\t{}", key, value),
None => {
println!("key '{}' was not found", key);
}
}
Ok(())
}