You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
distant/src/cli/commands/generate.rs

44 lines
1.3 KiB
Rust

use std::{fs, io};
use anyhow::Context;
use clap::CommandFactory;
use clap_complete::generate as clap_generate;
use crate::options::{Config, GenerateSubcommand};
use crate::{CliResult, Options};
pub fn run(cmd: GenerateSubcommand) -> CliResult {
let rt = tokio::runtime::Runtime::new().context("Failed to start up runtime")?;
rt.block_on(async_run(cmd))
}
async fn async_run(cmd: GenerateSubcommand) -> CliResult {
match cmd {
GenerateSubcommand::Config { file } => tokio::fs::write(file, Config::default_raw_str())
.await
.context("Failed to write default config to {file:?}")?,
GenerateSubcommand::Completion { file, shell } => {
let name = "distant";
let mut cmd = Options::command();
if let Some(path) = file {
clap_generate(
shell,
&mut cmd,
name,
&mut fs::OpenOptions::new()
.create(true)
.write(true)
.open(&path)
.with_context(|| format!("Failed to open {path:?}"))?,
)
} else {
clap_generate(shell, &mut cmd, name, &mut io::stdout())
}
}
}
Ok(())
}