Skip to content

Commit

Permalink
Merge pull request #1 from gnostr-org/1708814796/0d1432e/dd09829-supp…
Browse files Browse the repository at this point in the history
…ort-json-csv-output

1708814796/0d1432e/dd09829 support json csv output
  • Loading branch information
RandyMcMillan authored Feb 24, 2024
2 parents 6972b5b + 8765802 commit 6b139e1
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 33 deletions.
160 changes: 159 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
[package]
name = "gnostr-get-relays"
version = "0.0.0"
version = "0.0.3"
edition = "2021"
authors = ["gnostr <admin@gnostr.org>"]
description = "gnostr-get-relays: part of the gnostr workflow commands"
repository = "https://github.com/gnostr-org/gnostr-get-relays"
documentation = "https://docs.rs/gnostr-get-relays"
readme = "README.md"
keywords = [ "nostr", "gnostr", "git"]
keywords = [ "nostr", "gnostr", "git","ffi" ]
license = "MIT"
build = "build.rs"

[dependencies]
curl = "0.4.46"
getopts = "0.2.21"
libc = "0.2"

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate libc;

extern {
extern "C" {
pub fn gnostr_get_relays(input: libc::c_int) -> libc::c_int;
}

Expand Down
70 changes: 41 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,45 @@ use getopts::Options;
use std::env;
use std::process;

#[derive(Debug)]
struct NonClone;

extern crate gnostr_get_relays;

// Quick Start
use std::io::{stdout, Write};

use curl::easy::Easy;

// Print a web page onto stdout
fn get_relays_simple() {
let mut easy = Easy::new();
easy.url("https://api.nostr.watch/v1/online/").unwrap();
easy.write_function(|data| {
stdout().write_all(data).unwrap();
Ok(data.len())
})
.unwrap();
easy.perform().unwrap();

//println!("{}", easy.response_code().unwrap());
}

fn do_work(inp: &str, _out: Option<String>) {
println!("{}", inp);
let mut input: i32 = inp
.trim()
.parse()
.expect("Wanted a number");
//println!("{}", inp);
let mut input: i32 = inp.trim().parse().expect("Wanted a number");

if input < 100 {
input = 100;
input = 100;
}
let _output = unsafe { gnostr_get_relays::gnostr_get_relays(input) };
//match _out {
// //TODO call gnostr-set-relays //Some(x) => println!("{}", x),
// Some(x) => println!("{}", x),
// None => println!("No Output"),
//}
}

pub fn print_usage(program: &str, opts: &Options) {
let brief = format!("Usage: {} FILE [options]", program);
print!("{}", opts.usage(&brief));
}


//pub fn print_usage(program: &str, opts: &Options) {
// let brief = format!("Usage: {} [options]", program);
// print!("{}", opts.usage(&brief));
Expand All @@ -45,21 +58,16 @@ pub fn print_input(inp: &str, out: Option<String>) {
}

fn main() {

let args: Vec<String> = env::args().collect();
let program = args[0].clone();
//REF: https://docs.rs/getopts/latest/getopts/struct.Options.html
let mut opts = Options::new();

opts.optopt("o", "", "set output file name", "NAME");
opts.optopt(
"i",
"input",
"Specify the maximum number of commits to show (default: 10)",
"NUMBER",
);

opts.optflag("h", "help", "print this help menu");
opts.optflag("c", "csv", "return comma seperated list");
opts.optflag("j", "json", "return json object");
opts.optopt("o", "output", "set output file name", "NAME");
opts.optopt("i", "input", "Input an integer (default: 10)", "NUMBER");

if args.len() >= 1 {
let matches = match opts.parse(&args[1..]) {
Expand All @@ -73,14 +81,18 @@ fn main() {
print_usage(&program, &opts);
process::exit(0);
}
if matches.opt_present("j") {
//returns json
get_relays_simple();
process::exit(0);
}


let output = matches.opt_str("o");
//leave input as &Option<String>
let input = matches.opt_str("i");
//defed &str
let value = input.as_deref().unwrap_or("100");

do_work(&value, output);
let output = matches.opt_str("o");
//leave input as &Option<String>
let input = matches.opt_str("i");
//defed &str
let value = input.as_deref().unwrap_or("100");
//returns csv list
do_work(&value, output);
}
}

0 comments on commit 6b139e1

Please sign in to comment.