Browse Source

passphrase++

fix-typo
Coin de Gamma 3 months ago
parent
commit
a7f8d8d689
  1. 18
      src/main.rs
  2. 16
      src/storage.rs

18
src/main.rs

@ -65,8 +65,8 @@ fn get_prompt(question: &str) -> io::Result<PROMPT> {
} }
// TODO: change password functionality // TODO: change password functionality
// TODO: return storage inited
fn login() -> io::Result<String> { fn login() -> io::Result<String> {
// TODO: check if inited
print!("Enter passphrase for storage: "); print!("Enter passphrase for storage: ");
io::stdout().flush()?; io::stdout().flush()?;
// TODO: check in db // TODO: check in db
@ -75,6 +75,8 @@ fn login() -> io::Result<String> {
if password != "pass" { if password != "pass" {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase"));
} }
print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line
io::stdout().flush()?; // Ensure the changes are reflected immediately
Ok(String::from(password)) Ok(String::from(password))
} }
@ -100,12 +102,11 @@ fn init() -> io::Result<()> {
} }
fn list() -> io:: Result<()> { fn list() -> io:: Result<()> {
// TODO: get storage from login
let passphrase = login()?; let passphrase = login()?;
let st = Storage::from_db(passphrase)?; let st = Storage::from_db(passphrase)?;
let mut vec: Vec<_> = st.items.iter().collect(); for id in st.ids() {
vec.sort(); println!("{}", id);
for item in &vec {
println!("{}", item.id);
} }
Ok(()) Ok(())
} }
@ -123,7 +124,7 @@ fn add(id: &String) -> io::Result<()> {
} }
let content = editor::open_to_edit()?; let content = editor::open_to_edit()?;
st.items.insert(Item::from(id.clone(), content)); st.add(Item::from(id.clone(), content));
st.dump()?; st.dump()?;
Ok(()) Ok(())
@ -140,7 +141,7 @@ fn show(id: &String) -> io::Result<()> {
} }
let item = st.get(id); let item = st.get(id);
editor::open_to_show(&item.content); editor::open_to_show(&item.content)?;
Ok(()) Ok(())
} }
@ -166,8 +167,7 @@ fn run_command() -> io::Result<()> {
PROMPT::NO => Storage::print_init_hint(), PROMPT::NO => Storage::print_init_hint(),
} }
} else { } else {
login()?; list()?
// TODO: list()
} }
} }
} }

16
src/storage.rs

@ -98,7 +98,7 @@ impl Encoder {
} }
pub struct Storage { pub struct Storage {
pub items: HashSet::<Item>, items: HashSet::<Item>,
encoder: Encoder encoder: Encoder
} }
@ -110,6 +110,7 @@ impl Storage {
} }
} }
pub fn from_db(passphrase: String) -> io::Result<Storage> { pub fn from_db(passphrase: String) -> io::Result<Storage> {
// TODO: throw error if it's not inited
let encoder = Encoder::from(passphrase); let encoder = Encoder::from(passphrase);
// TODO: throw error is password is incorrect // TODO: throw error is password is incorrect
let file = fs::File::open(&*STORAGE_PATH)?; let file = fs::File::open(&*STORAGE_PATH)?;
@ -175,6 +176,15 @@ impl Storage {
let path = Path::new(&*STORAGE_FOLDER); let path = Path::new(&*STORAGE_FOLDER);
return path.exists(); return path.exists();
} }
pub fn ids(&self) -> Vec<String> {
let mut result = Vec::new();
for item in self.items.iter() {
result.push(item.id.clone());
}
result.sort();
result
}
pub fn contains(&self, id: &String) -> bool { pub fn contains(&self, id: &String) -> bool {
let item = Item::from_empty(id.clone()); let item = Item::from_empty(id.clone());
@ -186,6 +196,10 @@ impl Storage {
self.items.get(&item).unwrap() self.items.get(&item).unwrap()
} }
pub fn add(&mut self, item: Item) {
self.items.insert(item);
}
pub fn dump(&self) -> io::Result<()> { pub fn dump(&self) -> io::Result<()> {
let mut file = fs::OpenOptions::new() let mut file = fs::OpenOptions::new()
.write(true) .write(true)

Loading…
Cancel
Save