aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw.rs
blob: a110176bd335d4cc465ed1e04f28071393d88a94 (plain) (blame)
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
115
use std::io::Write as _;

fn ensure_agent() {
    let agent_path = std::env::var("RBW_AGENT");
    let agent_path = agent_path
        .as_ref()
        .map(|s| s.as_str())
        .unwrap_or("rbw-agent");
    let status = std::process::Command::new(agent_path).status().unwrap();
    if !status.success() {
        if let Some(code) = status.code() {
            if code != 23 {
                panic!("failed to run agent: {}", status);
            }
        }
    }
}

fn send(msg: &rbw::agent::Message) {
    let mut sock = std::os::unix::net::UnixStream::connect(
        rbw::dirs::runtime_dir().join("socket"),
    )
    .unwrap();
    sock.write_all(serde_json::to_string(msg).unwrap().as_bytes())
        .unwrap();
}

fn login() {
    send(&rbw::agent::Message {
        tty: std::env::var("TTY").ok(),
        action: rbw::agent::Action::Login,
    })
}

fn unlock() {
    send(&rbw::agent::Message {
        tty: std::env::var("TTY").ok(),
        action: rbw::agent::Action::Unlock,
    })
}

fn sync() {
    send(&rbw::agent::Message {
        tty: std::env::var("TTY").ok(),
        action: rbw::agent::Action::Sync,
    })
}

fn list() {
    todo!()
}

fn get() {
    todo!()
}

fn add() {
    todo!()
}

fn generate() {
    todo!()
}

fn edit() {
    todo!()
}

fn remove() {
    todo!()
}

fn lock() {
    todo!()
}

fn purge() {
    todo!()
}

fn main() {
    let matches = clap::App::new("rbw")
        .about("unofficial bitwarden cli")
        .author(clap::crate_authors!())
        .version(clap::crate_version!())
        .subcommand(clap::SubCommand::with_name("login"))
        .subcommand(clap::SubCommand::with_name("unlock"))
        .subcommand(clap::SubCommand::with_name("sync"))
        .subcommand(clap::SubCommand::with_name("list"))
        .subcommand(clap::SubCommand::with_name("get"))
        .subcommand(clap::SubCommand::with_name("add"))
        .subcommand(clap::SubCommand::with_name("generate"))
        .subcommand(clap::SubCommand::with_name("edit"))
        .subcommand(clap::SubCommand::with_name("remove"))
        .subcommand(clap::SubCommand::with_name("lock"))
        .subcommand(clap::SubCommand::with_name("purge"))
        .get_matches();

    ensure_agent();

    match matches.subcommand() {
        ("login", Some(_)) => login(),
        ("unlock", Some(_)) => unlock(),
        ("sync", Some(_)) => sync(),
        ("list", Some(_)) => list(),
        ("get", Some(_)) => get(),
        ("add", Some(_)) => add(),
        ("generate", Some(_)) => generate(),
        ("edit", Some(_)) => edit(),
        ("remove", Some(_)) => remove(),
        ("lock", Some(_)) => lock(),
        ("purge", Some(_)) => purge(),
        _ => unimplemented!(),
    }
}