syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
416
//! Environment creation wizard for deployment targets
//!
//! Interactive wizard that guides users through creating a new environment
//! with target type selection (Kubernetes or Cloud Runner).

use crate::platform::api::client::PlatformApiClient;
use crate::platform::api::types::{CloudProvider, ClusterSummary, Environment};
use crate::wizard::cloud_provider_data::{get_default_region, get_regions_for_provider};
use crate::wizard::provider_selection::get_provider_deployment_statuses;
use crate::wizard::render::{display_step_header, wizard_render_config};
use colored::Colorize;
use inquire::{InquireError, MultiSelect, Select, Text};
use std::collections::HashMap;

/// Environment type for the API
/// "cluster" = Kubernetes cluster
/// "cloud" = Cloud Runner (serverless)
#[derive(Debug, Clone, PartialEq, Eq)]
enum EnvironmentType {
    Cluster,
    Cloud,
}

impl EnvironmentType {
    fn as_str(&self) -> &'static str {
        match self {
            EnvironmentType::Cluster => "cluster",
            EnvironmentType::Cloud => "cloud",
        }
    }

    fn display_name(&self) -> &'static str {
        match self {
            EnvironmentType::Cluster => "Kubernetes",
            EnvironmentType::Cloud => "Cloud Runner",
        }
    }
}

/// Result of environment creation wizard
#[derive(Debug)]
pub enum EnvironmentCreationResult {
    /// Environment created successfully
    Created(Environment),
    /// User cancelled the wizard
    Cancelled,
    /// An error occurred
    Error(String),
}

/// Run the environment creation wizard
///
/// Guides user through:
/// 1. Choosing environment name
/// 2. Selecting target type (Kubernetes or Cloud Runner)
/// 3. If Kubernetes: selecting a cluster
pub async fn create_environment_wizard(
    client: &PlatformApiClient,
    project_id: &str,
) -> EnvironmentCreationResult {
    display_step_header(
        1,
        "Create Environment",
        "Set up a new deployment environment for your project.",
    );

    // Step 1: Get environment name
    let name = match Text::new("Environment name:")
        .with_placeholder("e.g., production, staging, development")
        .with_help_message("Choose a descriptive name for this environment")
        .prompt()
    {
        Ok(name) => {
            if name.trim().is_empty() {
                println!("\n{}", "Environment name cannot be empty.".red());
                return EnvironmentCreationResult::Cancelled;
            }
            name.trim().to_string()
        }
        Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
            println!("\n{}", "Wizard cancelled.".dimmed());
            return EnvironmentCreationResult::Cancelled;
        }
        Err(e) => {
            return EnvironmentCreationResult::Error(format!("Input error: {}", e));
        }
    };

    // Step 2: Select target type
    display_step_header(
        2,
        "Select Target Type",
        "Choose how this environment will deploy services.",
    );

    let target_options = vec![
        format!(
            "{}  {}",
            "Cloud Runner".cyan(),
            "Fully managed, auto-scaling containers".dimmed()
        ),
        format!(
            "{}  {}",
            "Kubernetes".cyan(),
            "Deploy to your own K8s cluster".dimmed()
        ),
    ];

    let target_selection = Select::new("Select target type:", target_options)
        .with_render_config(wizard_render_config())
        .with_help_message("Cloud Runner: serverless, Kubernetes: full control")
        .prompt();

    let env_type = match target_selection {
        Ok(answer) => {
            if answer.contains("Cloud Runner") {
                EnvironmentType::Cloud
            } else {
                EnvironmentType::Cluster
            }
        }
        Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
            println!("\n{}", "Wizard cancelled.".dimmed());
            return EnvironmentCreationResult::Cancelled;
        }
        Err(e) => {
            return EnvironmentCreationResult::Error(format!("Selection error: {}", e));
        }
    };

    println!(
        "\n{} Target: {}",
        "".green(),
        env_type.display_name().bold()
    );

    // Step 3: If Kubernetes (cluster), select cluster
    let cluster_id = if env_type == EnvironmentType::Cluster {
        match select_cluster_for_env(client, project_id).await {
            ClusterSelectionResult::Selected(id) => Some(id),
            ClusterSelectionResult::NoClusters => {
                println!(
                    "\n{}",
                    "No Kubernetes clusters available. Please provision a cluster first.".red()
                );
                return EnvironmentCreationResult::Cancelled;
            }
            ClusterSelectionResult::Cancelled => {
                return EnvironmentCreationResult::Cancelled;
            }
            ClusterSelectionResult::Error(e) => {
                return EnvironmentCreationResult::Error(e);
            }
        }
    } else {
        None
    };

    // Step 4 (Cloud Runner only): Optional provider region defaults
    let provider_regions = if env_type == EnvironmentType::Cloud {
        select_provider_regions()
    } else {
        None
    };

    // Create the environment via API
    println!("\n{}", "Creating environment...".dimmed());

    match client
        .create_environment(
            project_id,
            &name,
            env_type.as_str(),
            cluster_id.as_deref(),
            provider_regions.as_ref(),
        )
        .await
    {
        Ok(env) => {
            println!(
                "\n{} Environment {} created successfully!",
                "".green().bold(),
                env.name.bold()
            );
            println!("  ID: {}", env.id.dimmed());
            println!("  Type: {}", env.environment_type);
            if let Some(cid) = &env.cluster_id {
                println!("  Cluster: {}", cid);
            }
            EnvironmentCreationResult::Created(env)
        }
        Err(e) => EnvironmentCreationResult::Error(format!("Failed to create environment: {}", e)),
    }
}

