From 2ea28220b3b6212e10f571813090f2177e7e657b Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Fri, 6 Mar 2026 16:10:53 -0500 Subject: [PATCH] Setup commands moved to a subcommand menu --- src/app.rs | 46 +++++----------------------------------------- src/app/admin.rs | 35 +++++++++++++++++++++++++++++++++++ src/app/setup.rs | 0 src/main.rs | 42 +++++++++++++++++++++++++++++++++--------- 4 files changed, 73 insertions(+), 50 deletions(-) create mode 100644 src/app/admin.rs create mode 100644 src/app/setup.rs diff --git a/src/app.rs b/src/app.rs index 51b2091..34cf36f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,13 +1,12 @@ +pub mod admin; + use std::time::{Duration, Instant}; use colored::{ColoredString, Colorize}; -use reqwest::{StatusCode, Url}; +use reqwest::StatusCode; use crate::json_types::*; -const MAS_SERVER_BASE_URL: &str = "https://mas.supersaturn.space"; -const USERS_ENDPT: &str = "/api/admin/v1/users"; -const OAUTH_LINKS_ENDPT: &str = "/api/admin/v1/upstream-oauth-links"; #[derive(Debug)] pub struct App { matrix_base_url: String, @@ -32,11 +31,11 @@ impl App { println!("\nApp Base URL: {}", self.matrix_base_url); println!( "Auth Metadata? {}", - quick_format_bool(self.auth_metadata.is_some()) + crate::quick_format_bool(self.auth_metadata.is_some()) ); println!( "Client Reg? {}", - quick_format_bool(self.client_registration.is_some()) + crate::quick_format_bool(self.client_registration.is_some()) ); } @@ -218,39 +217,4 @@ impl App { println!("Authorized!"); self.auth_token = Some(token); } - - pub fn show_oauth_links(&self) { - let auth_token = self - .auth_token - .as_ref() - .expect("Need auth token to perform this action"); - - let url = format!("{}{OAUTH_LINKS_ENDPT}", self.mas_base_url); - - let client = reqwest::blocking::Client::new(); - - let req = client - .get(url) - .header("Accept", "application/json") - .header( - "Authorization", - format!("Bearer {}", auth_token.access_token.clone()), - ) - .header("Content-Type", "application/json") - .body(include_str!("../oauthlinks.json")); - - // println!("Req {:?}", req); - - let res = req.send().unwrap(); - - let json = prettify_json_str(res.text().unwrap().as_str()).unwrap(); - println!("{}", json); - } -} - -fn quick_format_bool(b: bool) -> ColoredString { - match b { - true => "true".bright_green(), - false => "false".red(), - } } diff --git a/src/app/admin.rs b/src/app/admin.rs new file mode 100644 index 0000000..7b8a2f0 --- /dev/null +++ b/src/app/admin.rs @@ -0,0 +1,35 @@ +const SITE_CONFIG: &str = "/api/admin/v1/site-config"; +const USERS_ENDPT: &str = "/api/admin/v1/users"; +const OAUTH_LINKS_ENDPT: &str = "/api/admin/v1/upstream-oauth-links"; + +use crate::json_types::*; + +impl super::App { + pub fn show_oauth_links(&self) { + let auth_token = self + .auth_token + .as_ref() + .expect("Need auth token to perform this action"); + + let url = format!("{}{OAUTH_LINKS_ENDPT}", self.mas_base_url); + + let client = reqwest::blocking::Client::new(); + + let req = client + .get(url) + .header("Accept", "application/json") + .header( + "Authorization", + format!("Bearer {}", auth_token.access_token.clone()), + ) + .header("Content-Type", "application/json") + .body(include_str!("../../oauthlinks.json")); + + // println!("Req {:?}", req); + + let res = req.send().unwrap(); + + let json = prettify_json_str(res.text().unwrap().as_str()).unwrap(); + println!("{}", json); + } +} diff --git a/src/app/setup.rs b/src/app/setup.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 212f011..535105b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,12 +10,10 @@ mod app; pub mod json_types; -use json_types::*; - use std::io::Write; use clap::{CommandFactory, Parser, Subcommand}; -use colored::Colorize; +use colored::{ColoredString, Colorize}; use enum_iterator::Sequence; use crate::app::App; @@ -62,10 +60,16 @@ fn main() { }; match cli.command { - Commands::GetAuthMetadata => app.get_auth_metadata(), - Commands::RegisterClient => app.register_client(), - Commands::AuthorizeDevice => app.authorize_device(), - Commands::RefreshAuth => app.refresh_auth(), + Commands::Setup(sub_args) => { + println!("subargs: {:?}", sub_args); + + match sub_args.command { + SetupCommands::GetAuthMetadata => app.get_auth_metadata(), + SetupCommands::RegisterClient => app.register_client(), + SetupCommands::AuthorizeDevice => app.authorize_device(), + SetupCommands::RefreshAuth => app.refresh_auth(), + } + } Commands::ShowOauthLinks => app.show_oauth_links(), } } @@ -149,11 +153,31 @@ struct Cli { command: Commands, } -#[derive(Debug, Subcommand, PartialEq, Eq, Sequence)] +#[derive(Debug, Subcommand)] enum Commands { + #[command(subcommand_help_heading = "Setup Auth for Admin API")] + #[command(arg_required_else_help = true)] + Setup(SetupCommandArgs), + ShowOauthLinks, +} + +#[derive(Debug, Parser)] +struct SetupCommandArgs { + #[command(subcommand)] + command: SetupCommands, +} + +#[derive(Debug, Subcommand, PartialEq, Eq, Sequence)] +enum SetupCommands { GetAuthMetadata, RegisterClient, AuthorizeDevice, RefreshAuth, - ShowOauthLinks, +} + +fn quick_format_bool(b: bool) -> ColoredString { + match b { + true => "true".bright_green(), + false => "false".red(), + } }