-
Notifications
You must be signed in to change notification settings - Fork 24
/
hoard_command.rs
235 lines (214 loc) · 7.15 KB
/
hoard_command.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
use serde::{Deserialize, Serialize};
use dialoguer::{theme::ColorfulTheme, Input};
pub trait Parsable {
fn parse_arguments(matches: &clap::ArgMatches) -> Self;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoardCommand {
pub name: String,
pub namespace: String,
pub tags: Option<Vec<String>>,
pub command: String,
pub description: Option<String>,
}
impl HoardCommand {
pub fn default() -> Self {
Self {
name: String::from(""),
namespace: String::from(""),
tags: None,
command: String::from(""),
description: None,
}
}
#[allow(dead_code)]
pub fn is_complete(&self) -> bool {
if self.name.is_empty()
|| self.namespace.is_empty()
|| self.tags.is_none()
|| self.command.is_empty()
|| self.description.is_none()
{
return false;
}
true
}
pub fn tags_as_string(&self) -> String {
self.tags
.as_ref()
.unwrap_or(&vec![String::from("")])
.join(",")
}
pub fn with_command_string_input(self, default_value: Option<String>) -> Self {
let command_string: String = Input::with_theme(&ColorfulTheme::default())
.default(default_value.unwrap_or(String::from("")))
.with_prompt("Command to hoard")
.interact_text()
.unwrap();
Self {
name: self.name,
namespace: self.namespace,
tags: self.tags,
command: command_string,
description: self.description,
}
}
pub fn with_tags_raw(self, tags: &str) -> Self {
Self {
name: self.name,
namespace: self.namespace,
tags: Some(
tags.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>()
.split(',')
.map(std::string::ToString::to_string)
.collect(),
),
command: self.command,
description: self.description,
}
}
pub fn with_tags_input(self, default_value: Option<String>) -> Self {
let tags: String = Input::with_theme(&ColorfulTheme::default())
.default(default_value.unwrap_or(String::from("")))
.with_prompt("Give your command tags ( comma separated )")
.validate_with({
move |input: &String| -> Result<(), &str> {
if input.contains(' ') {
Err("Tags cant contain whitespaces")
} else {
Ok(())
}
}
})
.interact_text()
.unwrap();
self.with_tags_raw(&tags)
}
pub fn with_namespace_input(self, default_namespace: Option<String>) -> Self {
let namespace = Input::with_theme(&ColorfulTheme::default())
.default(default_namespace.unwrap_or(String::from("")))
.with_prompt("Namespace of the command")
.interact_text()
.unwrap();
Self {
name: self.name,
namespace,
tags: self.tags,
command: self.command,
description: self.description,
}
}
pub fn with_name_input(self, default_value: Option<String>) -> Self {
let name: String = Input::with_theme(&ColorfulTheme::default())
.default(default_value.unwrap_or(String::from("")))
.with_prompt("Name your command")
.validate_with({
move |input: &String| -> Result<(), &str> {
if input.contains(' ') {
Err("The name cant contain whitespaces")
} else {
Ok(())
}
}
})
.interact_text()
.unwrap();
Self {
name,
namespace: self.namespace,
tags: self.tags,
command: self.command,
description: self.description,
}
}
pub fn with_description_input(self, default_value: Option<String>) -> Self {
let description_string: String = Input::with_theme(&ColorfulTheme::default())
.default(default_value.unwrap_or(String::from("")))
.with_prompt("Describe what the command does")
.interact_text()
.unwrap();
Self {
name: self.name,
namespace: self.namespace,
tags: self.tags,
command: self.command,
description: Some(description_string),
}
}
}
impl Parsable for HoardCommand {
fn parse_arguments(matches: &clap::ArgMatches) -> Self {
let mut new_command = Self::default();
if let Some(n) = matches.value_of("name") {
// TODO: some name validation for when we have it
new_command.name = n.to_string();
}
// Defaults to 'default' namespace
if let Some(ns) = matches.value_of("namespace") {
// TODO: some validation for when we have it
new_command.namespace = ns.to_string();
}
// "$ hoard test -t" was run
// Expects comma seperated tags
if let Some(tags) = matches.value_of("tags") {
new_command.tags = Some(
tags.split(',')
.map(std::string::ToString::to_string)
.collect(),
);
}
if let Some(c) = matches.value_of("command") {
// TODO: some validation for when we have it
new_command.command = c.to_string();
}
new_command
}
}
#[cfg(test)]
mod test_commands {
use super::*;
#[test]
fn one_tag_as_string() {
let command = HoardCommand::default().with_tags_raw("foo");
let expected = "foo";
assert_eq!(expected, command.tags_as_string());
}
#[test]
fn no_tag_as_string() {
let command = HoardCommand::default();
let expected = "";
assert_eq!(expected, command.tags_as_string());
}
#[test]
fn multiple_tags_as_string() {
let command = HoardCommand::default().with_tags_raw("foo,bar");
let expected = "foo,bar";
assert_eq!(expected, command.tags_as_string());
}
#[test]
fn parse_single_tag() {
let command = HoardCommand::default().with_tags_raw("foo");
let expected = Some(vec![String::from("foo")]);
assert_eq!(expected, command.tags);
}
#[test]
fn parse_no_tag() {
let command = HoardCommand::default();
let expected = None;
assert_eq!(expected, command.tags);
}
#[test]
fn parse_multiple_tags() {
let command = HoardCommand::default().with_tags_raw("foo,bar");
let expected = Some(vec![String::from("foo"), String::from("bar")]);
assert_eq!(expected, command.tags);
}
#[test]
fn parse_whitespace_in_tags() {
let command = HoardCommand::default().with_tags_raw("foo, bar");
let expected = Some(vec![String::from("foo"), String::from("bar")]);
assert_eq!(expected, command.tags);
}
}