-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstdout_subscriber.rs
52 lines (48 loc) · 1.53 KB
/
stdout_subscriber.rs
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
extern crate log;
use clap::{crate_authors, crate_version, App as ClApp, Arg};
use futures::{
sink::SinkExt,
stream::{StreamExt, TryStreamExt},
};
use post::subscriber::Subscription;
use std::convert::TryInto;
use std::error::Error as StdError;
use tokio_util::codec;
#[tokio::main]
async fn main() -> Result<(), Box<dyn StdError>> {
env_logger::init();
let matches = ClApp::new("stdin_Publisher")
.version(crate_version!())
.author(crate_authors!("\n"))
.arg(
Arg::with_name("url")
.short("u")
.long("url")
.required(true)
.takes_value(true),
)
.get_matches();
let base_url = matches.value_of("url").unwrap();
let mut client = post::find_service::Client::from_url(base_url)?
.connect()
.await?;
let desc = client
.get_descriptors_for_name("stdin".to_string())
.await?
.list
.into_iter()
.next()
.expect("No Publisher found")
.publisher
.expect("Registration without description")
.try_into()
.expect("Conversion from proto to regular description failed");
let sub = Subscription::new(desc).await?;
sub.map(|b| String::from_utf8(std::convert::From::from(b.as_ref())))
.map_err(Box::<dyn StdError>::from)
.forward(SinkExt::<String>::sink_map_err(
codec::FramedWrite::new(tokio::io::stdout(), codec::LinesCodec::new()),
Box::<dyn StdError>::from,
))
.await
}