use std::process::exit; use clap::{Parser, Subcommand}; use figment::Figment; use crate::config::Config; use crate::cli::{NomiloCommand}; use crate::models::{LocalUser, CreateUserRequest, Role}; use crate::get_db_conn; #[derive(Subcommand)] pub enum UserCommand { /// Add new user Add(AddUserCommand), } #[derive(Parser)] pub struct AddUserCommand { #[clap(long = "--email", short = 'e')] pub email: String, #[clap(long = "--is-admin", short = 'a')] pub is_admin: bool, #[clap(long = "--password", short = 'p')] pub password: Option, } impl NomiloCommand for UserCommand { fn run(self, figment: Figment, app_config: Config) { match self { UserCommand::Add(sub) => sub.run(figment, app_config), }; } } impl NomiloCommand for AddUserCommand { fn run(self, figment: Figment, _app_config: Config) { let res = LocalUser::create_user(&get_db_conn(&figment), CreateUserRequest { email: self.email, role: Some(if self.is_admin { Role::Admin } else { Role::ZoneAdmin }), password: self.password.unwrap(), }); match res { Ok(_) => println!("Successfully added user"), Err(err) => { eprintln!("Error while adding user: {:?}", err); exit(1); } }; } }