-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathoutput.rs
78 lines (71 loc) · 2.92 KB
/
output.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::fmt::Debug;
use atty::{self, Stream};
use prettytable::{cell, row, Table};
use rover_client::query::subgraph::list::ListDetails;
/// RoverStdout defines all of the different types of data that are printed
/// to `stdout`. Every one of Rover's commands should return `anyhow::Result<RoverStdout>`
/// If the command needs to output some type of data, it should be structured
/// in this enum, and its print logic should be handled in `RoverStdout::print`
///
/// Not all commands will output machine readable information, and those should
/// return `Ok(RoverStdout::None)`. If a new command is added and it needs to
/// return something that is not described well in this enum, it should be added.
#[derive(Clone, PartialEq, Debug)]
pub enum RoverStdout {
SDL(String),
SchemaHash(String),
SubgraphList(ListDetails),
None,
}
impl RoverStdout {
pub fn print(&self) {
match self {
RoverStdout::SDL(sdl) => {
// we check to see if stdout is redirected
// if it is, we don't print the content descriptor
// this is because it would look strange to see
// SDL:
// and nothing after the colon if you piped the output
// to another process or a file.
if atty::is(Stream::Stdout) {
tracing::info!("SDL:");
}
println!("{}", &sdl);
}
RoverStdout::SchemaHash(hash) => {
if atty::is(Stream::Stdout) {
tracing::info!("Schema Hash:");
}
println!("{}", &hash);
}
RoverStdout::SubgraphList(details) => {
if atty::is(Stream::Stdout) {
tracing::info!("Subgraphs:");
}
let mut table = Table::new();
table.add_row(row!["Name", "Routing Url", "Last Updated"]);
for subgraph in &details.subgraphs {
// if the url is None or empty (""), then set it to "N/A"
let url = subgraph.url.clone().unwrap_or_else(|| "N/A".to_string());
let url = if url.is_empty() {
"N/A".to_string()
} else {
url
};
let formatted_updated_at: String = if let Some(dt) = subgraph.updated_at {
dt.format("%Y-%m-%d %H:%M:%S %Z").to_string()
} else {
"N/A".to_string()
};
table.add_row(row![subgraph.name, url, formatted_updated_at]);
}
println!("{}", table);
println!(
"View full details at {}/graph/{}/service-list",
details.root_url, details.graph_name
);
}
RoverStdout::None => (),
}
}
}