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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::collections::{HashMap, HashSet};

use linked_hash_map::LinkedHashMap;
use log::{trace, warn};
use crate::vendor::read_input::prelude::*;
use serde_json::Value;

use crate::config::{AnswerInfo, VariableInfo, VariableType};
use crate::template_engine::Context;
use crate::{Archetect, ArchetectError};

const ACCEPTABLE_BOOLEANS: [&str; 8] = ["y", "yes", "true", "t", "n", "no", "false", "f"];

pub fn populate_context(
    archetect: &Archetect,
    variables: &LinkedHashMap<String, VariableInfo>,
    answers: &LinkedHashMap<String, AnswerInfo>,
    context: &mut Context,
) -> Result<(), ArchetectError> {
    for (identifier, variable_info) in variables {
        if let Some(answer) = answers.get(identifier) {
            if let Some(value) = answer.value() {
                // If there is an answer for this variable, it has an explicit value, and it is an acceptable answer,
                // use that.
                if insert_answered_variable(archetect, identifier, value, &variable_info, context)? {
                    continue;
                }
            }
        } else {
            if let Some(value) = variable_info.value() {
                // If no answer was provided, there is an explicit value on the variable definition, and it is an
                // acceptable value, use that.
                if insert_answered_variable(archetect, identifier, value, &variable_info, context)? {
                    continue;
                }
            }
        }

        trace!("Attempting to satisfy {} ({:?})", identifier, variable_info);

        // If we've made it this far, there was not an acceptable answer or explicit value.  We need to prompt for a
        // valid value
        let mut prompt = if let Some(prompt) = variable_info.prompt() {
            format!("{} ", archetect.render_string(prompt.trim(), context)?)
        } else {
            format!("{}: ", identifier)
        };

        // Determine if a default can be provided.
        let default = if let Some(answer) = answers.get(identifier) {
            if let Some(default) = answer.default() {
                Some(archetect.render_string(default, context)?)
            } else if let Some(default) = variable_info.default() {
                Some(archetect.render_string(default, context)?)
            } else {
                None
            }
        } else if let Some(default) = variable_info.default() {
            Some(archetect.render_string(default, context)?)
        } else {
            None
        };

        let value = match variable_info.variable_type() {
            VariableType::Enum(values) => prompt_for_enum(&mut prompt, &values, &default),
            VariableType::Bool => prompt_for_bool(&mut prompt, &default),
            VariableType::Int => prompt_for_int(&mut prompt, &default),
            VariableType::Array => prompt_for_list(archetect, context, &mut prompt, &default, variable_info)?,
            VariableType::String => prompt_for_string(&mut prompt, &default, variable_info.required()),
        };

        if let Some(value) = value {
            context.insert(identifier, &value);
        }
    }

    Ok(())
}

fn insert_answered_variable(archetect: &Archetect, identifier: &str, value: &str, variable_info: &VariableInfo,
                            context: &mut Context) -> Result<bool, ArchetectError> {

    trace!("Setting variable answer {:?}={:?}", identifier, value);
    
    match variable_info.variable_type() {
        VariableType::Enum(options) => {
            // If the provided answer matches one of the enum values, use that; otherwise, we'll have to
            // prompt the user for a valid answer
            if options.contains(&value.to_owned()) {
                context.insert(identifier, &archetect.render_string(value, context)?);
                return Ok(true);
            }
        }
        VariableType::Bool => {
            let value = value.to_lowercase();
            // If the provided answer is anything that resembled a boolean value, use that; otherwise, we'll
            // have to prompt the user for a valid answer
            if ACCEPTABLE_BOOLEANS.contains(&value.as_str()) {
                let value = match ACCEPTABLE_BOOLEANS.iter().position(|i| i == &value.as_str()).unwrap() {
                    0..=3 => true,
                    _ => false,
                };
                context.insert(identifier, &value);
                return Ok(true);
            }
        }
        VariableType::Int => {
            // If the provided answer parses to an integer, use that; otherwise, we'll have to prompt the
            // user for a proper integer
            if let Ok(value) = &archetect.render_string(value, context)?.parse::<i64>() {
                context.insert(identifier, &value);
                return Ok(true);
            } else {
                trace!("'{}' failed to parse as an int", value);
            }
        }
        VariableType::String => {
            context.insert(identifier, &archetect.render_string(value, context)?);
            return Ok(true);
        }
        VariableType::Array => {
            let values = convert_to_list(archetect, context, value)?;
            if !values.is_empty() || !variable_info.required() {
                trace!("Inserting {}={:?}", identifier, values);
                context.insert_value(identifier, &Value::Array(values));
                return Ok(true);
            }
        }
    }

    warn!("'{:?}' is not a valid answer for {:?} with type {:?}", value, identifier, variable_info.variable_type());
    return Ok(false);
}

