Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/cmd/bucket/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::io::std::output;
use crate::parse::Resource;
use clap::{ArgMatches, Command};
use reduct_rs::ReductClient;
use serde_json::json;

pub(super) fn create_bucket_cmd() -> Command {
let cmd = Command::new("create").about("Create a bucket");
Expand All @@ -24,16 +25,31 @@ pub(super) async fn create_bucket(ctx: &CliContext, args: &ArgMatches) -> anyhow
.unwrap()
.clone()
.pair()?;

let is_json = args
.try_get_one::<bool>("json")
.ok()
.flatten()
.copied()
.unwrap_or(false);

let bucket_settings = parse_bucket_settings(args);

let client: ReductClient = build_client(ctx, &alias_or_url).await?;

client
.create_bucket(&bucket_name)
.settings(bucket_settings)
.send()
.await?;

output!(ctx, "Bucket '{}' created", bucket_name);
if !is_json {
output!(ctx, "Bucket '{}' created", bucket_name);
} else {
ctx.stdout()
.print(serde_json::to_string_pretty(&json!({})).unwrap().as_str());
}

Ok(())
}

Expand Down
35 changes: 35 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::cmd::alias::{alias_cmd, alias_handler};
use crate::cmd::attachment::{attachment_cmd, attachment_handler};
use crate::cmd::write::{write_handler, write_record_cmd};
use crate::context::ContextBuilder;
use serde_json::json;
use std::time::Duration;

use crate::cmd::bucket::{bucket_cmd, bucket_handler};
Expand Down Expand Up @@ -69,6 +70,15 @@ fn cli() -> Command {
.required(false)
.global(true),
)
.arg(
Arg::new("json")
.long("json")
.short('j')
.help("Print output in JSON format")
.required(false)
.action(SetTrue)
.global(true),
)
.subcommand(alias_cmd())
.subcommand(attachment_cmd())
.subcommand(server_cmd())
Expand Down Expand Up @@ -111,10 +121,35 @@ async fn main() -> anyhow::Result<()> {
_ => Ok(()),
};

let command = matches.subcommand().unwrap().0;

if let Err(err) = result {
// Do not output json if the command is "cp"
if matches.get_flag("json") && command != "cp" {
let json_error = print_json_error(&err);
eprintln!("{}", serde_json::to_string_pretty(&json_error)?);
std::process::exit(1);
}

eprintln!("{}", err.to_string().red().bold(),);
std::process::exit(1);
}

Ok(())
}

fn print_json_error(err: &anyhow::Error) -> serde_json::Value {
// Try to downcast to ReductError to get the status code
let (status_code, error_message) =
if let Some(reduct_err) = err.downcast_ref::<reduct_rs::ReductError>() {
(reduct_err.status() as i32, reduct_err.message().to_string())
} else {
// If not a ReductError, use 1 as unknown status
(1, err.to_string())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @vbmade2000 , sorry looks like I missed your request. Yes, we can continue with the following approach but it should be -1 to be compatible with ReductStore error codes: https://github.com/reductstore/reductstore/blob/661918503a8d762f599ad1a7589c23330fb6c464/reduct_base/src/error.rs#L23

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No probs. I'll keep that in mind.

};

json!({
"status_code": status_code,
"error_message": error_message,
})
}