Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 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,26 @@ pub(super) async fn create_bucket(ctx: &CliContext, args: &ArgMatches) -> anyhow
.unwrap()
.clone()
.pair()?;
let is_json = args.get_flag("json");
let bucket_settings = parse_bucket_settings(args);

let client: ReductClient = build_client(ctx, &alias_or_url).await?;
// .map_err(|err| anyhow::anyhow!(get_json_error(err.to_string(), is_json)))?;

client
.create_bucket(&bucket_name)
.settings(bucket_settings)
.send()
.await?;
// .map_err(|err| anyhow::anyhow!(get_json_error(err.to_string(), is_json)))?;

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

output!(ctx, "Bucket '{}' created", bucket_name);
Ok(())
}

Expand Down
22 changes: 22 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,7 +121,19 @@ 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 = json!({
"status": "error",

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.

reduct-rs sdk returns the ReductError with a status and an error message. I think we can attach it to the anyhow error and downcast here to get the status. See https://docs.rs/anyhow/latest/anyhow/trait.Context.html.

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.

Ah I didn't know that ReductError error has status code too. I'll update the code.

"error_message": err.to_string(),
});
eprintln!("{}", serde_json::to_string_pretty(&json_error).unwrap());
std::process::exit(1);
}

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