2023-09-12 20:00:38 +00:00
|
|
|
mod api;
|
2023-09-10 22:54:26 +00:00
|
|
|
mod state;
|
2023-09-12 20:00:38 +00:00
|
|
|
mod ui;
|
2023-09-10 22:54:26 +00:00
|
|
|
|
|
|
|
use anyhow::Result;
|
2023-09-13 13:55:40 +00:00
|
|
|
use api::Api;
|
2023-09-12 20:00:38 +00:00
|
|
|
use clap::Parser;
|
|
|
|
|
|
|
|
/// A tui for administrating nerdcult.net
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
struct Args {
|
|
|
|
/// The base URL of the API
|
|
|
|
#[arg(short, long, default_value = "https://api.nerdcult.net")]
|
|
|
|
base_url: Option<String>,
|
|
|
|
|
|
|
|
/// An access token for the API
|
|
|
|
#[arg(short, long)]
|
|
|
|
token: Option<String>,
|
2023-09-23 10:31:09 +00:00
|
|
|
|
|
|
|
/// Skips the check for saved API access data
|
|
|
|
#[arg(short, long)]
|
|
|
|
no_saved_access: bool,
|
2023-09-12 20:00:38 +00:00
|
|
|
}
|
2023-09-10 22:54:26 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2023-09-12 20:00:38 +00:00
|
|
|
let args = Args::parse();
|
2023-09-23 10:31:09 +00:00
|
|
|
let base_url = args.base_url.unwrap();
|
2023-09-10 22:54:26 +00:00
|
|
|
|
|
|
|
let stdout = ui::prepare()?;
|
|
|
|
|
2023-09-23 10:31:09 +00:00
|
|
|
let api = match if let Some(token) = args.token {
|
|
|
|
match Api::from_token(&base_url, token).await? {
|
|
|
|
Ok(a) => Some(a),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
} else if !args.no_saved_access {
|
|
|
|
Api::from_file().await?
|
2023-09-13 13:55:40 +00:00
|
|
|
} else {
|
2023-09-23 10:31:09 +00:00
|
|
|
None
|
|
|
|
} {
|
|
|
|
Some(a) => a,
|
|
|
|
None => {
|
|
|
|
let mut login = ui::login::UI::new(stdout, base_url)?;
|
|
|
|
login.run().await?
|
|
|
|
}
|
2023-09-13 05:24:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
println!("{:#?}", api);
|
2023-09-10 22:54:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|