xref: /DragonOS/user/apps/user-manage/src/cmd/useradd.rs (revision 59a6bcf6aee15a11a16431bdf875905c5ecf9157)
1 use crate::{
2     check::check::UAddCheck,
3     error::error::{ErrorHandler, ExitStatus},
4     executor::executor::UAddExecutor,
5     parser::parser::UserParser,
6 };
7 use libc::geteuid;
8 use std::process::exit;
9 
10 #[path = "../check/mod.rs"]
11 mod check;
12 #[path = "../error/mod.rs"]
13 mod error;
14 #[path = "../executor/mod.rs"]
15 mod executor;
16 #[path = "../parser/mod.rs"]
17 mod parser;
18 
19 #[allow(dead_code)]
20 fn main() {
21     let args = std::env::args().collect::<Vec<_>>();
22 
23     if unsafe { geteuid() } != 0 {
24         ErrorHandler::error_handle(
25             "permission denied (are you root?)".to_string(),
26             ExitStatus::PermissionDenied,
27         )
28     }
29 
30     if args.len() < 2 {
31         ErrorHandler::error_handle(
32             format!("usage: {} [options] username", args[0]),
33             ExitStatus::InvalidCmdSyntax,
34         );
35     }
36 
37     let cmd = UserParser::parse(args);
38     let info = UAddCheck::check(cmd);
39     let username = info.username.clone();
40     UAddExecutor::execute(info);
41     println!("Add user[{}] successfully!", username);
42 
43     exit(ExitStatus::Success as i32);
44 }
45