oxyde_cloud_cli/commands/
deploy_config.rs

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