/// Result of cluster selection
enum ClusterSelectionResult {
    Selected(String),
    NoClusters,
    Cancelled,
    Error(String),
}

/// Select a Kubernetes cluster from available clusters
async fn select_cluster_for_env(
    client: &PlatformApiClient,
    project_id: &str,
) -> ClusterSelectionResult {
    display_step_header(
        3,
        "Select Cluster",
        "Choose a Kubernetes cluster for this environment.",
    );

    // Get available clusters
    let clusters: Vec<ClusterSummary> =
        match get_available_clusters_for_project(client, project_id).await {
            Ok(c) => c,
            Err(e) => return ClusterSelectionResult::Error(e),
        };

    if clusters.is_empty() {
        return ClusterSelectionResult::NoClusters;
    }

    // Build options
    let options: Vec<String> = clusters
        .iter()
        .map(|c| {
            let health = if c.is_healthy {
                "healthy".green()
            } else {
                "unhealthy".red()
            };
            format!("{} ({}) - {}", c.name.bold(), c.region.dimmed(), health)
        })
        .collect();

    let selection = Select::new("Select cluster:", options.clone())
        .with_render_config(wizard_render_config())
        .with_help_message("Choose the cluster to deploy to")
        .prompt();

    match selection {
        Ok(answer) => {
            // Find the selected cluster by matching the name at the start
            let selected_name = answer.split(" (").next().unwrap_or("");
            if let Some(cluster) = clusters.iter().find(|c| c.name == selected_name) {
                println!("\n{} Selected: {}", "".green(), cluster.name.bold());
                ClusterSelectionResult::Selected(cluster.id.clone())
            } else {
                ClusterSelectionResult::Error("Failed to match selected cluster".to_string())
            }
        }
        Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
            println!("\n{}", "Wizard cancelled.".dimmed());
            ClusterSelectionResult::Cancelled
        }
        Err(e) => ClusterSelectionResult::Error(format!("Selection error: {}", e)),
    }
}

/// Get available clusters from all connected providers for a project
async fn get_available_clusters_for_project(
    client: &PlatformApiClient,
    project_id: &str,
) -> Result<Vec<ClusterSummary>, String> {
    // Get provider deployment statuses which include cluster info
    let statuses = get_provider_deployment_statuses(client, project_id)
        .await
        .map_err(|e| format!("Failed to get provider statuses: {}", e))?;

    // Collect all clusters from connected providers
    let mut all_clusters = Vec::new();
    for status in statuses {
        if status.is_connected {
            all_clusters.extend(status.clusters);
        }
    }

    Ok(all_clusters)
}

