merino_cli/
main.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use anyhow::Result;
6use clap::{Parser, Subcommand};
7use merino::curated_recommendations::{
8    CuratedRecommendationsClient, CuratedRecommendationsConfig, CuratedRecommendationsRequest,
9};
10
11#[derive(Debug, Parser)]
12struct Cli {
13    /// Optional base host (defaults to prod)
14    #[arg(long)]
15    base_host: Option<String>,
16
17    /// Required user agent header
18    #[arg(long)]
19    user_agent: String,
20
21    #[command(subcommand)]
22    command: Commands,
23}
24
25#[derive(Debug, Subcommand)]
26enum Commands {
27    Query {
28        /// JSON string of type CuratedRecommendationsRequest
29        #[clap(long)]
30        json: Option<String>,
31
32        /// Path to a JSON file containing the request
33        #[clap(long, value_name = "FILE")]
34        json_file: Option<std::path::PathBuf>,
35    },
36}
37
38fn main() -> Result<()> {
39    let cli = Cli::parse();
40
41    viaduct_reqwest::use_reqwest_backend();
42    let config = CuratedRecommendationsConfig {
43        base_host: cli.base_host.clone(),
44        user_agent_header: cli.user_agent.clone(),
45    };
46
47    let client = CuratedRecommendationsClient::new(config)?;
48
49    match cli.command {
50        Commands::Query { json, json_file } => {
51            let json_data = match (json_file, json) {
52                (Some(path), _) => std::fs::read_to_string(path)?,
53                (None, Some(raw)) => raw,
54                (None, None) => anyhow::bail!("You must provide either --json or --json-file"),
55            };
56
57            query_from_json(json_data, &client)?;
58        }
59    };
60
61    Ok(())
62}
63
64fn query_from_json(json: String, client: &CuratedRecommendationsClient) -> Result<()> {
65    let request: CuratedRecommendationsRequest = serde_json::from_str(&json)?;
66    let response = client.get_curated_recommendations(&request)?;
67
68    println!("{}", serde_json::to_string_pretty(&response)?);
69    Ok(())
70}