Mercurial > hg > Members > anatofuz > ie-virsh
annotate src/main.rs @ 36:91a99ff9c4c5
String to &str
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 22 Nov 2020 11:03:09 +0900 |
parents | 1c3b6b6ee0b1 |
children | 0f966575a56a |
rev | line source |
---|---|
8 | 1 use clap::Clap; |
12 | 2 use ie_virsh::{command, user}; |
1 | 3 |
5 | 4 #[derive(Clap)] |
7 | 5 #[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")] |
5 | 6 struct Opts { |
7 #[clap(subcommand)] | |
8 subcmd: SubCommand, | |
9 } | |
10 | |
11 #[derive(Clap)] | |
12 enum SubCommand { | |
10 | 13 List(List), |
22 | 14 Undefine(Undefine), |
10 | 15 Define(Define), |
16 Shutdown(Shutdown), | |
17 Destroy(Destroy), | |
18 Console(Console), | |
19 Start(Start), | |
22 | 20 Ttyconsole(TTyconsole), |
23 | 21 Vncdisplay(Vncdisplay), |
22 | 22 Dumpxml(Dumpxml), |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
23 DefineGDB(DefineGDB), |
22 | 24 Domiflist(Domiflist), |
25 Dominfo(Dominfo), | |
28 | 26 Templates(Templates), |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
27 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
28 |
28 | 29 /// show templates vm |
30 #[derive(Clap)] | |
31 struct Templates {} | |
32 | |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
33 /// define the domain in which the gdb port is opened from the template XML file |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
34 #[derive(Clap)] |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
35 struct DefineGDB { |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
36 name: String, |
5 | 37 } |
38 | |
7 | 39 /// define (but don't start) a domain from an template XML file |
5 | 40 #[derive(Clap)] |
41 struct Define { | |
42 name: String, | |
43 } | |
44 | |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
45 /// domain information in XML |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
46 #[derive(Clap)] |
22 | 47 struct Dumpxml { |
18 | 48 name: String, |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
49 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
50 |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
51 /// vncdisplay |
11 | 52 #[derive(Clap)] |
23 | 53 struct Vncdisplay { |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
54 name: String, |
11 | 55 } |
56 | |
10 | 57 /// undefine a domain |
58 #[derive(Clap)] | |
22 | 59 struct Undefine { |
10 | 60 name: String, |
61 } | |
62 | |
63 /// start a (previously defined) inactive domain | |
64 #[derive(Clap)] | |
65 struct Start { | |
66 name: String, | |
67 } | |
68 | |
69 /// tty console | |
70 #[derive(Clap)] | |
22 | 71 struct TTyconsole { |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
72 name: String, |
10 | 73 } |
74 | |
7 | 75 /// gracefully shutdown a domain |
5 | 76 #[derive(Clap)] |
77 struct Shutdown { | |
78 name: String, | |
79 } | |
80 | |
7 | 81 /// list domains |
82 #[derive(Clap)] | |
83 struct List {} | |
84 | |
10 | 85 /// destroy (stop) a domain |
86 #[derive(Clap)] | |
87 struct Destroy { | |
88 name: String, | |
89 } | |
90 | |
91 /// connect to the guest console | |
92 #[derive(Clap)] | |
93 struct Console { | |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
94 name: String, |
10 | 95 } |
96 | |
19 | 97 /// list all domain virtual interfaces |
98 #[derive(Clap)] | |
22 | 99 struct Domiflist { |
19 | 100 name: String, |
101 } | |
102 | |
103 /// domain information | |
104 #[derive(Clap)] | |
22 | 105 struct Dominfo { |
19 | 106 name: String, |
107 } | |
108 | |
0 | 109 fn main() { |
7 | 110 let opts: Opts = Opts::parse(); |
111 | |
12 | 112 let uid = user::getuid(); |
113 let gid = user::getgid(); | |
114 let user_name = user::getlogin(uid); | |
3 | 115 println!("uid: {} gid: {} name: {}", uid, gid, user_name); |
5 | 116 |
7 | 117 match opts.subcmd { |
10 | 118 SubCommand::List(_) => { |
12 | 119 user::set_root_id(); |
24 | 120 command::list(&user_name); |
5 | 121 } |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
122 |
13 | 123 SubCommand::Start(arg) => { |
124 user::set_root_id(); | |
36 | 125 command::start(&user_name, &arg.name); |
13 | 126 } |
18 | 127 |
20 | 128 SubCommand::Define(arg) => { |
129 user::set_root_id(); | |
26 | 130 let user = user::User { |
131 uid, | |
132 gid, | |
133 name: user_name, | |
24 | 134 }; |
36 | 135 command::define(&user, &arg.name); |
20 | 136 } |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
137 |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
138 SubCommand::Shutdown(arg) => { |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
139 user::set_root_id(); |
36 | 140 command::shutdown(&user_name, &arg.name); |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
141 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
142 |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
143 SubCommand::Console(arg) => { |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
144 user::set_root_id(); |
36 | 145 command::console(&user_name, &arg.name); |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
146 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
147 |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
148 SubCommand::Destroy(arg) => { |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
149 user::set_root_id(); |
36 | 150 command::destroy(&user_name, &arg.name); |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
151 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
152 |
23 | 153 SubCommand::Vncdisplay(arg) => { |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
154 user::set_root_id(); |
36 | 155 command::vncdisplay(&user_name, &arg.name); |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
156 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
157 |
18 | 158 SubCommand::Ttyconsole(arg) => { |
159 user::set_root_id(); | |
36 | 160 command::ttyconsole(&user_name, &arg.name); |
18 | 161 } |
162 | |
22 | 163 SubCommand::Dumpxml(arg) => { |
18 | 164 user::set_root_id(); |
36 | 165 command::dumpxml(&user_name, &arg.name); |
18 | 166 } |
167 | |
168 SubCommand::Undefine(arg) => { | |
169 user::set_root_id(); | |
36 | 170 command::undefine(&user_name, &arg.name); |
18 | 171 } |
172 | |
22 | 173 SubCommand::Domiflist(arg) => { |
19 | 174 user::set_root_id(); |
36 | 175 command::domiflist(&user_name, &arg.name); |
19 | 176 } |
177 | |
22 | 178 SubCommand::Dominfo(arg) => { |
19 | 179 user::set_root_id(); |
36 | 180 command::dominfo(&user_name, &arg.name); |
19 | 181 } |
182 | |
28 | 183 SubCommand::Templates(_) => { |
184 if let Err(e) = command::templates() { | |
185 println!("{}", e); | |
186 } | |
187 } | |
188 | |
10 | 189 _ => {} |
5 | 190 } |
19 | 191 } |