Set up a client and basic command infrastructure

This commit is contained in:
Marc Plano-Lesay 2024-10-24 11:55:16 +11:00
parent 56a22203ef
commit 46338adca4
6 changed files with 278 additions and 1 deletions

29
src/args.rs Normal file
View file

@ -0,0 +1,29 @@
use clap::{command, Parser, Subcommand};
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub(crate) struct Opts {
#[arg(short, long)]
pub server_url: String,
#[arg(short, long)]
pub api_key: String,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub(crate) enum Commands {
/// Server related commands
Server {
#[command(subcommand)]
server_command: ServerCommands,
},
}
#[derive(Subcommand)]
pub(crate) enum ServerCommands {
/// Fetches the version of the server
Version {},
}

1
src/commands/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod server_version;

View file

@ -0,0 +1,9 @@
use anyhow::Result;
use crate::Client;
pub async fn server_version(client: &Client) -> Result<()> {
let version = client.get_server_version().await?;
println!("{}.{}.{}", version.major, version.minor, version.patch);
Ok(())
}

View file

@ -1,3 +1,45 @@
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
fn main() {}
use crate::args::Commands;
use anyhow::Result;
use args::ServerCommands;
use clap::Parser;
use commands::server_version::server_version;
use reqwest::header;
mod args;
mod commands;
#[tokio::main]
async fn main() {
let args = args::Opts::parse();
let client = get_client(&args.server_url, &args.api_key).unwrap();
let res = match &args.command {
Commands::Server { server_command } => match server_command {
ServerCommands::Version {} => server_version(&client).await,
},
};
match res {
Ok(_) => {}
Err(e) => println!("Error: {:?}", e),
}
}
fn get_client(url: &str, api_key: &str) -> Result<Client> {
let mut headers = header::HeaderMap::new();
let mut auth_value = header::HeaderValue::from_str(api_key)?;
auth_value.set_sensitive(true);
headers.insert("x-api-key", auth_value);
Ok(Client::new_with_client(
url,
reqwest::Client::builder()
.default_headers(headers)
.build()
.unwrap(),
))
}