examples_relay_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 clap::{Parser, Subcommand};
6use std::io::{self, Write};
7
8use relay::RelayClient;
9
10#[derive(Debug, Parser)]
11#[command(name = "relay", about = "CLI tool for interacting with Mozilla Relay", long_about = None)]
12struct Cli {
13    #[arg(short, long, action)]
14    verbose: bool,
15
16    #[command(subcommand)]
17    command: Commands,
18}
19
20#[derive(Debug, Subcommand)]
21enum Commands {
22    Fetch,
23}
24
25fn main() -> anyhow::Result<()> {
26    let cli = Cli::parse();
27    init_logging(&cli);
28
29    viaduct_reqwest::use_reqwest_backend();
30
31    let token = prompt_token()?;
32    let client = RelayClient::new("https://relay.firefox.com".to_string(), Some(token));
33
34    match cli.command {
35        Commands::Fetch => fetch_addresses(client?),
36    }
37}
38
39fn init_logging(cli: &Cli) {
40    let log_filter = if cli.verbose {
41        "relay=trace"
42    } else {
43        "relay=info"
44    };
45    env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", log_filter));
46}
47
48fn prompt_token() -> anyhow::Result<String> {
49    println!(
50        "See https://github.com/mozilla/fx-private-relay/blob/main/docs/api_auth.md#debugging-tip"
51    );
52    print!("Enter your Relay auth token: ");
53    io::stdout().flush()?;
54    let mut token = String::new();
55    io::stdin().read_line(&mut token)?;
56    Ok(token.trim().to_string())
57}
58
59fn fetch_addresses(client: RelayClient) -> anyhow::Result<()> {
60    match client.fetch_addresses() {
61        Ok(addresses) => {
62            println!("Fetched {} addresses:", addresses.len());
63            for address in addresses {
64                println!("{:#?}", address);
65            }
66        }
67        Err(e) => {
68            eprintln!("Failed to fetch addresses: {:?}", e);
69        }
70    }
71    Ok(())
72}