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
use anyhow::Result;
use serde_json::json;
use std::process;
use tokio::io::{AsyncBufReadExt, BufReader};
use worterbuch_cli::app;
use worterbuch_client::connect;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
    dotenv::dotenv().ok();

    let (_matches, proto, host_addr, port, _json, debug) = app(
        "last-will-test",
        "Test Wörterbuch's last will funciton.",
        vec![],
    )?;

    let on_disconnect = async move {
        eprintln!("Connection to server lost.");
        process::exit(1);
    };

    if debug {
        eprintln!("Server: {proto}://{host_addr}:{port}");
    }

    let mut con = connect(
        &proto,
        &host_addr,
        port,
        vec![("last_will/hello/world", json!("OFFLINE")).into()],
        vec![],
        on_disconnect,
    )
    .await?;

    con.set("last_will/hello/world".to_owned(), json!("ONLINE"))?;

    let mut lines = BufReader::new(tokio::io::stdin()).lines();
    while let Ok(Some(value)) = lines.next_line().await {
        con.set("last_will/hello/world".to_owned(), json!(value))?;
    }

    Ok(())
}