Setup commands moved to a subcommand menu
This commit is contained in:
46
src/app.rs
46
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(),
|
||||
}
|
||||
}
|
||||
|
||||
35
src/app/admin.rs
Normal file
35
src/app/admin.rs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
0
src/app/setup.rs
Normal file
0
src/app/setup.rs
Normal file
42
src/main.rs
42
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(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user