fn convert_to_list(archetect: &Archetect, context: &Context, value: &str) -> Result<Vec<Value>, ArchetectError> {
    let mut values = Vec::new();
    let splits: Vec<&str> = value.split(",")
        .map(|split| split.trim()).collect();
    for split in splits {
        if !split.is_empty() {
            values.push(Value::String(archetect.render_string(split, context)?))
        }
    }
    Ok(values)
}

fn prompt_for_string(prompt: &mut String, default: &Option<String>, required: bool) -> Option<Value> {
    if let Some(default) = &default {
        prompt.push_str(format!("[{}] ", default).as_str());
    };
    let mut input_builder = input::<String>().prompting_on_stderr().msg(&prompt);

    if required {
        input_builder = input_builder

            .add_test(|value| value.len() > 0)
            .repeat_msg(&prompt)
            .err("Please provide a value.");
    }

    let value = if let Some(default) = &default {
        input_builder.default(default.clone().to_owned()).get()
    } else {
        input_builder.get()
    };
    Some(Value::String(value))
}

fn prompt_for_int(prompt: &mut String, default: &Option<String>) -> Option<Value> {
    let default = default.as_ref().map_or(None, |value| value.parse::<i64>().ok());

    if let Some(default) = default {
        prompt.push_str(format!("[{}] ", default).as_str());
    }

    let input_builder = input::<i64>()
        .prompting_on_stderr()
        .msg(&prompt)
        .err("Please specify an integer.")
        .repeat_msg(&prompt);

    let value = if let Some(default) = default {
        input_builder.default(default).get()
    } else {
        input_builder.get()
    };

    Some(Value::from(value))
}

fn prompt_for_bool(prompt: &mut String, default: &Option<String>) -> Option<Value> {
    let default = default.as_ref().map_or(None, |value| {
        let value = value.to_lowercase();
        if ACCEPTABLE_BOOLEANS.contains(&value.as_str()) {
            Some(value.to_owned())
        } else {
            None
        }
    });

    if let Some(default) = default.clone() {
        prompt.push_str(format!("[{}] ", default).as_str());
    }

    let input_builder = input::<String>()
        .prompting_on_stderr()
        .add_test(|value| ACCEPTABLE_BOOLEANS.contains(&value.to_lowercase().as_str()))
        .msg(&prompt)
        .err(format!("Please specify a value of {:?}.", ACCEPTABLE_BOOLEANS))
        .repeat_msg(&prompt);

    let value = if let Some(default) = default.clone() {
        input_builder.default(default.to_owned()).get()
    } else {
        input_builder.get()
    };

    let value = match ACCEPTABLE_BOOLEANS.iter().position(|i| i == &value.as_str()).unwrap() {
        0..=3 => true,
        _ => false,
    };

    Some(Value::Bool(value))
}

fn prompt_for_list(
    archetect: &Archetect,
    context: &Context,
    prompt: &mut String,
    default: &Option<String>,
    variable_info: &VariableInfo,
) -> Result<Option<Value>, ArchetectError> {
    if let Some(default) = &default {
        prompt.push_str(format!("[{}] ", default).as_str());
    };
    
    eprintln!("{}", &prompt);

    let mut results = vec![];

    loop {
        let requirements_met = if let Some(default) = default {
            !default.trim().is_empty()
        } else {
            !results.is_empty()
        };

        let mut input_builder = input::<String>().prompting_on_stderr().msg(" - ");

        if variable_info.required() {
            input_builder = input_builder
                .add_test(move |value| requirements_met || !value.trim().is_empty())
                .err("This list requires at least one item.")
                .repeat_msg(" - ")
        }
        let item = input_builder.get();

        if item.trim().is_empty() {
            break;
        }

        results.push(Value::String(item));
    }

    if results.len() > 0 || !variable_info.required() {
        Ok(Some(Value::Array(results)))
    } else if let Some(default) = default {
        Ok(Some(Value::Array(convert_to_list(archetect, context, default)?)))
    } else {
        Ok(None)
    }

}

