Skip to content

Commit

Permalink
feat: add argument to just print the request instead of sending it
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoccesar committed Nov 10, 2023
1 parent b783139 commit 961ccd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 8 additions & 1 deletion linkup-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ enum Commands {
)]
config: Option<String>,
// TODO: Easy way to have validation and parsing of tuples.
#[arg(help = "service=url pairs to preview.")]
services: Vec<String>,
#[arg(long, help = "Print the request body instead of sending it.")]
print_request: bool,
},
}

Expand All @@ -219,6 +222,10 @@ fn main() -> Result<()> {
LocalDNSSubcommand::Uninstall => local_dns::uninstall(config),
},
Commands::Completion { shell } => completion(shell),
Commands::Preview { config, services } => preview(config, services),
Commands::Preview {
config,
services,
print_request,
} => preview(config, services, *print_request),
}
}
18 changes: 13 additions & 5 deletions linkup-cli/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use linkup::CreatePreviewRequest;
use reqwest::blocking::Client;
use reqwest::StatusCode;

pub fn preview(config: &Option<String>, services: &[String]) -> Result<(), CliError> {
pub fn preview(
config: &Option<String>,
services: &[String],
print_request: bool,
) -> Result<(), CliError> {
if services.is_empty() {
// TODO: Oliver don't care about this error handling (type)
return Err(CliError::BadConfig("No services specified".to_string()));
Expand All @@ -21,16 +25,20 @@ pub fn preview(config: &Option<String>, services: &[String]) -> Result<(), CliEr
let input_config = get_config(&config_path)?;
let create_preview_request: CreatePreviewRequest =
input_config.create_preview_request(&services);
let url = input_config.linkup.remote.clone();
let create_req_json = serde_json::to_string(&create_preview_request)
.map_err(|e| CliError::LoadConfig(url.to_string(), e.to_string()))?;

if print_request {
println!("{}", create_req_json);
return Ok(());
}

let client = Client::new();
let url = input_config.linkup.remote.clone();
let endpoint = url
.join("/preview")
.map_err(|e| CliError::LoadConfig(url.to_string(), e.to_string()))?;

let create_req_json = serde_json::to_string(&create_preview_request)
.map_err(|e| CliError::LoadConfig(url.to_string(), e.to_string()))?;

let response = client
.post(endpoint.clone())
.body(create_req_json)
Expand Down

0 comments on commit 961ccd0

Please sign in to comment.