/// Interactive provider region selection for Cloud Runner environments
///
/// Asks user which providers they want to set default regions for,
/// then presents region list per provider.
fn select_provider_regions() -> Option<HashMap<String, String>> {
    display_step_header(
        4,
        "Provider Regions (Optional)",
        "Set default regions for each cloud provider. Press Esc to skip.",
    );

    let available_providers = [
        ("GCP", CloudProvider::Gcp),
        ("Hetzner", CloudProvider::Hetzner),
        ("Azure", CloudProvider::Azure),
    ];

    let provider_labels: Vec<String> = available_providers
        .iter()
        .map(|(label, _)| label.to_string())
        .collect();

    let selected = match MultiSelect::new(
        "Select providers to set default regions for:",
        provider_labels,
    )
    .with_render_config(wizard_render_config())
    .with_help_message("Space to select, Enter to confirm, Esc to skip")
    .prompt()
    {
        Ok(s) if !s.is_empty() => s,
        _ => return None,
    };

    let mut regions = HashMap::new();

    for provider_label in &selected {
        let (_, provider) = available_providers
            .iter()
            .find(|(label, _)| label == provider_label)
            .unwrap();

        let provider_regions = get_regions_for_provider(provider);
        let default_region = get_default_region(provider);

        if provider_regions.is_empty() {
            // For providers with dynamic regions (Hetzner), use a text input
            let region = match Text::new(&format!("{} region:", provider_label))
                .with_default(default_region)
                .with_render_config(wizard_render_config())
                .prompt()
            {
                Ok(r) => r,
                Err(_) => continue,
            };
            regions.insert(provider.as_str().to_string(), region);
        } else {
            let region_labels: Vec<String> = provider_regions
                .iter()
                .map(|r| format!("{} - {} ({})", r.id, r.name, r.location))
                .collect();

            let default_idx = provider_regions
                .iter()
                .position(|r| r.id == default_region)
                .unwrap_or(0);

            let region = match Select::new(&format!("{} region:", provider_label), region_labels)
                .with_render_config(wizard_render_config())
                .with_starting_cursor(default_idx)
                .prompt()
            {
                Ok(r) => {
                    // Extract region ID from the display string (before first " - ")
                    r.split(" - ").next().unwrap_or(default_region).to_string()
                }
                Err(_) => continue,
            };
            regions.insert(provider.as_str().to_string(), region);
        }
    }

    if regions.is_empty() {
        None
    } else {
        println!("\n{} Provider regions configured:", "".green());
        for (provider, region) in &regions {
            println!("  {}: {}", provider, region.bold());
        }
        Some(regions)
    }
}

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

    #[test]
    fn test_environment_creation_result_variants() {
        let created = EnvironmentCreationResult::Created(Environment {
            id: "env-1".to_string(),
            name: "test".to_string(),
            project_id: "proj-1".to_string(),
            environment_type: "cloud".to_string(),
            cluster_id: None,
            namespace: None,
            description: None,
            is_active: true,
            created_at: None,
            updated_at: None,
            provider_regions: None,
        });
        assert!(matches!(created, EnvironmentCreationResult::Created(_)));

        let cancelled = EnvironmentCreationResult::Cancelled;
        assert!(matches!(cancelled, EnvironmentCreationResult::Cancelled));

        let error = EnvironmentCreationResult::Error("test error".to_string());
        assert!(matches!(error, EnvironmentCreationResult::Error(_)));
    }

    #[test]
    fn test_environment_type_as_str() {
        assert_eq!(EnvironmentType::Cluster.as_str(), "cluster");
        assert_eq!(EnvironmentType::Cloud.as_str(), "cloud");
    }

    #[test]
    fn test_environment_type_display_name() {
        assert_eq!(EnvironmentType::Cluster.display_name(), "Kubernetes");
        assert_eq!(EnvironmentType::Cloud.display_name(), "Cloud Runner");
    }
}