this repo has no description
1use std::process::Command;
2
3use websocket::sync::client::ClientBuilder;
4
5mod args;
6mod did;
7mod knot;
8
9fn main() -> Result<(), ()> {
10 // load configuration
11 let config = match args::load_config() {
12 Ok(res) => res,
13 Err(_) => return Err(()),
14 };
15
16 // resolve handle to did
17 println!("Resolving {}", config.handle);
18 let did_doc = match did::get_did(&config.handle) {
19 Ok(res) => res,
20 Err(_) => return Err(()),
21 };
22 println!(
23 " Found DID `{0}` hosted at `{1}`",
24 did_doc.did, did_doc.pds
25 );
26
27 // resolve did+repoName to knotserver
28 println!("Resolving @{0}/{1}", config.handle, config.repo_name);
29 let knot_server = match knot::find_knot(&did_doc.did, &config.repo_name, &did_doc.pds) {
30 Ok(val) => val,
31 Err(_) => return Err(()),
32 };
33 println!(" Found knot `{}`", knot_server);
34
35 // connect to /events on knotserver
36 println!(
37 "Connecting to `{}`",
38 format!("wss://{}/events", knot_server)
39 );
40 let mut client = match ClientBuilder::new(&format!("wss://{}/events", knot_server)) {
41 Ok(mut val) => match val.connect(None) {
42 Ok(val) => val,
43 Err(_) => {
44 println!(" Couldn't open a connection");
45 return Err(());
46 }
47 },
48 Err(_) => {
49 println!(" Knot server was malformed.");
50 return Err(());
51 }
52 };
53 println!(" Connected successfully.");
54
55 // on event:
56 for message in client.incoming_messages() {
57 if let Ok(ref val) = message
58 && let websocket::OwnedMessage::Text(body) = val
59 {
60 // parse json
61 let body = match json::parse(&body) {
62 Ok(val) => val,
63 Err(_) => {
64 println!(" Invalid body. Skipping message");
65 continue;
66 }
67 };
68
69 // filter by did and reponame
70 if let Some(repo_did) = body["event"]["repoDid"].as_str()
71 && let Some(repo) = body["event"]["repoName"].as_str()
72 && repo_did != &did_doc.did
73 && repo != &config.repo_name
74 {
75 // repo doesnt match so skip
76 continue;
77 }
78
79 // exec shell command in /bin/sh
80 match Command::new("/bin/sh")
81 .arg("-c")
82 .arg(&config.shell)
83 .output()
84 {
85 Ok(val) => val,
86 Err(_) => continue,
87 };
88 } else if let Err(err) = message {
89 println!(" Got websocket error: {}", err)
90 }
91 }
92
93 return Ok(());
94}