use clap::Subcommand;
use clove_lang::cli::{self, CheckOptions, CheckResult};
use std::io::{self, Read};
#[derive(Subcommand)]
pub enum CloveCommands {
Check {
query: String,
#[arg(short, long)]
input: Option<String>,
#[arg(short, long)]
pretty: bool,
#[arg(long)]
syntax_only: bool,
},
Docs,
Doc {
category: String,
},
Onboard,
}
pub fn run(command: CloveCommands) -> Result<(), Box<dyn std::error::Error>> {
match command {
CloveCommands::Check {
query,
input,
pretty,
syntax_only,
} => {
let input = resolve_input(input)?;
let options = CheckOptions {
query,
input,
pretty,
syntax_only,
};
match cli::execute_check(&options)? {
CheckResult::SyntaxValid => println!("Syntax is valid"),
CheckResult::Success(output) => {
let json = if pretty {
serde_json::to_string_pretty(&output)?
} else {
serde_json::to_string(&output)?
};
println!("{}", json);
}
}
}
CloveCommands::Docs => print!("{}", cli::get_docs_overview()),
CloveCommands::Doc { category } => print!("{}", cli::get_doc_category(&category)?),
CloveCommands::Onboard => print!("{}", cli::get_onboarding_content()),
}
Ok(())
}
fn resolve_input(input: Option<String>) -> Result<Option<String>, io::Error> {
match input {
Some(s) => Ok(Some(s)),
None if !atty::is(atty::Stream::Stdin) => {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer)?;
Ok(Some(buffer))
}
None => Ok(None),
}
}