-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.rs
More file actions
258 lines (242 loc) · 8.96 KB
/
Copy pathargs.rs
File metadata and controls
258 lines (242 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use {
crate::error::Error,
anyhow::Result,
clap::{
Arg,
ArgAction,
},
std::{
fs,
str::FromStr,
},
};
#[derive(Debug, Eq, PartialEq)]
pub enum Privilege {
Normal,
Experimental,
}
#[derive(Debug)]
pub struct CallArgs {
pub privileges: Privilege,
pub command: Command,
}
impl CallArgs {
pub fn validate(&self) -> Result<()> {
if self.privileges == Privilege::Experimental {
return Ok(());
}
match &self.command {
| Command::Info { .. } => Err(Error::Experimental("info".to_owned()).into()),
| Command::Edit { .. } => Err(Error::Experimental("edit".to_owned()).into()),
| _ => Ok(()),
}
}
}
#[derive(Debug)]
pub enum ManualFormat {
Manpages,
Markdown,
}
#[derive(Debug)]
pub enum Command {
Manual { path: String, format: ManualFormat },
Autocomplete { path: String, shell: clap_complete::Shell },
Split(SplitCommand),
Restore(RestoreCommand),
Info { share: (String, Vec<u8>) },
Edit { share: (String, Vec<u8>) },
}
#[derive(Debug)]
pub enum SplitCommand {
Auto {
secret_data: Vec<u8>,
blueprint: Vec<u8>,
trust: bool,
},
Interactive {
secret_data: Vec<u8>,
},
}
#[derive(Debug)]
pub enum RestoreCommand {
Auto { shares: Vec<(String, Vec<u8>)> },
Interactive { shares: Vec<(String, Vec<u8>)> },
}
pub struct ClapArgumentLoader {}
impl ClapArgumentLoader {
pub fn root_command() -> clap::Command {
clap::Command::new("agree")
.version(env!("CARGO_PKG_VERSION"))
.about("A multi-key-turn encryption/decryption CLI implementing shamirs secret sharing.")
.author("Alexander Weber <alexanderh.weber@outlook.com>")
.propagate_version(true)
.subcommand_required(true)
.args([Arg::new("experimental")
.short('e')
.long("experimental")
.help("Enables experimental features.")
.num_args(0)])
.subcommand(
clap::Command::new("man")
.about("Renders the manual.")
.arg(clap::Arg::new("out").short('o').long("out").required(true))
.arg(
clap::Arg::new("format")
.short('f')
.long("format")
.value_parser(["manpages", "markdown"])
.required(true),
),
)
.subcommand(
clap::Command::new("autocomplete")
.about("Renders shell completion scripts.")
.arg(clap::Arg::new("out").short('o').long("out").required(true))
.arg(
clap::Arg::new("shell")
.short('s')
.long("shell")
.value_parser(["bash", "zsh", "fish", "elvish", "powershell"])
.required(true),
),
)
.subcommand(
clap::Command::new("split")
.about("Split a secret.")
.arg(
clap::Arg::new("interactive")
.long("interactive")
.short('i')
.help("Interactive mode.")
.num_args(0)
.conflicts_with_all(["blueprint"]),
)
.arg(
clap::Arg::new("secret")
.long("secret")
.short('s')
.help("Path to the file containing the secret.")
.required(true),
)
.arg(
clap::Arg::new("blueprint")
.long("blueprint")
.short('b')
.help("Path to the blueprint file.")
.required(true),
)
.arg(
clap::Arg::new("trust")
.long("trust")
.short('t')
.help("Allow shell invocations from blueprint scripts.")
.num_args(0),
),
)
.subcommand(
clap::Command::new("restore")
.about("Restores a secret from shares.")
.arg(
clap::Arg::new("share")
.long("share")
.short('s')
.help("Path to a share file.")
.required(true)
.action(ArgAction::Append),
)
.arg(
clap::Arg::new("interactive")
.long("interactive")
.short('i')
.help("Interactive mode.")
.num_args(0),
),
)
.subcommand(
clap::Command::new("info")
.about("Display information about a share.")
.arg(
clap::Arg::new("share")
.long("share")
.short('s')
.help("Path to a share file.")
.required(true)
.action(ArgAction::Append),
),
)
.subcommand(
clap::Command::new("edit").about("Edit the share.").arg(
clap::Arg::new("share")
.long("share")
.short('s')
.help("Path to a share file.")
.required(true)
.action(ArgAction::Append),
),
)
}
pub fn load() -> Result<CallArgs> {
let command = Self::root_command().get_matches();
let privileges = if command.get_flag("experimental") {
Privilege::Experimental
} else {
Privilege::Normal
};
let cmd = if let Some(subc) = command.subcommand_matches("man") {
Command::Manual {
path: subc.get_one::<String>("out").unwrap().into(),
format: match subc.get_one::<String>("format").unwrap().as_str() {
| "manpages" => ManualFormat::Manpages,
| "markdown" => ManualFormat::Markdown,
| _ => return Err(Error::Argument("unknown format".into()).into()),
},
}
} else if let Some(subc) = command.subcommand_matches("autocomplete") {
Command::Autocomplete {
path: subc.get_one::<String>("out").unwrap().into(),
shell: clap_complete::Shell::from_str(subc.get_one::<String>("shell").unwrap().as_str()).unwrap(),
}
} else if let Some(subc) = command.subcommand_matches("split") {
if subc.get_flag("interactive") {
Command::Split(SplitCommand::Interactive {
secret_data: fs::read(subc.get_one::<String>("secret").unwrap())?,
})
} else {
Command::Split(SplitCommand::Auto {
secret_data: fs::read(subc.get_one::<String>("secret").unwrap())?,
blueprint: fs::read(subc.get_one::<String>("blueprint").unwrap())?,
trust: subc.get_flag("trust"),
})
}
} else if let Some(subc) = command.subcommand_matches("restore") {
let shares_args = subc.get_many::<String>("share").unwrap();
let mut shares = Vec::<(String, Vec<u8>)>::new();
for s in shares_args {
shares.push((s.to_owned(), fs::read(s)?));
}
if subc.get_flag("interactive") {
Command::Restore(RestoreCommand::Interactive { shares })
} else {
Command::Restore(RestoreCommand::Auto { shares })
}
} else if let Some(subc) = command.subcommand_matches("info") {
let shares_args = subc.get_one::<String>("share").unwrap();
Command::Info {
share: (shares_args.to_owned(), fs::read(shares_args)?),
}
} else if let Some(subc) = command.subcommand_matches("edit") {
let shares_args = subc.get_one::<String>("share").unwrap();
Command::Edit {
share: (shares_args.to_owned(), fs::read(shares_args)?),
}
} else {
return Err(Error::UnknownCommand.into());
};
let callargs = CallArgs {
privileges,
command: cmd,
};
callargs.validate()?;
Ok(callargs)
}
}