Skip to content

Commit 280b867

Browse files
chore: adds data.success bool to json output (#681)
1 parent c680eeb commit 280b867

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

src/cli.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use camino::Utf8PathBuf;
22
use reqwest::blocking::Client;
33
use serde::Serialize;
4-
use serde_json::json;
54
use structopt::{clap::AppSettings, StructOpt};
65

6+
use crate::command::output::JsonOutput;
77
use crate::command::{self, RoverOutput};
88
use crate::utils::{
99
client::StudioClientConfig,
@@ -115,16 +115,15 @@ impl Rover {
115115
match rover_output {
116116
Ok(output) => {
117117
if self.json {
118-
let data = output.get_internal_json();
119-
println!("{}", json!({"data": data, "error": null}));
118+
println!("{}", JsonOutput::success(output));
120119
} else {
121120
output.print();
122121
}
123122
process::exit(0);
124123
}
125124
Err(error) => {
126125
if self.json {
127-
println!("{}", json!({"data": null, "error": error}));
126+
println!("{}", JsonOutput::error(error));
128127
} else {
129128
tracing::debug!(?error);
130129
eprint!("{}", error);

src/command/output.rs

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::Debug;
22
use std::{collections::HashMap, fmt::Display};
33

4+
use crate::error::RoverError;
45
use crate::utils::table::{self, cell, row};
56

67
use ansi_term::{
@@ -13,6 +14,7 @@ use rover_client::operations::graph::publish::GraphPublishResponse;
1314
use rover_client::operations::subgraph::list::SubgraphListResponse;
1415
use rover_client::operations::subgraph::publish::SubgraphPublishResponse;
1516
use rover_client::shared::{CheckResponse, FetchResponse, GraphRef, SdlType};
17+
use serde::Serialize;
1618
use serde_json::{json, Value};
1719
use termimad::MadSkin;
1820

@@ -192,7 +194,7 @@ impl RoverOutput {
192194
}
193195
}
194196

195-
pub fn get_internal_json(&self) -> Option<Value> {
197+
pub(crate) fn get_internal_json(&self) -> Value {
196198
match self {
197199
RoverOutput::DocsList(shortlinks) => {
198200
let mut shortlink_vec = vec![];
@@ -201,32 +203,69 @@ impl RoverOutput {
201203
json!({"slug": shortlink_slug, "description": shortlink_description }),
202204
);
203205
}
204-
Some(json!({ "shortlinks": shortlink_vec }))
206+
json!({ "shortlinks": shortlink_vec })
205207
}
206-
RoverOutput::FetchResponse(fetch_response) => Some(json!(fetch_response)),
207-
RoverOutput::CoreSchema(csdl) => Some(json!({ "core_schema": csdl })),
208+
RoverOutput::FetchResponse(fetch_response) => json!(fetch_response),
209+
RoverOutput::CoreSchema(csdl) => json!({ "core_schema": csdl }),
208210
RoverOutput::GraphPublishResponse {
209211
graph_ref: _,
210212
publish_response,
211-
} => Some(json!(publish_response)),
213+
} => json!(publish_response),
212214
RoverOutput::SubgraphPublishResponse {
213215
graph_ref: _,
214216
subgraph: _,
215217
publish_response,
216-
} => Some(json!(publish_response)),
217-
RoverOutput::SubgraphList(list_response) => Some(json!(list_response)),
218-
RoverOutput::CheckResponse(check_response) => Some(json!(check_response)),
219-
RoverOutput::VariantList(variants) => Some(json!({ "variants": variants })),
220-
RoverOutput::Profiles(profiles) => Some(json!({ "profiles": profiles })),
218+
} => json!(publish_response),
219+
RoverOutput::SubgraphList(list_response) => json!(list_response),
220+
RoverOutput::CheckResponse(check_response) => json!(check_response),
221+
RoverOutput::VariantList(variants) => json!({ "variants": variants }),
222+
RoverOutput::Profiles(profiles) => json!({ "profiles": profiles }),
221223
RoverOutput::Introspection(introspection_response) => {
222-
Some(json!({ "introspection_response": introspection_response }))
224+
json!({ "introspection_response": introspection_response })
223225
}
224-
RoverOutput::Markdown(markdown_string) => Some(json!({ "markdown": markdown_string })),
225-
RoverOutput::None => None,
226+
RoverOutput::Markdown(markdown_string) => json!({ "markdown": markdown_string }),
227+
RoverOutput::None => json!(null),
226228
}
227229
}
228230
}
229231

232+
#[derive(Debug, Clone, Serialize)]
233+
pub(crate) struct JsonOutput {
234+
pub(crate) data: JsonData,
235+
pub(crate) error: Value,
236+
}
237+
238+
impl JsonOutput {
239+
pub(crate) fn success(output: RoverOutput) -> Value {
240+
let json_output = JsonOutput {
241+
data: JsonData {
242+
inner: output.get_internal_json(),
243+
success: true,
244+
},
245+
error: json!(null),
246+
};
247+
json!(json_output)
248+
}
249+
250+
pub(crate) fn error(error: RoverError) -> Value {
251+
let json_output = JsonOutput {
252+
data: JsonData {
253+
inner: json!(null),
254+
success: false,
255+
},
256+
error: json!(error),
257+
};
258+
json!(json_output)
259+
}
260+
}
261+
262+
#[derive(Debug, Clone, Serialize)]
263+
pub(crate) struct JsonData {
264+
#[serde(flatten)]
265+
pub(crate) inner: Value,
266+
pub(crate) success: bool,
267+
}
268+
230269
fn print_descriptor(descriptor: impl Display) {
231270
if atty::is(Stream::Stdout) {
232271
eprintln!("{}: \n", Style::new().bold().paint(descriptor.to_string()));

0 commit comments

Comments
 (0)