greentic-setup 0.5.9

End-to-end bundle setup engine for the Greentic platform — pack discovery, QA-driven configuration, secrets persistence, and bundle lifecycle management
Documentation
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
//! Interactive CLI prompts for QA setup wizard.
//!
//! Handles user input collection, validation, and formatting for FormSpec questions.

use std::io::{self, Write as _};

use anyhow::{Result, anyhow};
use qa_spec::spec::question::ListSpec;
use qa_spec::{FormSpec, QuestionSpec, QuestionType, VisibilityMode, resolve_visibility};
use rpassword::prompt_password;
use serde_json::{Map as JsonMap, Value};

use crate::qa::bridge;
use crate::setup_to_formspec;

/// Interactively prompt the user using FormSpec questions.
///
/// Evaluates `visible_if` expressions after each answer so that conditional
/// questions are shown/hidden dynamically as answers are collected.
pub fn prompt_form_spec_answers(
    spec: &FormSpec,
    provider_id: &str,
    advanced: bool,
) -> Result<Value> {
    prompt_form_spec_answers_with_existing(
        spec,
        provider_id,
        advanced,
        &Value::Object(JsonMap::new()),
    )
}

/// Prompt for FormSpec answers with pre-existing initial values.
///
/// Only prompts for questions that don't already have satisfactory answers.
pub fn prompt_form_spec_answers_with_existing(
    spec: &FormSpec,
    provider_id: &str,
    advanced: bool,
    initial_answers: &Value,
) -> Result<Value> {
    let display = setup_to_formspec::strip_domain_prefix(provider_id);
    let mode_label = if advanced { " (advanced)" } else { "" };
    println!("\nConfiguring {display}: {}{mode_label}", spec.title);
    if let Some(ref pres) = spec.presentation
        && let Some(ref intro) = pres.intro
    {
        println!("{intro}");
    }

    let mut answers = initial_answers.as_object().cloned().unwrap_or_default();
    for question in &spec.questions {
        if question.id.is_empty() {
            continue;
        }
        // Re-evaluate visibility with answers collected so far.
        if question.visible_if.is_some() {
            let current = Value::Object(answers.clone());
            let vis = resolve_visibility(spec, &current, VisibilityMode::Visible);
            if !vis.get(&question.id).copied().unwrap_or(true) {
                continue;
            }
        }
        let existing = answers.get(&question.id);
        if existing
            .filter(|value| answer_satisfies_question(question, value))
            .is_some()
        {
            continue;
        }
        // In normal mode, skip optional missing questions — except `List`
        // (table) kinds. A table is a structural hand-off to the operator
        // ("here's where nav-links / repeating data go") that doesn't make
        // sense to silently hide based on the required flag, even when
        // optional. They get the table; if they want to skip rows they
        // answer "n" to "Add a row?" and move on.
        if !advanced && !question.required && question.kind != QuestionType::List {
            continue;
        }
        if let Some(value) = ask_form_spec_question(question)? {
            answers.insert(question.id.clone(), value);
        }
    }
    Ok(Value::Object(answers))
}

/// Check if an answer satisfies a question's requirements.
pub fn answer_satisfies_question(question: &QuestionSpec, value: &Value) -> bool {
    if value.is_null() {
        return false;
    }

    // Empty or blank string is not satisfactory for any question
    if let Some(s) = value.as_str()
        && s.trim().is_empty()
    {
        return false;
    }

    // Check for environment variable placeholder (e.g., "${PUBLIC_BASE_URL}")
    // These are considered valid values that will be resolved at runtime
    if let Some(s) = value.as_str()
        && s.starts_with("${")
        && s.ends_with('}')
    {
        return true;
    }

    if let Some(ref choices) = question.choices
        && !choices.is_empty()
    {
        let Some(candidate) = value.as_str() else {
            return false;
        };
        if !choices.iter().any(|choice| choice == candidate) {
            return false;
        }
    }
    if let Some(ref constraint) = question.constraint
        && let Some(ref pattern) = constraint.pattern
        && let Some(candidate) = value.as_str()
        && !matches_pattern(candidate, pattern)
    {
        return false;
    }
    true
}

/// Prompt for a single FormSpec question and return the answer.
pub fn ask_form_spec_question(question: &QuestionSpec) -> Result<Option<Value>> {
    // Table / repeating-row questions (kind: List) get a dedicated row loop
    // — see `ask_list_question` for the prompt protocol. Falls through to
    // the scalar path if the question is List-typed but missing its
    // `list` schema (defensive: shouldn't happen with a well-formed spec).
    if question.kind == QuestionType::List
        && let Some(ref list) = question.list
    {
        return ask_list_question(question, list);
    }

    // Print question header
    let marker = if question.required {
        " (required)"
    } else {
        " (optional)"
    };
    println!();
    println!("  {}{marker}", question.title);

    // Print description as contextual help
    if let Some(ref desc) = question.description
        && !desc.is_empty()
    {
        println!("  {desc}");
    }

    if let Some(ref choices) = question.choices {
        println!();
        for (idx, choice) in choices.iter().enumerate() {
            println!("    {}) {choice}", idx + 1);
        }
    }

    loop {
        let prompt = build_form_spec_prompt(question);
        let input = read_input(&prompt, question.secret)?;
        let trimmed = input.trim();

        if trimmed.is_empty() {
            if let Some(ref default) = question.default_value {
                return Ok(Some(parse_typed_value(question.kind, default)));
            }
            if question.required {
                println!("  This field is required.");
                continue;
            }
            return Ok(None);
        }

        let normalized = bridge::normalize_answer(trimmed, question.kind);

        if let Some(ref constraint) = question.constraint
            && let Some(ref pattern) = constraint.pattern
            && !matches_pattern(&normalized, pattern)
        {
            println!("  Invalid format. Expected pattern: {pattern}");
            continue;
        }

        if let Some(ref choices) = question.choices
            && !choices.is_empty()
        {
            if let Ok(idx) = normalized.parse::<usize>()
                && let Some(choice) = choices.get(idx - 1)
            {
                return Ok(Some(Value::String(choice.clone())));
            }
            if !choices.contains(&normalized) {
                println!("  Invalid choice. Options: {}", choices.join(", "));
                continue;
            }
        }

        return Ok(Some(parse_typed_value(question.kind, &normalized)));
    }
}

/// Prompt for a `QuestionType::List` (repeating-row) question. Loops
/// "Add another?" / row-by-row prompts and returns a `Value::Array` of
/// per-row JSON objects whose keys match the column field IDs.
///
/// Constraints:
/// - `list.min_items` / `max_items` enforce row count bounds.
/// - The outer question's `required` flag is honoured: when required and
///   no rows were collected, we re-prompt instead of returning `None`.
/// - Rows whose required columns are all empty are dropped silently
///   (lets the operator type a blank line and "step out" mid-table).
fn ask_list_question(question: &QuestionSpec, list: &ListSpec) -> Result<Option<Value>> {
    let marker = if question.required {
        " (required)"
    } else {
        " (optional)"
    };
    println!();
    println!("  {}{marker}", question.title);
    if let Some(ref desc) = question.description
        && !desc.is_empty()
    {
        println!("  {desc}");
    }

    let max = list.max_items;
    let min = list.min_items.unwrap_or(0);

    let mut rows: Vec<Value> = Vec::new();
    loop {
        if let Some(cap) = max
            && rows.len() >= cap
        {
            println!("  (max {} rows reached)", cap);
            break;
        }

        // Ask whether to add another row.
        let prompt = if rows.is_empty() {
            "  > Add a row? [y/N] "
        } else {
            "  > Add another row? [y/N] "
        };
        let input = read_input(prompt, false)?;
        let trimmed = input.trim().to_ascii_lowercase();
        let yes = matches!(trimmed.as_str(), "y" | "yes" | "1" | "true");
        if !yes {
            if rows.len() < min {
                println!(
                    "  At least {min} row(s) required — got {}. Type 'y' to add another.",
                    rows.len()
                );
                continue;
            }
            break;
        }

        // Prompt each column for the new row.
        println!("  Row #{}:", rows.len() + 1);
        let mut row_obj = JsonMap::new();
        for column in &list.fields {
            if let Some(value) = ask_form_spec_question(column)? {
                row_obj.insert(column.id.clone(), value);
            }
        }

        // Drop the row if every required column ended up empty — lets the
        // operator back out by hitting Enter through every column.
        let row_has_required_content = list.fields.iter().all(|c| {
            !c.required
                || row_obj
                    .get(&c.id)
                    .map(|v| !is_empty_answer(v))
                    .unwrap_or(false)
        });
        if !row_has_required_content {
            println!("  (row dropped — required columns were empty)");
            continue;
        }

        rows.push(Value::Object(row_obj));
    }

    if rows.is_empty() {
        if question.required {
            println!("  This field is required — at least one row needed.");
            return Ok(None);
        }
        return Ok(None);
    }

    Ok(Some(Value::Array(rows)))
}

/// Treat an empty string, false bool, or null as "no answer" for the
/// row-required check.
fn is_empty_answer(v: &Value) -> bool {
    match v {
        Value::Null => true,
        Value::String(s) => s.trim().is_empty(),
        Value::Array(a) => a.is_empty(),
        Value::Object(o) => o.is_empty(),
        _ => false,
    }
}

/// Build the prompt string for a FormSpec question.
fn build_form_spec_prompt(question: &QuestionSpec) -> String {
    let mut prompt = String::from("  > ");
    match question.kind {
        QuestionType::Boolean => prompt.push_str("[yes/no] "),
        QuestionType::Number | QuestionType::Integer => prompt.push_str("[number] "),
        QuestionType::Enum => prompt.push_str("[choice] "),
        _ => {}
    }
    if let Some(ref default) = question.default_value
        && !default.is_empty()
    {
        prompt.push_str(&format!("(default: {default}) "));
    }
    prompt
}

/// Read input from user, optionally masking for secrets.
fn read_input(prompt: &str, secret: bool) -> Result<String> {
    if secret {
        prompt_password(prompt).map_err(|err| anyhow!("read secret: {err}"))
    } else {
        print!("{prompt}");
        io::stdout().flush()?;
        let mut buffer = String::new();
        io::stdin().read_line(&mut buffer)?;
        Ok(buffer)
    }
}

/// Simple pattern matching for common constraint patterns.
///
/// Supports the URL pattern `^https?://\S+` used by setup specs.
pub fn matches_pattern(value: &str, pattern: &str) -> bool {
    if pattern == r"^https?://\S+" {
        (value.starts_with("http://") || value.starts_with("https://"))
            && value.len() > 8
            && !value.contains(char::is_whitespace)
    } else {
        // Unknown pattern — accept (validation is best-effort).
        true
    }
}

/// Parse a string input into the appropriate JSON value type.
pub fn parse_typed_value(kind: QuestionType, input: &str) -> Value {
    match kind {
        QuestionType::Boolean => match input.to_ascii_lowercase().as_str() {
            "true" | "yes" | "y" | "1" | "on" => Value::Bool(true),
            "false" | "no" | "n" | "0" | "off" => Value::Bool(false),
            _ => Value::String(input.to_string()),
        },
        QuestionType::Number | QuestionType::Integer => {
            if let Ok(n) = input.parse::<i64>() {
                Value::Number(n.into())
            } else if let Ok(n) = input.parse::<f64>() {
                serde_json::Number::from_f64(n)
                    .map(Value::Number)
                    .unwrap_or_else(|| Value::String(input.to_string()))
            } else {
                Value::String(input.to_string())
            }
        }
        _ => Value::String(input.to_string()),
    }
}

/// Check if a FormSpec has any required questions.
pub fn has_required_questions(spec: Option<&FormSpec>) -> bool {
    spec.map(|s| s.questions.iter().any(|q| q.required))
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn parse_typed_values() {
        assert_eq!(
            parse_typed_value(QuestionType::Boolean, "true"),
            Value::Bool(true)
        );
        assert_eq!(
            parse_typed_value(QuestionType::Boolean, "no"),
            Value::Bool(false)
        );
        assert_eq!(parse_typed_value(QuestionType::Number, "42"), json!(42));
        assert_eq!(
            parse_typed_value(QuestionType::String, "hello"),
            Value::String("hello".into())
        );
    }

    #[test]
    fn matches_url_pattern() {
        assert!(matches_pattern("https://example.com", r"^https?://\S+"));
        assert!(matches_pattern("http://localhost:8080", r"^https?://\S+"));
        assert!(!matches_pattern("not-a-url", r"^https?://\S+"));
        assert!(!matches_pattern("https://", r"^https?://\S+")); // too short
    }
}