xref: /DragonOS/user/apps/user-manage/src/parser/cmd.rs (revision f9fe30be89e89499aad4ef52b4648986bef5a7d8)
1 use std::collections::HashMap;
2 
3 /// 命令类型
4 pub enum CmdType {
5     User,
6     Passwd,
7     Group,
8 }
9 
10 #[derive(Debug, PartialEq, Eq, Hash, Clone)]
11 pub enum CmdOption {
12     /// 用户描述
13     Comment,
14     /// 用户主目录
15     Dir,
16     /// 组名
17     Group,
18     /// 组id
19     Gid,
20     /// 终端程序
21     Shell,
22     /// 用户id
23     Uid,
24     /// 删除用户的home目录
25     Remove,
26     /// 添加到其它用户组中
27     Append,
28     /// 修改用户名
29     Login,
30     /// 设置组密码
31     Passwd,
32     /// 修改组名
33     NewGroupName,
34     /// 无效选项
35     Invalid,
36 }
37 
38 impl From<String> for CmdOption {
39     fn from(s: String) -> Self {
40         match s.as_str() {
41             "-c" => CmdOption::Comment,
42             "-d" => CmdOption::Dir,
43             "-G" => CmdOption::Group,
44             "-g" => CmdOption::Gid,
45             "-s" => CmdOption::Shell,
46             "-u" => CmdOption::Uid,
47             "-r" => CmdOption::Remove,
48             "-a" => CmdOption::Append,
49             "-l" => CmdOption::Login,
50             "-p" => CmdOption::Passwd,
51             "-n" => CmdOption::NewGroupName,
52             _ => CmdOption::Invalid,
53         }
54     }
55 }
56 
57 impl From<CmdOption> for &str {
58     fn from(option: CmdOption) -> Self {
59         match option {
60             CmdOption::Comment => "-c",
61             CmdOption::Dir => "-d",
62             CmdOption::Group => "-G",
63             CmdOption::Shell => "-s",
64             CmdOption::Uid => "-u",
65             CmdOption::Login => "-l",
66             CmdOption::Append => "-a",
67             CmdOption::Gid => "-g",
68             CmdOption::NewGroupName => "-n",
69             CmdOption::Passwd => "-p",
70             CmdOption::Remove => "-r",
71             CmdOption::Invalid => "Invalid option",
72         }
73     }
74 }
75 
76 /// useradd/userdel/usermod命令
77 #[derive(Debug)]
78 pub struct UserCommand {
79     /// 用户名
80     pub username: String,
81     /// 选项
82     pub options: HashMap<CmdOption, String>,
83 }
84 
85 /// passwd命令
86 #[derive(Debug)]
87 pub struct PasswdCommand {
88     pub username: Option<String>,
89 }
90 
91 /// groupadd/groupdel/groupmod命令
92 #[derive(Debug)]
93 pub struct GroupCommand {
94     pub groupname: String,
95     pub options: HashMap<CmdOption, String>,
96 }
97