oxyde_cloud_cli/commands/
deploy_config.rs

1use crate::commands::TEMPLATES;
2use anyhow::{Context as AnyhowContext, Result};
3use cliclack::{intro, outro, select};
4use std::fmt::Formatter;
5use std::fs::create_dir_all;
6use tera::Context;
7
8#[derive(Debug, Clone, Copy, Eq, PartialEq)]
9pub enum DeployConfig {
10    None,
11    GitHub,
12    // TODO : GitLab, ...
13}
14
15#[derive(Debug, Clone, Copy, Eq, PartialEq)]
16enum RustToolchain {
17    Stable,
18    Nightly,
19}
20
21impl std::fmt::Display for RustToolchain {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        match self {
24            RustToolchain::Stable => write!(f, "stable"),
25            RustToolchain::Nightly => write!(f, "nightly"),
26        }
27    }
28}
29
30pub fn init_deploy_config() -> Result<()> {
31    intro("Oxyde Cloud deployment init").context("Failed to show deployment config intro")?;
32
33    let deploy_config = select("How do you want to deploy?")
34        .item(DeployConfig::GitHub, "GitHub Workflow", "")
35        .item(DeployConfig::None, "None", "Setup deployment later")
36        .initial_value(DeployConfig::GitHub)
37        .interact()
38        .context("Failed to get deployment config selection")?;
39
40    match deploy_config {
41        DeployConfig::GitHub => {
42            let toolchain = select_rust_toolchain().context("Failed to select Rust toolchain")?;
43            // let sqlx = select_sqlx()?;
44
45            let mut context = Context::new();
46            context.insert("toolchain", &toolchain.to_string());
47            // context.insert("sqlx", &sqlx.to_string());
48            let config_str = TEMPLATES
49                .render("github-workflow.yml", &context)
50                .context("Failed to render GitHub workflow template")?;
51
52            create_dir_all(".github/workflows")
53                .context("Failed to create .github/workflows directory")?;
54            std::fs::write(".github/workflows/oxyde-cloud-deploy.yml", config_str)
55                .context("Failed to write GitHub workflow file")?;
56
57            outro("Created .github/workflows/oxyde-cloud-deploy.yml")
58                .context("Failed to show success message")?;
59        }
60        DeployConfig::None => {
61            outro("You can setup deployment yourself manually or call `leco deploy-config` at any time")
62                .context("Failed to show manual setup message")?;
63        }
64    }
65
66    Ok(())
67}
68
69fn select_rust_toolchain() -> Result<RustToolchain> {
70    // TODO : check for rust-toolchain.toml
71
72    let toolchain = select("Select a Rust toolchain:")
73        .item(RustToolchain::Stable, "stable", "")
74        .item(RustToolchain::Nightly, "nightly", "")
75        .initial_value(RustToolchain::Stable)
76        .interact()
77        .context("Failed to get Rust toolchain selection")?;
78
79    Ok(toolchain)
80}