CLI
Because curl is so passé
Awesomesauce. We're almost there to a functioning... thing? App? Yes. A functioning app.
The server is all deployed, let's build the cli. Start by making a new src/cli.rs file with the following contents:
#[tokio::main]
async fn main() {
println!("Hello, world!")
}Now we need to tell Cargo.toml about the new binary target, so add the following to Cargo.toml.
[[bin]]
name = "cli"
path = "src/cli.rs"Come to think of it, we'll want some command line argument parsing too, so let's throw that dependency in there now:
cargo add clap --features deriveWhile we're at it, add reqwest too. reqwest is a http request library based on hyper.
cargo add reqwest --features jsonGreat! So we'll want to basically have two commands:
Store a secret
Fetch a secret
Let's start by adding some clap library magic. In the cli.rs file, add this bit at the top:
use std::process::exit;
use clap::Parser;
use uuid::Uuid;
#[derive(Parser, Debug)]
struct Args {
#[arg(short, long)]
store: Option<String>,
#[arg(short, long)]
fetch: Option<Uuid>,
#[arg(short, long)]
code: Option<String>,
}This parses command line arguments and basically accepts the flags "--store", "--fetch", and "--code".
Now, the rest of this code isn't very interesting, so I'll drop it below. Basically it checks whether you have added the store or fetch arguments, and makes the request to our server accordingly. When you write this code, remember to update the SERVER_URI variable!
Now simply run the command to store,
and use the resulting id and code to fetch it
Your results should be "super secret test message". You did it!! Congratulations!!
Last updated
Was this helpful?