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
use crate::cli::label::create::CreateLabelArgs;
use crate::client::BergClient;
use crate::endpoints::endpoint_generator::EndpointGenerator;
use crate::render::ui::multi_fuzzy_select_with_key;
use crate::types::api::create_options::create_label_options::CreateLabelOption;
use crate::types::api::label::Label;
use inquire::validator::Validation;
use strum::Display;
use crate::actions::text_manipulation::{edit_prompt_for, input_prompt_for};
pub async fn create_label(args: CreateLabelArgs, client: &BergClient) -> anyhow::Result<()> {
let options = fill_in_mandatory_values(&args)?;
let options = fill_in_optional_values(options, args)?;
let api_endpoint = EndpointGenerator::repo_labels()?;
let label: Label = client.post_body(api_endpoint, options).await?;
println!("Successfully created label: {}", label.name);
Ok(())
}
fn fill_in_mandatory_values(args: &CreateLabelArgs) -> anyhow::Result<CreateLabelOption> {
let name = match args.name.clone() {
Some(name) => name,
None => inquire::Text::new(input_prompt_for("Label Title").as_str()).prompt()?,
};
Ok(CreateLabelOption::new(name))
}
fn fill_in_optional_values(
mut options: CreateLabelOption,
args: CreateLabelArgs,
) -> anyhow::Result<CreateLabelOption> {
options = options
.with_color(args.color.clone().unwrap_or_default())
.with_description(args.description.clone().unwrap_or_default());
#[derive(Debug, Clone, Copy, Display, PartialEq, Eq)]
enum PossiblyMissing {
Description,
Color,
}
use PossiblyMissing::*;
let missing_options = [
args.color.is_none().then_some(Color),
args.description.is_none().then_some(Description),
]
.into_iter()
.flatten()
.collect::<Vec<_>>();
if missing_options.is_empty() {
return Ok(options);
}
let selected_options =
multi_fuzzy_select_with_key(missing_options, "Add additional information for", |_| false)?;
if selected_options.contains(&Description) {
let new_description = inquire::Editor::new(edit_prompt_for("a label description").as_str())
.with_predefined_text("Enter a label description")
.prompt()?;
options = options.with_description(new_description);
}
if selected_options.contains(&Color) {
let new_color = inquire::Text::new(input_prompt_for("Enter a color").as_str())
.with_help_message("(format: #xxxxxx)")
.with_validator(|color: &str| {
Ok((color.len() == 7
&& color.starts_with('#')
&& color
.chars()
.skip(1)
.take(6)
.filter(|digit| digit.is_ascii_hexdigit())
.count()
== 6)
.then_some(Validation::Valid)
.unwrap_or_else(|| Validation::Invalid("Not a color: format <#XXXXXX>".into())))
})
.prompt()?;
options = options.with_color(new_color);
}
Ok(options)
}