Skip to content

Commit

Permalink
bins:gnostr-get-relays.rs:handle --nip <int> and output type
Browse files Browse the repository at this point in the history
src/bin/gnostr-get-relays.rs
src/watch_list.rs
  • Loading branch information
RandyMcMillan committed Sep 9, 2024
1 parent 8ddfa6f commit a28236f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
33 changes: 31 additions & 2 deletions bins/src/bin/gnostr-get-relays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@ use std::env;

use futures::executor::block_on;
use gnostr_bins::{get_stripped_urls, get_watch_list, get_watch_list_json, print_watch_list};

use gnostr_bins::watch_list::{parse_urls, parse_json, stripped_urls};
pub fn handle_command(mut args: env::Args) -> Result<bool, Box<dyn std::error::Error>> {
let _ = args.next(); // program name
let nip: String;
let relays: String;
//default json output
if args.len() == 2 && args.next().unwrap() == "--nip"{
nip = String::from(args.next().unwrap());
relays = gnostr_bins::get_relays_by_nip(&nip)?;
let relays_json = parse_json(&relays);
let _ = block_on(relays_json);
std::process::exit(0);
}
if args.len() == 3 && args.next().unwrap() == "--nip"{
nip = String::from(args.next().unwrap());
relays = gnostr_bins::get_relays_by_nip(&nip)?;
let output_type = args.next().unwrap();
if output_type == "-j" {
let relays_json = parse_json(&relays);
let _ = block_on(relays_json);
}
if output_type == "-p" {
let relays_json = parse_urls(&relays);
let _ = block_on(relays_json);
}
if output_type == "-s" {
let relays_json = stripped_urls(&relays);
let _ = block_on(relays_json);
}
std::process::exit(0);
}
let command = args.next().unwrap(); // must be there or we would not have been called

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -147,7 +175,8 @@ fn gnostr_get_relays_stripped() {
fn gnostr_get_relays_handle_command() {
let args = env::args();
if args.len() > 1 {
handle_command(env::args());

handle_command(args);
}
println!();
}
6 changes: 6 additions & 0 deletions bins/src/watch_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct Relay {
url: String,
}

/// pub async fn parse_json(urls_str: &str) -> Result<Vec<String>>
///
pub async fn parse_json(urls_str: &str) -> Result<Vec<String>> {
let mut part = String::new();
let mut collected = Vec::new();
Expand Down Expand Up @@ -44,6 +46,8 @@ pub async fn parse_json(urls_str: &str) -> Result<Vec<String>> {
}
Ok(collected)
}
/// pub async fn parse_urls(urls_str: &str) -> Result<Vec<String>>
///
pub async fn parse_urls(urls_str: &str) -> Result<Vec<String>> {
let mut part = String::new();
let mut collected = Vec::new();
Expand Down Expand Up @@ -71,6 +75,8 @@ pub async fn parse_urls(urls_str: &str) -> Result<Vec<String>> {
}
Ok(collected)
}
/// pub async fn stripped_urls(urls_str: &str) -> Result<Vec<String>>
///
pub async fn stripped_urls(urls_str: &str) -> Result<Vec<String>> {
let mut part = String::new();
let mut collected = Vec::new();
Expand Down

0 comments on commit a28236f

Please sign in to comment.