diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-04-11 18:53:45 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-04-11 18:53:45 -0400 |
commit | f65b65bb24960a75cf1f900819c4005e7729e834 (patch) | |
tree | 568998776208a3d9ba1c4adf738cef4a4fa28ea4 /src/bin/rbw/actions.rs | |
parent | 34053ffef22233c32b731acbf03d79f061e6c63b (diff) | |
download | rbw-f65b65bb24960a75cf1f900819c4005e7729e834.tar.gz rbw-f65b65bb24960a75cf1f900819c4005e7729e834.zip |
refactor client code
Diffstat (limited to 'src/bin/rbw/actions.rs')
-rw-r--r-- | src/bin/rbw/actions.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs new file mode 100644 index 0000000..cca2cf1 --- /dev/null +++ b/src/bin/rbw/actions.rs @@ -0,0 +1,60 @@ +pub fn login() { + simple_action(rbw::agent::Action::Login, "login"); +} + +pub fn unlock() { + simple_action(rbw::agent::Action::Unlock, "unlock"); +} + +pub fn sync() { + simple_action(rbw::agent::Action::Sync, "sync"); +} + +pub fn lock() { + simple_action(rbw::agent::Action::Lock, "lock"); +} + +pub fn quit() { + let mut sock = crate::sock::Sock::connect(); + sock.send(&rbw::agent::Request { + tty: std::env::var("TTY").ok(), + action: rbw::agent::Action::Quit, + }); +} + +pub fn decrypt(cipherstring: &str) -> String { + let mut sock = crate::sock::Sock::connect(); + sock.send(&rbw::agent::Request { + tty: std::env::var("TTY").ok(), + action: rbw::agent::Action::Decrypt { + cipherstring: cipherstring.to_string(), + }, + }); + + let res = sock.recv(); + match res { + rbw::agent::Response::Decrypt { plaintext } => plaintext, + rbw::agent::Response::Error { error } => { + panic!("failed to decrypt: {}", error) + } + _ => panic!("unexpected message: {:?}", res), + } +} + +fn simple_action(action: rbw::agent::Action, desc: &str) { + let mut sock = crate::sock::Sock::connect(); + + sock.send(&rbw::agent::Request { + tty: std::env::var("TTY").ok(), + action, + }); + + let res = sock.recv(); + match res { + rbw::agent::Response::Ack => (), + rbw::agent::Response::Error { error } => { + panic!("failed to {}: {}", desc, error) + } + _ => panic!("unexpected message: {:?}", res), + } +} |