Set up a client and basic command infrastructure
This commit is contained in:
		
							parent
							
								
									56a22203ef
								
							
						
					
					
						commit
						46338adca4
					
				
					 6 changed files with 278 additions and 1 deletions
				
			
		
							
								
								
									
										29
									
								
								src/args.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/args.rs
									
										
									
									
									
										Normal 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
									
								
							
							
						
						
									
										1
									
								
								src/commands/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
pub mod server_version;
 | 
			
		||||
							
								
								
									
										9
									
								
								src/commands/server_version.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/commands/server_version.rs
									
										
									
									
									
										Normal 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(())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										44
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -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(),
 | 
			
		||||
    ))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue