Skip to main content

greentic_setup/
setup_input.rs

1//! Load and validate user-provided setup answers from JSON/YAML files.
2//!
3//! Supports both per-provider keyed answers (where the top-level JSON object
4//! maps provider IDs to their answers) and flat single-provider answers.
5
6use std::collections::BTreeSet;
7use std::fs::{self, File};
8use std::io::{self, Read, Write};
9use std::path::Path;
10use std::str::FromStr;
11
12use anyhow::{Context, anyhow};
13use rpassword::prompt_password;
14use serde::Deserialize;
15use serde_json::{Map as JsonMap, Value};
16use zip::{ZipArchive, result::ZipError};
17
18/// Answers loaded from a user-provided `--setup-input` file.
19#[derive(Clone)]
20pub struct SetupInputAnswers {
21    raw: Value,
22    provider_keys: BTreeSet<String>,
23}
24
25impl SetupInputAnswers {
26    /// Creates a new helper with the raw file data and the set of known provider IDs.
27    pub fn new(raw: Value, provider_keys: BTreeSet<String>) -> anyhow::Result<Self> {
28        Ok(Self { raw, provider_keys })
29    }
30
31    /// Returns the answers that correspond to a provider/pack.
32    ///
33    /// If the raw value is keyed by provider ID, returns only that provider's
34    /// answers.  Otherwise, returns the entire raw value (flat mode).
35    pub fn answers_for_provider(&self, provider: &str) -> Option<&Value> {
36        if let Some(map) = self.raw.as_object() {
37            if let Some(value) = map.get(provider) {
38                return Some(value);
39            }
40            if !self.provider_keys.is_empty()
41                && map.keys().all(|key| self.provider_keys.contains(key))
42            {
43                return None;
44            }
45        }
46        Some(&self.raw)
47    }
48}
49
50/// Reads a JSON/YAML answers file.
51pub fn load_setup_input(path: &Path) -> anyhow::Result<Value> {
52    let raw = load_text_from_path_or_url(path)?;
53    serde_json::from_str(&raw)
54        .or_else(|_| serde_yaml_bw::from_str(&raw))
55        .with_context(|| format!("parse setup input {}", path.display()))
56}
57
58fn load_text_from_path_or_url(path: &Path) -> anyhow::Result<String> {
59    let raw = path.to_string_lossy();
60    if raw.starts_with("https://") || raw.starts_with("http://") {
61        let response = ureq::get(raw.as_ref())
62            .call()
63            .map_err(|err| anyhow!("failed to fetch {}: {err}", raw))?;
64        return response
65            .into_body()
66            .read_to_string()
67            .map_err(|err| anyhow!("failed to read {}: {err}", raw));
68    }
69    fs::read_to_string(path).with_context(|| format!("read setup input {}", path.display()))
70}
71
72/// Represents a provider setup spec extracted from `assets/setup.yaml`.
73#[derive(Debug, Deserialize)]
74pub struct SetupSpec {
75    #[serde(default)]
76    pub title: Option<String>,
77    #[serde(default)]
78    pub description: Option<String>,
79    #[serde(default)]
80    pub questions: Vec<SetupQuestion>,
81    #[serde(default)]
82    pub setup_actions: Vec<Value>,
83}
84
85/// A single setup question definition.
86#[derive(Debug, Default, Deserialize)]
87pub struct SetupQuestion {
88    #[serde(default)]
89    pub name: String,
90    #[serde(default = "default_kind")]
91    pub kind: String,
92    #[serde(default)]
93    pub required: bool,
94    #[serde(default)]
95    pub help: Option<String>,
96    #[serde(default)]
97    pub choices: Vec<String>,
98    #[serde(default)]
99    pub default: Option<Value>,
100    #[serde(default)]
101    pub secret: bool,
102    #[serde(default)]
103    pub title: Option<String>,
104    #[serde(default)]
105    pub visible_if: Option<SetupVisibleIf>,
106    /// Example value shown as placeholder in the input field.
107    #[serde(default)]
108    pub placeholder: Option<String>,
109    /// Group/section name for organizing questions in the UI.
110    #[serde(default)]
111    pub group: Option<String>,
112    /// URL to external setup documentation.
113    #[serde(default)]
114    pub docs_url: Option<String>,
115    /// Column definitions for `kind: table` questions. Each row's answer is a
116    /// JSON object whose keys match the columns' `key` field.
117    #[serde(default)]
118    pub columns: Vec<SetupTableColumn>,
119    /// Minimum required row count for a `kind: table` question.
120    #[serde(default)]
121    pub min_rows: Option<u16>,
122    /// Maximum row count for a `kind: table` question.
123    #[serde(default)]
124    pub max_rows: Option<u16>,
125}
126
127/// One column in a `kind: table` setup question.
128#[derive(Debug, Default, Deserialize)]
129pub struct SetupTableColumn {
130    /// JSON object key the column's value is stored under (e.g. `"label"`).
131    /// Stable identifier — do not rename without a migration.
132    #[serde(default)]
133    pub key: String,
134    /// Header label shown above the column / next to each row's input.
135    #[serde(default)]
136    pub title: Option<String>,
137    /// Column scalar kind. Same vocabulary as top-level `kind` — but nested
138    /// tables are not supported.
139    #[serde(default = "default_kind")]
140    pub kind: String,
141    /// Whether the column must be filled for a row to count as non-empty.
142    #[serde(default)]
143    pub required: bool,
144    /// Optional inline help.
145    #[serde(default)]
146    pub help: Option<String>,
147    /// Optional placeholder shown when the cell is empty.
148    #[serde(default)]
149    pub placeholder: Option<String>,
150    /// Optional pre-defined choices for `kind: choice` columns.
151    #[serde(default)]
152    pub choices: Vec<String>,
153    /// Optional per-row default applied to new rows.
154    #[serde(default)]
155    pub default: Option<Value>,
156    /// When true, the wizard renders a multi-locale cell instead of a
157    /// scalar input. Operator types the primary (English) value and may
158    /// add per-locale translations via "+ Add language". Persisted as a
159    /// locale-keyed object `{en: "...", id: "...", ...}` (or a plain
160    /// string when only one locale was filled). Only meaningful for
161    /// `kind: string` columns.
162    #[serde(default)]
163    pub multilingual: bool,
164}
165
166/// Conditional visibility for a setup question.
167///
168/// Example in setup.yaml (struct format):
169/// ```yaml
170/// visible_if:
171///   field: public_base_url_mode
172///   eq: static
173/// ```
174///
175/// Or string expression format:
176/// ```yaml
177/// visible_if: "preset != 'stdout'"
178/// ```
179#[derive(Debug)]
180pub enum SetupVisibleIf {
181    /// Struct format with field and optional eq
182    Struct { field: String, eq: Option<String> },
183    /// String expression format (e.g., "preset != 'stdout'")
184    Expr(String),
185}
186
187impl SetupVisibleIf {
188    /// Get the field name (for struct format, or parse from expr format).
189    pub fn field(&self) -> Option<&str> {
190        match self {
191            SetupVisibleIf::Struct { field, .. } => Some(field),
192            SetupVisibleIf::Expr(_) => None,
193        }
194    }
195
196    /// Get the equality value (for struct format only).
197    pub fn eq(&self) -> Option<&str> {
198        match self {
199            SetupVisibleIf::Struct { eq, .. } => eq.as_deref(),
200            SetupVisibleIf::Expr(_) => None,
201        }
202    }
203}
204
205impl<'de> serde::Deserialize<'de> for SetupVisibleIf {
206    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
207    where
208        D: serde::Deserializer<'de>,
209    {
210        use serde::de::{self, MapAccess, Visitor};
211
212        struct SetupVisibleIfVisitor;
213
214        impl<'de> Visitor<'de> for SetupVisibleIfVisitor {
215            type Value = SetupVisibleIf;
216
217            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
218                formatter
219                    .write_str("a string expression or a struct with 'field' and optional 'eq'")
220            }
221
222            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
223            where
224                E: de::Error,
225            {
226                Ok(SetupVisibleIf::Expr(value.to_string()))
227            }
228
229            fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
230            where
231                E: de::Error,
232            {
233                Ok(SetupVisibleIf::Expr(value))
234            }
235
236            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
237            where
238                M: MapAccess<'de>,
239            {
240                let mut field: Option<String> = None;
241                let mut eq: Option<String> = None;
242
243                while let Some(key) = map.next_key::<String>()? {
244                    match key.as_str() {
245                        "field" => {
246                            field = Some(map.next_value()?);
247                        }
248                        "eq" => {
249                            eq = Some(map.next_value()?);
250                        }
251                        _ => {
252                            let _: serde::de::IgnoredAny = map.next_value()?;
253                        }
254                    }
255                }
256
257                let field = field.ok_or_else(|| de::Error::missing_field("field"))?;
258                Ok(SetupVisibleIf::Struct { field, eq })
259            }
260        }
261
262        deserializer.deserialize_any(SetupVisibleIfVisitor)
263    }
264}
265
266fn default_kind() -> String {
267    "string".to_string()
268}
269
270/// Load a `SetupSpec` from `assets/setup.yaml` inside a `.gtpack` archive.
271///
272/// Falls back to reading `setup.yaml` from the filesystem next to the pack
273/// (sibling or `assets/` subdirectory) when the archive does not contain it.
274pub fn load_setup_spec(pack_path: &Path) -> anyhow::Result<Option<SetupSpec>> {
275    let file = File::open(pack_path)?;
276    let mut archive = match ZipArchive::new(file) {
277        Ok(archive) => archive,
278        Err(ZipError::InvalidArchive(_)) | Err(ZipError::UnsupportedArchive(_)) => return Ok(None),
279        Err(err) => return Err(err.into()),
280    };
281    let contents = match read_setup_yaml(&mut archive)? {
282        Some(value) => value,
283        None => match read_setup_yaml_from_filesystem(pack_path)? {
284            Some(value) => value,
285            None => return Ok(None),
286        },
287    };
288    let spec: SetupSpec =
289        serde_yaml_bw::from_str(&contents).context("parse provider setup spec")?;
290    Ok(Some(spec))
291}
292
293fn read_setup_yaml(archive: &mut ZipArchive<File>) -> anyhow::Result<Option<String>> {
294    for entry in ["assets/setup.yaml", "setup.yaml"] {
295        match archive.by_name(entry) {
296            Ok(mut file) => {
297                let mut contents = String::new();
298                file.read_to_string(&mut contents)?;
299                return Ok(Some(contents));
300            }
301            Err(ZipError::FileNotFound) => continue,
302            Err(err) => return Err(err.into()),
303        }
304    }
305    Ok(None)
306}
307
308/// Fallback: look for `setup.yaml` on the filesystem near the `.gtpack` file.
309///
310/// Searches sibling paths relative to the pack file:
311///   1. `<pack_dir>/assets/setup.yaml`
312///   2. `<pack_dir>/setup.yaml`
313///
314/// Also searches based on pack filename (e.g. for `messaging-telegram.gtpack`):
315///   3. `<pack_dir>/../../../packs/messaging-telegram/assets/setup.yaml`
316///   4. `<pack_dir>/../../../packs/messaging-telegram/setup.yaml`
317fn read_setup_yaml_from_filesystem(pack_path: &Path) -> anyhow::Result<Option<String>> {
318    let pack_dir = pack_path.parent().unwrap_or(Path::new("."));
319    let pack_stem = pack_path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
320
321    let candidates = [
322        pack_dir.join("assets/setup.yaml"),
323        pack_dir.join("setup.yaml"),
324    ];
325
326    // Also try a source-layout path: packs/<pack_stem>/assets/setup.yaml
327    let mut all_candidates: Vec<std::path::PathBuf> = candidates.to_vec();
328    if !pack_stem.is_empty() {
329        // Walk up to find a packs/ directory (common in greentic-messaging-providers layout)
330        for ancestor in pack_dir.ancestors().skip(1).take(4) {
331            let source_dir = ancestor.join("packs").join(pack_stem);
332            if source_dir.is_dir() {
333                all_candidates.push(source_dir.join("assets/setup.yaml"));
334                all_candidates.push(source_dir.join("setup.yaml"));
335                break;
336            }
337        }
338    }
339
340    for candidate in &all_candidates {
341        if candidate.is_file() {
342            let contents = fs::read_to_string(candidate)?;
343            return Ok(Some(contents));
344        }
345    }
346    Ok(None)
347}
348
349/// Collect setup answers for a provider pack.
350///
351/// Uses provided input answers if available, otherwise falls back to
352/// interactive prompting (if `interactive` is true) or returns an error.
353pub fn collect_setup_answers(
354    pack_path: &Path,
355    provider_id: &str,
356    setup_input: Option<&SetupInputAnswers>,
357    interactive: bool,
358) -> anyhow::Result<Value> {
359    let spec = load_setup_spec(pack_path)?;
360    if let Some(input) = setup_input {
361        if let Some(value) = input.answers_for_provider(provider_id) {
362            let answers = ensure_object(value.clone())?;
363            ensure_required_answers(spec.as_ref(), &answers)?;
364            return Ok(answers);
365        }
366        if has_required_questions(spec.as_ref()) {
367            return Err(anyhow!("setup input missing answers for {provider_id}"));
368        }
369        return Ok(Value::Object(JsonMap::new()));
370    }
371    if let Some(spec) = spec {
372        if spec.questions.is_empty() {
373            return Ok(Value::Object(JsonMap::new()));
374        }
375        if interactive {
376            let answers = prompt_setup_answers(&spec, provider_id)?;
377            ensure_required_answers(Some(&spec), &answers)?;
378            return Ok(answers);
379        }
380        return Err(anyhow!(
381            "setup answers required for {provider_id} but run is non-interactive"
382        ));
383    }
384    Ok(Value::Object(JsonMap::new()))
385}
386
387fn has_required_questions(spec: Option<&SetupSpec>) -> bool {
388    spec.map(|spec| spec.questions.iter().any(|q| q.required))
389        .unwrap_or(false)
390}
391
392/// Validate that all required answers are present.
393pub fn ensure_required_answers(spec: Option<&SetupSpec>, answers: &Value) -> anyhow::Result<()> {
394    let map = answers
395        .as_object()
396        .ok_or_else(|| anyhow!("setup answers must be an object"))?;
397    if let Some(spec) = spec {
398        for question in spec.questions.iter().filter(|q| q.required) {
399            match map.get(&question.name) {
400                Some(value) if !value.is_null() => continue,
401                _ => {
402                    return Err(anyhow!(
403                        "missing required setup answer for {}",
404                        question.name
405                    ));
406                }
407            }
408        }
409    }
410    Ok(())
411}
412
413/// Ensure a JSON value is an object.
414pub fn ensure_object(value: Value) -> anyhow::Result<Value> {
415    match value {
416        Value::Object(_) => Ok(value),
417        other => Err(anyhow!(
418            "setup answers must be a JSON object, got {}",
419            other
420        )),
421    }
422}
423
424/// Interactively prompt the user for setup answers.
425pub fn prompt_setup_answers(spec: &SetupSpec, provider: &str) -> anyhow::Result<Value> {
426    if spec.questions.is_empty() {
427        return Ok(Value::Object(JsonMap::new()));
428    }
429    let title = spec.title.as_deref().unwrap_or(provider).to_string();
430    println!("\nConfiguring {provider}: {title}");
431    let mut answers = JsonMap::new();
432    for question in &spec.questions {
433        if question.name.trim().is_empty() {
434            continue;
435        }
436        if let Some(value) = ask_setup_question(question)? {
437            answers.insert(question.name.clone(), value);
438        }
439    }
440    Ok(Value::Object(answers))
441}
442
443fn ask_setup_question(question: &SetupQuestion) -> anyhow::Result<Option<Value>> {
444    if let Some(help) = question.help.as_ref()
445        && !help.trim().is_empty()
446    {
447        println!("  {help}");
448    }
449    if !question.choices.is_empty() {
450        println!("  Choices:");
451        for (idx, choice) in question.choices.iter().enumerate() {
452            println!("    {}) {}", idx + 1, choice);
453        }
454    }
455    loop {
456        let prompt = build_question_prompt(question);
457        let input = read_question_input(&prompt, question.secret)?;
458        let trimmed = input.trim();
459        if trimmed.is_empty() {
460            if let Some(default) = question.default.clone() {
461                return Ok(Some(default));
462            }
463            if question.required {
464                println!("  This field is required.");
465                continue;
466            }
467            return Ok(None);
468        }
469        match parse_question_value(question, trimmed) {
470            Ok(value) => return Ok(Some(value)),
471            Err(err) => {
472                println!("  {err}");
473                continue;
474            }
475        }
476    }
477}
478
479fn build_question_prompt(question: &SetupQuestion) -> String {
480    let mut prompt = question
481        .title
482        .as_deref()
483        .unwrap_or(&question.name)
484        .to_string();
485    if question.kind != "string" {
486        prompt = format!("{prompt} [{}]", question.kind);
487    }
488    if let Some(default) = &question.default {
489        prompt = format!("{prompt} [default: {}]", display_value(default));
490    }
491    prompt.push_str(": ");
492    prompt
493}
494
495fn read_question_input(prompt: &str, secret: bool) -> anyhow::Result<String> {
496    if secret {
497        prompt_password(prompt).map_err(|err| anyhow!("read secret: {err}"))
498    } else {
499        print!("{prompt}");
500        io::stdout().flush()?;
501        let mut buffer = String::new();
502        io::stdin().read_line(&mut buffer)?;
503        Ok(buffer)
504    }
505}
506
507fn parse_question_value(question: &SetupQuestion, input: &str) -> anyhow::Result<Value> {
508    let kind = question.kind.to_lowercase();
509    match kind.as_str() {
510        "number" => serde_json::Number::from_str(input)
511            .map(Value::Number)
512            .map_err(|err| anyhow!("invalid number: {err}")),
513        "choice" => {
514            if question.choices.is_empty() {
515                return Ok(Value::String(input.to_string()));
516            }
517            if let Ok(index) = input.parse::<usize>()
518                && let Some(choice) = question.choices.get(index - 1)
519            {
520                return Ok(Value::String(choice.clone()));
521            }
522            for choice in &question.choices {
523                if choice == input {
524                    return Ok(Value::String(choice.clone()));
525                }
526            }
527            Err(anyhow!("invalid choice '{input}'"))
528        }
529        "boolean" => match input.to_lowercase().as_str() {
530            "true" | "t" | "yes" | "y" => Ok(Value::Bool(true)),
531            "false" | "f" | "no" | "n" => Ok(Value::Bool(false)),
532            _ => Err(anyhow!("invalid boolean value")),
533        },
534        _ => Ok(Value::String(input.to_string())),
535    }
536}
537
538fn display_value(value: &Value) -> String {
539    match value {
540        Value::String(v) => v.clone(),
541        Value::Number(n) => n.to_string(),
542        Value::Bool(b) => b.to_string(),
543        other => other.to_string(),
544    }
545}
546
547#[cfg(test)]
548mod tests {
549    use super::*;
550    use serde_json::json;
551    use std::io::Write;
552    use zip::write::{FileOptions, ZipWriter};
553
554    fn create_test_pack(yaml: &str) -> anyhow::Result<(tempfile::TempDir, std::path::PathBuf)> {
555        let temp_dir = tempfile::tempdir()?;
556        let pack_path = temp_dir.path().join("messaging-test.gtpack");
557        let file = File::create(&pack_path)?;
558        let mut writer = ZipWriter::new(file);
559        let options: FileOptions<'_, ()> =
560            FileOptions::default().compression_method(zip::CompressionMethod::Stored);
561        writer.start_file("assets/setup.yaml", options)?;
562        writer.write_all(yaml.as_bytes())?;
563        writer.finish()?;
564        Ok((temp_dir, pack_path))
565    }
566
567    #[test]
568    fn parse_setup_yaml_questions() -> anyhow::Result<()> {
569        let yaml =
570            "provider_id: dummy\nquestions:\n  - name: public_base_url\n    required: true\n";
571        let (_dir, pack_path) = create_test_pack(yaml)?;
572        let spec = load_setup_spec(&pack_path)?.expect("expected spec");
573        assert_eq!(spec.questions.len(), 1);
574        assert_eq!(spec.questions[0].name, "public_base_url");
575        assert!(spec.questions[0].required);
576        Ok(())
577    }
578
579    #[test]
580    fn parse_setup_yaml_setup_actions() -> anyhow::Result<()> {
581        let yaml = r#"
582provider_id: slack
583questions: []
584setup_actions:
585  - id: add_to_slack
586    label: Add to Slack
587    kind: oauth_install_button
588"#;
589        let (_dir, pack_path) = create_test_pack(yaml)?;
590        let spec = load_setup_spec(&pack_path)?.expect("expected spec");
591        assert!(spec.questions.is_empty());
592        assert_eq!(spec.setup_actions.len(), 1);
593        assert_eq!(spec.setup_actions[0]["id"], json!("add_to_slack"));
594        assert_eq!(spec.setup_actions[0]["kind"], json!("oauth_install_button"));
595        Ok(())
596    }
597
598    #[test]
599    fn collect_setup_answers_uses_input() -> anyhow::Result<()> {
600        let yaml =
601            "provider_id: telegram\nquestions:\n  - name: public_base_url\n    required: true\n";
602        let (_dir, pack_path) = create_test_pack(yaml)?;
603        let provider_keys = BTreeSet::from(["messaging-telegram".to_string()]);
604        let raw = json!({ "messaging-telegram": { "public_base_url": "https://example.com" } });
605        let answers = SetupInputAnswers::new(raw, provider_keys)?;
606        let collected =
607            collect_setup_answers(&pack_path, "messaging-telegram", Some(&answers), false)?;
608        assert_eq!(
609            collected.get("public_base_url"),
610            Some(&Value::String("https://example.com".to_string()))
611        );
612        Ok(())
613    }
614
615    #[test]
616    fn collect_setup_answers_missing_required_errors() -> anyhow::Result<()> {
617        let yaml =
618            "provider_id: slack\nquestions:\n  - name: slack_bot_token\n    required: true\n";
619        let (_dir, pack_path) = create_test_pack(yaml)?;
620        let provider_keys = BTreeSet::from(["messaging-slack".to_string()]);
621        let raw = json!({ "messaging-slack": {} });
622        let answers = SetupInputAnswers::new(raw, provider_keys)?;
623        let error = collect_setup_answers(&pack_path, "messaging-slack", Some(&answers), false)
624            .unwrap_err();
625        assert!(error.to_string().contains("missing required setup answer"));
626        Ok(())
627    }
628}