48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use anyhow::Result;
|
|
use clap::{Parser, Subcommand, Args};
|
|
|
|
use nom_nom_gc::models::{read_config, self, AppState, Configuration, User, UserUuid};
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Parser, Debug)]
|
|
#[command(author, version, about, long_about = None)]
|
|
struct CLIArgs {
|
|
#[arg(short, long)]
|
|
config: Option<String>,
|
|
#[command(subcommand)]
|
|
command: Command
|
|
}
|
|
|
|
#[derive(Subcommand, Debug)]
|
|
enum Command {
|
|
RegisterUser(RegisterUserArgs)
|
|
}
|
|
|
|
#[derive(Args, Debug)]
|
|
struct RegisterUserArgs {
|
|
username: String
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
let args = CLIArgs::parse();
|
|
let config = read_config(&args.config.unwrap_or("/etc/nom-nom-gc/config.json".to_owned()))
|
|
.unwrap_or_else(|e| panic!("Cannot read config file: {}", e));
|
|
// todo: don't consume config in appstate new
|
|
let state = models::AppState::new(config.clone()).await;
|
|
match args.command {
|
|
Command::RegisterUser(args) => register_user(args.username, state, config).await,
|
|
}
|
|
}
|
|
|
|
async fn register_user(user_name: String, state: AppState<'_>, conf: Configuration) -> Result<()> {
|
|
let user = User {
|
|
uuid: UserUuid(Uuid::new_v4()),
|
|
name: user_name,
|
|
};
|
|
state.save_user(&user).await?;
|
|
let uuid = state.generate_registration_uuid(&user.uuid).await?;
|
|
println!("Registration link: {}/account/register/{}", &conf.url, &uuid.0.to_string());
|
|
Ok(())
|
|
}
|