Skip to content

Commit

Permalink
feat!: suiteql command now fetches all results, suiteql-raw retai…
Browse files Browse the repository at this point in the history
…ns previous behavior

Both commands support pretty printing via `--pretty` flag.
  • Loading branch information
jacobsvante committed Nov 3, 2021
1 parent f4a2689 commit 78690ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum CliError {
BadParam,
#[error("IO error: {0}")]
IOError(#[from] std::io::Error),
#[error("Serde error: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("{0}")]
LibraryError(#[from] crate::error::Error),
}
36 changes: 33 additions & 3 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::{fs::{self, read_to_string}, io::{stdout, Write}, net::SocketAddrV4, os::unix::prelude::OsStrExt};
use std::{
fs::{self, read_to_string},
io::{stdout, Write},
net::SocketAddrV4,
os::unix::prelude::OsStrExt,
};

use clap::Parser;
use log::{debug, LevelFilter};

use super::error::CliError;
use super::opts::{Opts, RestApiSubCommand, SubCommand};
use super::{helpers::safe_extract_arg, ini};
use super::error::CliError;
use crate::config::Config;
use crate::rest_api::RestApi;

Expand Down Expand Up @@ -51,13 +56,38 @@ fn rest_api_sub_command(subcmd: RestApiSubCommand, api: RestApi) -> Result<(), C
use RestApiSubCommand::*;
match subcmd {
RestApiSubCommand::SuiteQl {
path,
threads,
pretty,
} => {
let query = read_to_string(path)?;
let api = {
let mut api = api;
api.suiteql.set_threads(threads);
api
};
let resp = api.suiteql.fetch_values(&query)?;
let dumped = if pretty {
serde_json::to_string_pretty(&resp)?
} else {
serde_json::to_string(&resp)?
};
println!("{}", dumped);
}
RestApiSubCommand::SuiteQlRaw {
path,
limit,
offset,
pretty,
} => {
let query = read_to_string(path)?;
let resp = api.suiteql.raw(&query, limit, offset)?;
println!("{}", &resp.body());
let dumped = if pretty {
serde_json::to_string_pretty(&resp)?
} else {
serde_json::to_string(&resp)?
};
println!("{}", dumped);
}
Get {
endpoint,
Expand Down
16 changes: 16 additions & 0 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::net::SocketAddrV4;
use std::num::NonZeroU8;
use std::{path::PathBuf, str::FromStr};

use clap::Parser;
Expand Down Expand Up @@ -113,15 +114,30 @@ pub(crate) enum RestApiSubCommand {
#[clap(short = 'H', long = "header")]
headers: Vec<ParamStr>,
},
/// Fetch all SuiteQL results for the given query
#[clap(name = "suiteql")]
SuiteQl {
/// Path to SQL file to execute. Defaults to reading from standard input.
#[clap(default_value = "/dev/stdin")]
path: PathBuf,
/// Number of threads to use, i.e. number of concurrent requests.
#[clap(short, long, default_value = "10")]
threads: NonZeroU8,
#[clap(short = 'P', long)]
pretty: bool,
},
/// Fetch a raw SuiteQL result page, including pagination etc
#[clap(name = "suiteql-raw")]
SuiteQlRaw {
/// Path to SQL file to execute. Defaults to reading from standard input.
#[clap(default_value = "/dev/stdin")]
path: PathBuf,
#[clap(short, long, default_value = "1000")]
limit: usize,
#[clap(short, long, default_value = "0")]
offset: usize,
#[clap(short = 'P', long)]
pretty: bool,
},
#[clap(name = "jsonschema")]
JsonSchema {
Expand Down

0 comments on commit 78690ca

Please sign in to comment.