fn prompt_for_enum(prompt: &mut String, options: &Vec<String>, default: &Option<String>) -> Option<Value> {
    eprintln!("{}", &prompt);
    let choices = options
        .iter()
        .enumerate()
        .map(|(id, entry)| (id + 1, entry.clone()))
        .collect::<HashMap<_, _>>();

    for (id, option) in options.iter().enumerate() {
        eprintln!("{:>2}) {}", id + 1, option);
    }

    let mut message = String::from("Select and entry: ");
    if let Some(default) = default {
        if options.contains(default) {
            message.push_str(format!("[{}] ", default).as_str());
        }
    };

    let test_values = choices.keys().map(|v| *v).collect::<HashSet<_>>();

    let input_builder = input::<usize>()
        .prompting_on_stderr()
        .msg(&message)
        .add_test(move |value| test_values.contains(value))
        .err("Please enter the number of a selection from the list.")
        .repeat_msg(&message);

    let value = if let Some(default) = default {
        if let Some(index) = options.iter().position(|e| e.eq(default)) {
            input_builder.default(index + 1).get()
        } else {
            input_builder.get()
        }
    } else {
        input_builder.get()
    };

    Some(Value::String(choices.get(&value).unwrap().to_owned()))
}

pub fn render_answers(
    archetect: &Archetect,
    answers: &LinkedHashMap<String, AnswerInfo>,
    context: &Context,
) -> Result<LinkedHashMap<String, AnswerInfo>, ArchetectError> {
    let mut results = LinkedHashMap::new();
    for (identifier, answer_info) in answers {
        let mut result = AnswerInfo::new();
        if let Some(value) = answer_info.value() {
            result = result.with_value(archetect.render_string(value, context)?);
        }
        if let Some(prompt) = answer_info.prompt() {
            result = result.with_prompt(archetect.render_string(prompt, context)?);
        }
        if let Some(default) = answer_info.default() {
            result = result.with_default(archetect.render_string(default, context)?);
        }
        results.insert(identifier.to_owned(), result.build());
    }
    Ok(results)
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum VariableDescriptor {
    #[serde(rename = "object!")]
    Object {
        #[serde(skip_serializing_if = "Option::is_none")]
        prompt: Option<String>,
        //        #[serde(flatten)]
        items: LinkedHashMap<String, Box<VariableDescriptor>>,
    },

    #[serde(rename = "array!")]
    Array {
        prompt: String,
        //        #[serde(flatten)]
        item: Box<VariableDescriptor>,
    },

    #[serde(rename = "string!")]
    String {
        prompt: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        default: Option<String>,
    },

    #[serde(rename = "enum!")]
    Enum {
        prompt: String,
        options: Vec<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        default: Option<String>,
    },

    #[serde(rename = "bool!")]
    Bool {
        prompt: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        default: Option<String>,
    },

    #[serde(rename = "number!")]
    Number {
        prompt: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        default: Option<String>,
    },

    #[serde(rename = "json!")]
    Json { content: String, render: Option<bool> },
}

#[cfg(test)]
mod tests {
    use crate::actions::set::VariableDescriptor;
    use linked_hash_map::LinkedHashMap;

    #[test]
    fn test_serialize() {
        let object = VariableDescriptor::Object {
            prompt: Some("Schema:".to_string()),
            items: values_map(vec![(
                "tables",
                VariableDescriptor::Array {
                    prompt: "Tables: ".to_string(),
                    item: Box::new(VariableDescriptor::Object {
                        prompt: None,
                        items: values_map(vec![
                            (
                                "name",
                                VariableDescriptor::String {
                                    prompt: "Table Name: ".to_string(),
                                    default: None,
                                },
                            ),
                            (
                                "fields",
                                VariableDescriptor::Array {
                                    prompt: "Fields: ".to_string(),
                                    item: Box::new(VariableDescriptor::Object {
                                        prompt: None,
                                        items: values_map(vec![
                                            (
                                                "type",
                                                VariableDescriptor::Enum {
                                                    prompt: "Field Type: ".to_string(),
                                                    options: vec!["String".to_owned(), "Integer".to_owned()],
                                                    default: Some("String".to_owned()),
                                                },
                                            ),
                                            (
                                                "name",
                                                VariableDescriptor::String {
                                                    prompt: "Field Name: ".to_string(),
                                                    default: None,
                                                },
                                            ),
                                        ]),
                                    }),
                                },
                            ),
                        ]),
                    }),
                },
            )]),
        };

        let yaml = serde_yaml::to_string(&object).unwrap();
        println!("{}", yaml);
    }

    fn values_map<K: Into<String>, V>(values: Vec<(K, V)>) -> LinkedHashMap<String, Box<V>> {
        let mut results = LinkedHashMap::new();
        for (identifier, value) in values {
            results.insert(identifier.into(), Box::new(value));
        }
        results
    }
}