Skip to main content

greentic_setup/
lib.rs

1//! End-to-end bundle setup engine for the Greentic platform.
2//!
3//! Provides pack discovery, QA-driven configuration, secrets persistence,
4//! and bundle lifecycle management as a library crate.
5
6pub mod admin;
7pub mod answers_crypto;
8pub mod bundle;
9pub mod bundle_source;
10pub mod capabilities;
11pub mod card_setup;
12pub mod cli_args;
13pub mod cli_commands;
14pub mod cli_helpers;
15pub mod cli_i18n;
16pub mod config_envelope;
17pub mod deployment_targets;
18pub mod discovery;
19pub mod engine;
20pub mod flow;
21pub mod gtbundle;
22pub mod plan;
23pub mod platform_setup;
24pub mod reload;
25pub mod secret_name;
26pub mod secrets;
27pub mod setup_input;
28pub mod setup_to_formspec;
29pub mod webhook;
30
31pub mod qa {
32    //! QA-driven configuration: FormSpec bridge, wizard prompts, answers
33    //! persistence, and setup input loading.
34    pub mod bridge;
35    pub mod persist;
36    pub mod prompts;
37    pub mod shared_questions;
38    pub mod wizard;
39}
40
41pub use bundle_source::BundleSource;
42pub use engine::SetupEngine;
43pub use plan::{SetupMode, SetupPlan, SetupStep, SetupStepKind};
44
45// Re-export shared questions types and functions for convenient multi-provider setup
46pub use qa::wizard::{
47    ProviderFormSpec, SHARED_QUESTION_IDS, SharedQuestionsResult, build_provider_form_specs,
48    collect_shared_questions, prompt_shared_questions, run_qa_setup_with_shared,
49};
50
51/// Returns the crate version.
52pub fn version() -> &'static str {
53    env!("CARGO_PKG_VERSION")
54}
55
56/// Resolve the effective environment string.
57///
58/// Priority: explicit override > `$GREENTIC_ENV` > `"dev"`.
59pub fn resolve_env(override_env: Option<&str>) -> String {
60    override_env
61        .map(|v| v.to_string())
62        .or_else(|| std::env::var("GREENTIC_ENV").ok())
63        .unwrap_or_else(|| "dev".to_string())
64}
65
66/// Build a canonical secret URI: `secrets://{env}/{tenant}/{team}/{provider}/{key}`.
67pub fn canonical_secret_uri(
68    env: &str,
69    tenant: &str,
70    team: Option<&str>,
71    provider: &str,
72    key: &str,
73) -> String {
74    let team_segment = canonical_team(team);
75    let provider_segment = if provider.is_empty() {
76        "messaging".to_string()
77    } else {
78        provider.to_string()
79    };
80    let normalized_key = secret_name::canonical_secret_name(key);
81    format!("secrets://{env}/{tenant}/{team_segment}/{provider_segment}/{normalized_key}")
82}
83
84/// Normalize the team segment for secret URIs.
85///
86/// Empty, `"default"`, or `None` → `"_"` (wildcard).
87fn canonical_team(team: Option<&str>) -> &str {
88    match team
89        .map(|v| v.trim())
90        .filter(|t| !t.is_empty() && !t.eq_ignore_ascii_case("default"))
91    {
92        Some(v) => v,
93        None => "_",
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn version_is_correct() {
103        assert!(version().starts_with("0.4"));
104    }
105
106    #[test]
107    fn secret_uri_basic() {
108        let uri = canonical_secret_uri("dev", "demo", None, "messaging-telegram", "bot_token");
109        assert_eq!(uri, "secrets://dev/demo/_/messaging-telegram/bot_token");
110    }
111
112    #[test]
113    fn secret_uri_with_team() {
114        let uri = canonical_secret_uri("dev", "acme", Some("ops"), "state-redis", "redis_url");
115        assert_eq!(uri, "secrets://dev/acme/ops/state-redis/redis_url");
116    }
117
118    #[test]
119    fn secret_uri_default_team_becomes_wildcard() {
120        let uri = canonical_secret_uri(
121            "dev",
122            "demo",
123            Some("default"),
124            "messaging-slack",
125            "bot_token",
126        );
127        assert_eq!(uri, "secrets://dev/demo/_/messaging-slack/bot_token");
128    }
129}