use std::io::{self, Write};
use std::str::FromStr;
use anyhow::Context;
use serde::Serialize;
use serde_json;
use tonic::transport::Uri;
pub fn https_default_scheme(url: String) -> anyhow::Result<String> {
let mut uri_parts = Uri::from_str(&url).context("invalid url")?.into_parts();
if uri_parts.authority.is_none() {
bail!("invalid url '{}': missing authority", url);
}
if uri_parts.scheme.is_none() {
uri_parts.scheme = Some("https".parse().unwrap());
uri_parts.path_and_query = Some(uri_parts.path_and_query
.unwrap_or_else(|| "".parse().unwrap())
);
let new = Uri::from_parts(uri_parts).unwrap();
Ok(new.to_string())
} else {
Ok(url)
}
}
pub fn output_json<T>(value: &T)
where
T: ?Sized + Serialize,
{
serde_json::to_writer_pretty(io::stdout(), value).expect("value is serializable");
write!(io::stdout(), "\n").expect("Failed to write newline to stdout");
}