oxyde_cloud_cli/commands/init/
mod.rs

1use anyhow::{Context as AnyhowContext, Result};
2use cliclack::log::remark;
3use cliclack::{intro, outro};
4use std::path::PathBuf;
5use tera::Context;
6
7mod app_slug;
8mod team;
9
10use crate::commands::TEMPLATES;
11use crate::commands::deploy_config::init_deploy_config;
12use crate::commands::init::app_slug::input_app_slug;
13use crate::commands::init::team::input_team_slug;
14
15pub async fn init(
16    app_slug: Option<String>,
17    team_slug: Option<String>,
18    config_file: PathBuf,
19) -> Result<()> {
20    intro("Oxyde Cloud app init").context("Failed to show init intro")?;
21
22    let team_slug = match team_slug {
23        Some(team_slug) => {
24            remark(format!("Team provided: {team_slug}"))
25                .context("Failed to show team slug remark")?;
26            team_slug
27        }
28        None => input_team_slug().await.context("Failed to get team slug")?,
29    };
30
31    let app_slug = match app_slug {
32        Some(slug) => {
33            remark(format!("App slug provided: {slug}"))
34                .context("Failed to show app slug remark")?;
35            slug
36        }
37        None => input_app_slug(&team_slug)
38            .await
39            .context("Failed to get app slug")?,
40    };
41
42    let mut context = Context::new();
43    context.insert("app_slug", &app_slug);
44    let config_str = TEMPLATES
45        .render("oxyde-cloud.toml", &context)
46        .context("Failed to render config template")?;
47
48    std::fs::write(&config_file, config_str)
49        .with_context(|| format!("Failed to write config file: {}", config_file.display()))?;
50
51    outro(format!("Created config file: {}\n", config_file.display()))
52        .context("Failed to show config creation message")?;
53
54    init_deploy_config().context("Failed to initialize deploy config")?;
55
56    Ok(())
57}