1use 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#[derive(Clone)]
20pub struct SetupInputAnswers {
21 raw: Value,
22 provider_keys: BTreeSet<String>,
23}
24
25impl SetupInputAnswers {
26 pub fn new(raw: Value, provider_keys: BTreeSet<String>) -> anyhow::Result<Self> {
28 Ok(Self { raw, provider_keys })
29 }
30
31 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
50pub 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#[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}
82
83#[derive(Debug, Deserialize)]
85pub struct SetupQuestion {
86 #[serde(default)]
87 pub name: String,
88 #[serde(default = "default_kind")]
89 pub kind: String,
90 #[serde(default)]
91 pub required: bool,
92 #[serde(default)]
93 pub help: Option<String>,
94 #[serde(default)]
95 pub choices: Vec<String>,
96 #[serde(default)]
97 pub default: Option<Value>,
98 #[serde(default)]
99 pub secret: bool,
100 #[serde(default)]
101 pub title: Option<String>,
102 #[serde(default)]
103 pub visible_if: Option<SetupVisibleIf>,
104}
105
106#[derive(Debug)]
120pub enum SetupVisibleIf {
121 Struct { field: String, eq: Option<String> },
123 Expr(String),
125}
126
127impl SetupVisibleIf {
128 pub fn field(&self) -> Option<&str> {
130 match self {
131 SetupVisibleIf::Struct { field, .. } => Some(field),
132 SetupVisibleIf::Expr(_) => None,
133 }
134 }
135
136 pub fn eq(&self) -> Option<&str> {
138 match self {
139 SetupVisibleIf::Struct { eq, .. } => eq.as_deref(),
140 SetupVisibleIf::Expr(_) => None,
141 }
142 }
143}
144
145impl<'de> serde::Deserialize<'de> for SetupVisibleIf {
146 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
147 where
148 D: serde::Deserializer<'de>,
149 {
150 use serde::de::{self, MapAccess, Visitor};
151
152 struct SetupVisibleIfVisitor;
153
154 impl<'de> Visitor<'de> for SetupVisibleIfVisitor {
155 type Value = SetupVisibleIf;
156
157 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
158 formatter
159 .write_str("a string expression or a struct with 'field' and optional 'eq'")
160 }
161
162 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
163 where
164 E: de::Error,
165 {
166 Ok(SetupVisibleIf::Expr(value.to_string()))
167 }
168
169 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
170 where
171 E: de::Error,
172 {
173 Ok(SetupVisibleIf::Expr(value))
174 }
175
176 fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
177 where
178 M: MapAccess<'de>,
179 {
180 let mut field: Option<String> = None;
181 let mut eq: Option<String> = None;
182
183 while let Some(key) = map.next_key::<String>()? {
184 match key.as_str() {
185 "field" => {
186 field = Some(map.next_value()?);
187 }
188 "eq" => {
189 eq = Some(map.next_value()?);
190 }
191 _ => {
192 let _: serde::de::IgnoredAny = map.next_value()?;
193 }
194 }
195 }
196
197 let field = field.ok_or_else(|| de::Error::missing_field("field"))?;
198 Ok(SetupVisibleIf::Struct { field, eq })
199 }
200 }
201
202 deserializer.deserialize_any(SetupVisibleIfVisitor)
203 }
204}
205
206fn default_kind() -> String {
207 "string".to_string()
208}
209
210pub fn load_setup_spec(pack_path: &Path) -> anyhow::Result<Option<SetupSpec>> {
215 let file = File::open(pack_path)?;
216 let mut archive = match ZipArchive::new(file) {
217 Ok(archive) => archive,
218 Err(ZipError::InvalidArchive(_)) | Err(ZipError::UnsupportedArchive(_)) => return Ok(None),
219 Err(err) => return Err(err.into()),
220 };
221 let contents = match read_setup_yaml(&mut archive)? {
222 Some(value) => value,
223 None => match read_setup_yaml_from_filesystem(pack_path)? {
224 Some(value) => value,
225 None => return Ok(None),
226 },
227 };
228 let spec: SetupSpec =
229 serde_yaml_bw::from_str(&contents).context("parse provider setup spec")?;
230 Ok(Some(spec))
231}
232
233fn read_setup_yaml(archive: &mut ZipArchive<File>) -> anyhow::Result<Option<String>> {
234 for entry in ["assets/setup.yaml", "setup.yaml"] {
235 match archive.by_name(entry) {
236 Ok(mut file) => {
237 let mut contents = String::new();
238 file.read_to_string(&mut contents)?;
239 return Ok(Some(contents));
240 }
241 Err(ZipError::FileNotFound) => continue,
242 Err(err) => return Err(err.into()),
243 }
244 }
245 Ok(None)
246}
247
248fn read_setup_yaml_from_filesystem(pack_path: &Path) -> anyhow::Result<Option<String>> {
258 let pack_dir = pack_path.parent().unwrap_or(Path::new("."));
259 let pack_stem = pack_path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
260
261 let candidates = [
262 pack_dir.join("assets/setup.yaml"),
263 pack_dir.join("setup.yaml"),
264 ];
265
266 let mut all_candidates: Vec<std::path::PathBuf> = candidates.to_vec();
268 if !pack_stem.is_empty() {
269 for ancestor in pack_dir.ancestors().skip(1).take(4) {
271 let source_dir = ancestor.join("packs").join(pack_stem);
272 if source_dir.is_dir() {
273 all_candidates.push(source_dir.join("assets/setup.yaml"));
274 all_candidates.push(source_dir.join("setup.yaml"));
275 break;
276 }
277 }
278 }
279
280 for candidate in &all_candidates {
281 if candidate.is_file() {
282 let contents = fs::read_to_string(candidate)?;
283 return Ok(Some(contents));
284 }
285 }
286 Ok(None)
287}
288
289pub fn collect_setup_answers(
294 pack_path: &Path,
295 provider_id: &str,
296 setup_input: Option<&SetupInputAnswers>,
297 interactive: bool,
298) -> anyhow::Result<Value> {
299 let spec = load_setup_spec(pack_path)?;
300 if let Some(input) = setup_input {
301 if let Some(value) = input.answers_for_provider(provider_id) {
302 let answers = ensure_object(value.clone())?;
303 ensure_required_answers(spec.as_ref(), &answers)?;
304 return Ok(answers);
305 }
306 if has_required_questions(spec.as_ref()) {
307 return Err(anyhow!("setup input missing answers for {provider_id}"));
308 }
309 return Ok(Value::Object(JsonMap::new()));
310 }
311 if let Some(spec) = spec {
312 if spec.questions.is_empty() {
313 return Ok(Value::Object(JsonMap::new()));
314 }
315 if interactive {
316 let answers = prompt_setup_answers(&spec, provider_id)?;
317 ensure_required_answers(Some(&spec), &answers)?;
318 return Ok(answers);
319 }
320 return Err(anyhow!(
321 "setup answers required for {provider_id} but run is non-interactive"
322 ));
323 }
324 Ok(Value::Object(JsonMap::new()))
325}
326
327fn has_required_questions(spec: Option<&SetupSpec>) -> bool {
328 spec.map(|spec| spec.questions.iter().any(|q| q.required))
329 .unwrap_or(false)
330}
331
332pub fn ensure_required_answers(spec: Option<&SetupSpec>, answers: &Value) -> anyhow::Result<()> {
334 let map = answers
335 .as_object()
336 .ok_or_else(|| anyhow!("setup answers must be an object"))?;
337 if let Some(spec) = spec {
338 for question in spec.questions.iter().filter(|q| q.required) {
339 match map.get(&question.name) {
340 Some(value) if !value.is_null() => continue,
341 _ => {
342 return Err(anyhow!(
343 "missing required setup answer for {}",
344 question.name
345 ));
346 }
347 }
348 }
349 }
350 Ok(())
351}
352
353pub fn ensure_object(value: Value) -> anyhow::Result<Value> {
355 match value {
356 Value::Object(_) => Ok(value),
357 other => Err(anyhow!(
358 "setup answers must be a JSON object, got {}",
359 other
360 )),
361 }
362}
363
364pub fn prompt_setup_answers(spec: &SetupSpec, provider: &str) -> anyhow::Result<Value> {
366 if spec.questions.is_empty() {
367 return Ok(Value::Object(JsonMap::new()));
368 }
369 let title = spec.title.as_deref().unwrap_or(provider).to_string();
370 println!("\nConfiguring {provider}: {title}");
371 let mut answers = JsonMap::new();
372 for question in &spec.questions {
373 if question.name.trim().is_empty() {
374 continue;
375 }
376 if let Some(value) = ask_setup_question(question)? {
377 answers.insert(question.name.clone(), value);
378 }
379 }
380 Ok(Value::Object(answers))
381}
382
383fn ask_setup_question(question: &SetupQuestion) -> anyhow::Result<Option<Value>> {
384 if let Some(help) = question.help.as_ref()
385 && !help.trim().is_empty()
386 {
387 println!(" {help}");
388 }
389 if !question.choices.is_empty() {
390 println!(" Choices:");
391 for (idx, choice) in question.choices.iter().enumerate() {
392 println!(" {}) {}", idx + 1, choice);
393 }
394 }
395 loop {
396 let prompt = build_question_prompt(question);
397 let input = read_question_input(&prompt, question.secret)?;
398 let trimmed = input.trim();
399 if trimmed.is_empty() {
400 if let Some(default) = question.default.clone() {
401 return Ok(Some(default));
402 }
403 if question.required {
404 println!(" This field is required.");
405 continue;
406 }
407 return Ok(None);
408 }
409 match parse_question_value(question, trimmed) {
410 Ok(value) => return Ok(Some(value)),
411 Err(err) => {
412 println!(" {err}");
413 continue;
414 }
415 }
416 }
417}
418
419fn build_question_prompt(question: &SetupQuestion) -> String {
420 let mut prompt = question
421 .title
422 .as_deref()
423 .unwrap_or(&question.name)
424 .to_string();
425 if question.kind != "string" {
426 prompt = format!("{prompt} [{}]", question.kind);
427 }
428 if let Some(default) = &question.default {
429 prompt = format!("{prompt} [default: {}]", display_value(default));
430 }
431 prompt.push_str(": ");
432 prompt
433}
434
435fn read_question_input(prompt: &str, secret: bool) -> anyhow::Result<String> {
436 if secret {
437 prompt_password(prompt).map_err(|err| anyhow!("read secret: {err}"))
438 } else {
439 print!("{prompt}");
440 io::stdout().flush()?;
441 let mut buffer = String::new();
442 io::stdin().read_line(&mut buffer)?;
443 Ok(buffer)
444 }
445}
446
447fn parse_question_value(question: &SetupQuestion, input: &str) -> anyhow::Result<Value> {
448 let kind = question.kind.to_lowercase();
449 match kind.as_str() {
450 "number" => serde_json::Number::from_str(input)
451 .map(Value::Number)
452 .map_err(|err| anyhow!("invalid number: {err}")),
453 "choice" => {
454 if question.choices.is_empty() {
455 return Ok(Value::String(input.to_string()));
456 }
457 if let Ok(index) = input.parse::<usize>()
458 && let Some(choice) = question.choices.get(index - 1)
459 {
460 return Ok(Value::String(choice.clone()));
461 }
462 for choice in &question.choices {
463 if choice == input {
464 return Ok(Value::String(choice.clone()));
465 }
466 }
467 Err(anyhow!("invalid choice '{input}'"))
468 }
469 "boolean" => match input.to_lowercase().as_str() {
470 "true" | "t" | "yes" | "y" => Ok(Value::Bool(true)),
471 "false" | "f" | "no" | "n" => Ok(Value::Bool(false)),
472 _ => Err(anyhow!("invalid boolean value")),
473 },
474 _ => Ok(Value::String(input.to_string())),
475 }
476}
477
478fn display_value(value: &Value) -> String {
479 match value {
480 Value::String(v) => v.clone(),
481 Value::Number(n) => n.to_string(),
482 Value::Bool(b) => b.to_string(),
483 other => other.to_string(),
484 }
485}
486
487#[cfg(test)]
488mod tests {
489 use super::*;
490 use serde_json::json;
491 use std::io::Write;
492 use zip::write::{FileOptions, ZipWriter};
493
494 fn create_test_pack(yaml: &str) -> anyhow::Result<(tempfile::TempDir, std::path::PathBuf)> {
495 let temp_dir = tempfile::tempdir()?;
496 let pack_path = temp_dir.path().join("messaging-test.gtpack");
497 let file = File::create(&pack_path)?;
498 let mut writer = ZipWriter::new(file);
499 let options: FileOptions<'_, ()> =
500 FileOptions::default().compression_method(zip::CompressionMethod::Stored);
501 writer.start_file("assets/setup.yaml", options)?;
502 writer.write_all(yaml.as_bytes())?;
503 writer.finish()?;
504 Ok((temp_dir, pack_path))
505 }
506
507 #[test]
508 fn parse_setup_yaml_questions() -> anyhow::Result<()> {
509 let yaml =
510 "provider_id: dummy\nquestions:\n - name: public_base_url\n required: true\n";
511 let (_dir, pack_path) = create_test_pack(yaml)?;
512 let spec = load_setup_spec(&pack_path)?.expect("expected spec");
513 assert_eq!(spec.questions.len(), 1);
514 assert_eq!(spec.questions[0].name, "public_base_url");
515 assert!(spec.questions[0].required);
516 Ok(())
517 }
518
519 #[test]
520 fn collect_setup_answers_uses_input() -> anyhow::Result<()> {
521 let yaml =
522 "provider_id: telegram\nquestions:\n - name: public_base_url\n required: true\n";
523 let (_dir, pack_path) = create_test_pack(yaml)?;
524 let provider_keys = BTreeSet::from(["messaging-telegram".to_string()]);
525 let raw = json!({ "messaging-telegram": { "public_base_url": "https://example.com" } });
526 let answers = SetupInputAnswers::new(raw, provider_keys)?;
527 let collected =
528 collect_setup_answers(&pack_path, "messaging-telegram", Some(&answers), false)?;
529 assert_eq!(
530 collected.get("public_base_url"),
531 Some(&Value::String("https://example.com".to_string()))
532 );
533 Ok(())
534 }
535
536 #[test]
537 fn collect_setup_answers_missing_required_errors() -> anyhow::Result<()> {
538 let yaml =
539 "provider_id: slack\nquestions:\n - name: slack_bot_token\n required: true\n";
540 let (_dir, pack_path) = create_test_pack(yaml)?;
541 let provider_keys = BTreeSet::from(["messaging-slack".to_string()]);
542 let raw = json!({ "messaging-slack": {} });
543 let answers = SetupInputAnswers::new(raw, provider_keys)?;
544 let error = collect_setup_answers(&pack_path, "messaging-slack", Some(&answers), false)
545 .unwrap_err();
546 assert!(error.to_string().contains("missing required setup answer"));
547 Ok(())
548 }
549}