-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rs
371 lines (315 loc) · 12.8 KB
/
config.rs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use crate::{error::Error, Result};
use std::{mem, path::PathBuf, vec};
use inquire::{validator::Validation, Confirm, CustomUserError, InquireError, Select, Text};
use serde::{Deserialize, Serialize};
use tokio::fs;
const CONFIG_PATH: &str = "filecrab/config.toml";
/// Represents the CLI config.
#[derive(Deserialize, Serialize, Default, Clone)]
pub(super) struct Config {
pub(super) active: Instance,
pub(super) others: Option<Vec<Instance>>,
}
/// Represents a filecrab instance.
#[derive(Deserialize, Serialize, Default, Clone)]
pub(super) struct Instance {
pub(super) name: String,
pub(super) url: String,
pub(super) api_key: String,
}
/// Gives the original command and a pathbuf, used to create filecrab configs.
pub enum CommandAndPath<'a> {
Init(&'a PathBuf),
Other(&'a PathBuf),
}
impl Config {
/// Returns the active instance
pub(super) fn get_active_instance(&self) -> &Instance {
&self.active
}
/// Loads the config.
pub(super) async fn load_config() -> Result<Config> {
// Builds the path to the config file.
let config_path = match dirs::config_dir() {
Some(config_dir) => config_dir.join(CONFIG_PATH),
None => return Err(Error::ConfigNotFound),
};
// Prompts the user to set the config if it does not exist.
if !config_path.exists() {
Config::prompt_new_config(CommandAndPath::Other(&config_path)).await?;
}
// Deserializes the config.
let config = toml::from_str(&fs::read_to_string(&config_path).await.map_err(|err| {
Error::ReadFile {
path: format!("{}", config_path.display()),
source: err,
}
})?)
.map_err(Error::ParseToml)?;
Ok(config)
}
/// Prompts the user to set the initial config and saves it.
async fn prompt_new_config(command_and_path: CommandAndPath<'_>) -> Result<()> {
let path = match command_and_path {
CommandAndPath::Init(path) => {
println!("Initializing config:");
path
}
CommandAndPath::Other(path) => {
println!("The config file is not set, we're going to create it:");
path
}
};
// Get the new instance
let instance = Config::prompt_instance_input(None)?;
// Build new config struct
let config = Config {
active: instance,
others: None,
};
Config::write_config(path, &config).await?;
// Prints the completion message.
println!();
println!("Thanks, your file has been written in {path:?}. You can modify it manually.");
println!("Enjoy pinching files and text! BLAZINGLY FAST!");
println!();
Ok(())
}
/// Writes the config to the given path
async fn write_config(path: &PathBuf, config: &Config) -> Result {
// Builds the config and writes it to the file.
let parent = match path.parent() {
Some(parent) => parent,
None => return Err(Error::NoParentDir),
};
// Create dir all if needed
fs::create_dir_all(parent)
.await
.map_err(Error::CreateConfigDir)?;
fs::write(
path,
&toml::to_string(config).map_err(Error::SerializeToml)?,
)
.await
.map_err(|err| Error::WriteFile {
path: format!("{}", path.display()),
source: err,
})
}
/// Prompts the user to get the input of a new instance
///
/// INFO: As of `now prompt_instance_input` needs it's own version of config (if passed) due to the
/// 'static lifetime of the with_validator in inquire. Maybe one day this will be fixed in the
/// lib, an issue has been opened.
fn prompt_instance_input(config: Option<Config>) -> Result<Instance> {
let instance_name = Text::new("What's the filecrab instance's name?")
.with_validator(
move |val: &str| -> std::result::Result<Validation, CustomUserError> {
// So we make sure we don't consume the config here, consuming the config
// appears to fail the 'static annotation of the closure
if let Some(conf) = &config {
// If either the active config or the others already have the given name,
// refuse the validation
if conf.active.name == val
|| conf
.others
.as_ref()
.is_some_and(|others| others.iter().any(|i| i.name == val))
{
return Ok(Validation::Invalid(
"This instance name is already in use.".into(),
));
}
}
Ok(Validation::Valid)
},
)
.prompt()
.map_err(|err| match err {
InquireError::OperationCanceled | InquireError::OperationInterrupted => {
Error::UserCancel
}
_ => err.into(),
})?
.to_string();
// Ask the user for the url
let url = Text::new("Enter the complete URL of your filecrab:")
.with_initial_value("https://")
.with_validator(|val: &str| {
if !val.starts_with("http://") && !val.starts_with("https://") {
return Ok(Validation::Invalid(
"The given url is missing the `http(s)://` prefix.".into(),
));
}
Ok(Validation::Valid)
})
.with_help_message(
"The `http(s)` prefix is mandatory! You should also set the port if needed.",
)
.prompt()
.map_err(|err| match err {
InquireError::OperationCanceled | InquireError::OperationInterrupted => {
Error::UserCancel
}
_ => err.into(),
})?;
let url = url.trim().to_string();
// Reads the API key from the stdin.
let api_key = Text::new("Enter the API key:").prompt()?.trim().to_string();
Ok(Instance {
name: instance_name,
url,
api_key,
})
}
pub(super) async fn switch_instance(&mut self) -> Result {
let others = self.others.as_mut().ok_or(Error::NoOtherInstances)?;
// Collect all the names
let names = others.iter().map(|i| i.name.as_str()).collect();
// Ask the user which instance to activate
let new_name = Select::new("Which Filecrab instance do you want to activate?", names)
.prompt()
.map_err(|err| match err {
InquireError::OperationCanceled | InquireError::OperationInterrupted => {
Error::UserCancel
}
_ => err.into(),
})?
.to_string();
// Find the instance to switch to
if let Some(new) = others.iter_mut().find(|i| i.name == new_name) {
mem::swap(&mut self.active, new);
}
// Create the path
let path = match dirs::config_dir() {
Some(config_dir) => config_dir.join(CONFIG_PATH),
None => return Err(Error::ConfigNotFound),
};
// Write the config
Config::write_config(&path, self).await?;
println!("Successfully switched to `{new_name}`.");
Ok(())
}
pub(super) async fn add(&mut self) -> Result {
// Prompt the user and get the new instance
println!("Adding a new instance:");
let new_instance = Config::prompt_instance_input(Some(self.clone()))?;
let new_name = new_instance.name.clone();
// Push the instance to the others
match self.others.as_mut() {
Some(others) => {
// If one already exists
if others.iter().any(|i| i.name == new_name) {
return Err(Error::DuplicateInstanceName(new_name.clone()));
}
others.push(new_instance)
}
None => self.others = Some(vec![new_instance]),
}
// Prompt the user if he want's to switch it as active
let ans = Confirm::new("Do you want to set it as the active instance?")
.with_default(false)
.prompt()?;
// If yes, switch it
if ans {
// We unwrap as this should never fail because it has just been added
let others = self.others.as_mut().unwrap();
// Find the instance to switch to
if let Some(new) = others.iter_mut().find(|i| i.name == new_name) {
mem::swap(&mut self.active, new);
}
}
// Create the path
let path = match dirs::config_dir() {
Some(config_dir) => config_dir.join(CONFIG_PATH),
None => return Err(Error::ConfigNotFound),
};
// Write the config
Config::write_config(&path, self).await?;
if ans {
println!("Successfully added `{new_name}` and switched it as active.")
} else {
println!("Successfully added `{new_name}`.")
}
Ok(())
}
pub(super) async fn remove(&mut self) -> Result {
// Build a list to select from
let mut names = vec![self.active.name.clone()];
// If there are other instances push also it's names
if let Some(others) = self.others.as_ref() {
others.iter().for_each(|i| names.push(i.name.clone()))
}
// Ask the user which instance to remove
let name_to_remove = Select::new("Which Filecrab instance do you want to remove?", names)
.prompt()
.map_err(|err| match err {
InquireError::OperationCanceled | InquireError::OperationInterrupted => {
Error::UserCancel
}
_ => err.into(),
})?
.to_string();
// Make sure the user want's to remove it
if !Confirm::new("Are you sure?").with_default(false).with_help_message("Removing the active instance can have 2 consequences, if there are no more instances the config file is deleted. Otherwise the first instance in `others` is swapped as active.").prompt()? {
println!("Exited without removing any instance.");
return Ok(());
}
// If the one being deleted is the active one, either swap it with another or remove the
// config file
if name_to_remove == self.active.name.as_ref() {
match self.others.as_mut() {
// Remove the first of the others and place it as active
Some(others) => {
let mut new_active = others.remove(0);
mem::swap(&mut self.active, &mut new_active);
}
None => self.delete_config_file().await?,
}
} else {
// Unwrapping is safe as if the selected name is not the active it for sure exists in
// the others
self.others
.as_mut()
.unwrap()
.retain(|i| i.name != name_to_remove);
}
// If there are no remaining instances, set the others as None
if self.others.as_ref().is_some_and(|i| i.is_empty()) {
self.others = None;
}
// Create the path
let path = match dirs::config_dir() {
Some(config_dir) => config_dir.join(CONFIG_PATH),
None => return Err(Error::ConfigNotFound),
};
// Write the config
Config::write_config(&path, self).await?;
Ok(())
}
pub(super) async fn delete_config_file(&self) -> Result {
// Create the path
let path = match dirs::config_dir() {
Some(config_dir) => config_dir.join(CONFIG_PATH),
None => return Err(Error::ConfigNotFound),
};
fs::remove_file(path).await.map_err(Error::RemoveConfig)?;
Ok(())
}
pub(super) async fn init(&self) -> Result {
let path = match dirs::config_dir() {
Some(config_dir) => config_dir.join(CONFIG_PATH),
None => return Err(Error::ConfigNotFound),
};
let exists = fs::try_exists(&path).await;
// if exists is ok then return early
if exists.as_ref().is_ok_and(|val| *val) {
return Err(Error::ConfigExists);
} else if exists.is_err() {
exists.map_err(Error::FindConfig)?;
}
// If it doesn't exists prompt the user
Config::prompt_new_config(CommandAndPath::Init(&path)).await?;
Ok(())
}
}