feat: Make application async
This commit is contained in:
+25
-21
@@ -1,14 +1,16 @@
|
||||
mod cli;
|
||||
|
||||
use std::process::Command;
|
||||
use std::pin::pin;
|
||||
|
||||
use clap::{CommandFactory, Parser};
|
||||
use clap_complete::{Shell, generate as generate_complete};
|
||||
use crete::cluster::Cluster;
|
||||
use crete::environment::PathEnvironment;
|
||||
use crete::{get_configs_path, get_repo_path, set_repo_path};
|
||||
use futures::StreamExt;
|
||||
use minijinja::context;
|
||||
use thiserror::Error;
|
||||
use tokio::process::Command;
|
||||
|
||||
use crate::cli::{Cli, Commands, GlobalOpts};
|
||||
|
||||
@@ -18,8 +20,8 @@ enum Error {
|
||||
NoClustersFound,
|
||||
}
|
||||
|
||||
fn run_command(mut command: Command) {
|
||||
let output = command.output().unwrap();
|
||||
async fn run_command(mut command: Command) {
|
||||
let output = command.output().await.unwrap();
|
||||
if !output.stdout.is_empty() {
|
||||
println!("{}", String::from_utf8_lossy(&output.stdout).trim_end());
|
||||
}
|
||||
@@ -28,24 +30,25 @@ fn run_command(mut command: Command) {
|
||||
}
|
||||
}
|
||||
|
||||
fn generate(opts: &GlobalOpts) -> Result<(), Error> {
|
||||
async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
|
||||
set_repo_path(&opts.repo);
|
||||
|
||||
let clusters = Cluster::get_all();
|
||||
let clusters = Cluster::get_all().await;
|
||||
|
||||
if clusters.is_empty() {
|
||||
return Err(Error::NoClustersFound);
|
||||
}
|
||||
|
||||
let path = opts.repo.join("rendered");
|
||||
if path.exists() {
|
||||
std::fs::remove_dir_all(&path).unwrap();
|
||||
}
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
// We don't really care if this fails, as that likely mean that the directory did not exist
|
||||
tokio::fs::remove_dir_all(&path).await.ok();
|
||||
tokio::fs::create_dir_all(&path).await.unwrap();
|
||||
|
||||
// Render templates
|
||||
let template_env = PathEnvironment::new(&get_repo_path().join("templates"));
|
||||
for template_name in &template_env {
|
||||
|
||||
let mut stream = pin!(template_env.files());
|
||||
while let Some(template_name) = stream.next().await {
|
||||
let template = template_env.get_template(&template_name).unwrap();
|
||||
|
||||
let content = template
|
||||
@@ -55,35 +58,36 @@ fn generate(opts: &GlobalOpts) -> Result<(), Error> {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
std::fs::write(path.join(template_name), content).unwrap();
|
||||
tokio::fs::write(path.join(template_name), content)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Remove existing config files and create output directory
|
||||
let path = get_configs_path();
|
||||
if path.exists() {
|
||||
std::fs::remove_dir_all(&path).unwrap();
|
||||
}
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
tokio::fs::remove_dir_all(&path).await.ok();
|
||||
tokio::fs::create_dir_all(&path).await.unwrap();
|
||||
|
||||
// Generate config files
|
||||
for cluster in clusters {
|
||||
for node in cluster.nodes() {
|
||||
run_command(node.talosctl_gen_config_command(&cluster));
|
||||
run_command(node.talosctl_gen_config_command(&cluster)).await;
|
||||
}
|
||||
|
||||
run_command(cluster.talosctl_gen_config_command());
|
||||
run_command(cluster.talosctl_add_endpoint_command());
|
||||
run_command(cluster.talosctl_merge_command());
|
||||
run_command(cluster.talosctl_gen_config_command()).await;
|
||||
run_command(cluster.talosctl_add_endpoint_command()).await;
|
||||
run_command(cluster.talosctl_merge_command()).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Commands::Generate => generate(&cli.global_opts)?,
|
||||
Commands::Generate => generate(&cli.global_opts).await?,
|
||||
Commands::ShellCompletions => generate_complete(
|
||||
Shell::from_env().unwrap_or(Shell::Bash),
|
||||
&mut Cli::command(),
|
||||
|
||||
Reference in New Issue
Block a user