oxyde_cloud_cli/commands/
deploy_config.rs1use 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 }
29
30#[derive(Debug, Clone, Copy, Eq, PartialEq)]
31enum RustToolchain {
32 Stable,
33 Nightly,
34}
35
36#[derive(Debug, Clone, Copy, Eq, PartialEq)]
37enum Sqlx {
38 Postgres,
39 None,
40}
41
42impl std::fmt::Display for RustToolchain {
43 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44 match self {
45 RustToolchain::Stable => write!(f, "stable"),
46 RustToolchain::Nightly => write!(f, "nightly"),
47 }
48 }
49}
50
51impl std::fmt::Display for Sqlx {
52 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53 match self {
54 Sqlx::None => write!(f, "none"),
55 Sqlx::Postgres => write!(f, "postgres"),
56 }
57 }
58}
59
60pub fn init_deploy_config() -> Result<(), Error> {
61 intro("Oxyde Cloud deployment init")?;
62
63 let deploy_config = select("How do you want to deploy?")
64 .item(DeployConfig::GitHub, "GitHub Workflow", "")
65 .item(DeployConfig::None, "None", "Setup deployment later")
66 .initial_value(DeployConfig::GitHub)
67 .interact()?;
68
69 match deploy_config {
70 DeployConfig::GitHub => {
71 let toolchain = select_rust_toolchain()?;
72 let mut context = Context::new();
75 context.insert("toolchain", &toolchain.to_string());
76 let config_str = TEMPLATES.render("github-workflow.yml", &context)?;
78
79 create_dir_all(".github/workflows")?;
80 std::fs::write(".github/workflows/oxyde-cloud-deploy.yml", config_str)?;
81
82 outro("Created .github/workflows/oxyde-cloud-deploy.yml")?;
83 }
84 DeployConfig::None => {
85 outro("You can setup deployment yourself manually or call `leco deploy-config` at any time")?;
86 }
87 }
88
89 Ok(())
90}
91
92fn select_rust_toolchain() -> Result<RustToolchain, Error> {
93 let toolchain = select("Select a Rust toolchain:")
96 .item(RustToolchain::Stable, "stable", "")
97 .item(RustToolchain::Nightly, "nightly", "")
98 .initial_value(RustToolchain::Stable)
99 .interact()?;
100
101 Ok(toolchain)
102}
103fn select_sqlx() -> Result<Sqlx, Error> {
104 let sqlx = select("Select your sqlx database server:")
105 .item(Sqlx::None, "none", "")
106 .item(Sqlx::Postgres, "postgres", "")
107 .initial_value(Sqlx::None)
108 .interact()?;
109
110 Ok(sqlx)
111}