feat(cli): add better error message, support stdin via -i - or just no args (#1660)

This commit is contained in:
Kalvin C
2025-03-12 19:27:43 -07:00
committed by GitHub
parent 4537264387
commit 2135d8b596

View File

@@ -12,7 +12,7 @@ use goose_cli::commands::session::handle_session_list;
use goose_cli::logging::setup_logging; use goose_cli::logging::setup_logging;
use goose_cli::session; use goose_cli::session;
use goose_cli::session::build_session; use goose_cli::session::build_session;
use std::io::{self, Read}; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Parser)] #[derive(Parser)]
@@ -147,7 +147,7 @@ enum Command {
short, short,
long, long,
value_name = "FILE", value_name = "FILE",
help = "Path to instruction file containing commands", help = "Path to instruction file containing commands. Use - for stdin.",
conflicts_with = "input_text" conflicts_with = "input_text"
)] )]
instructions: Option<String>, instructions: Option<String>,
@@ -360,24 +360,28 @@ async fn main() -> Result<()> {
extension, extension,
builtin, builtin,
}) => { }) => {
// Validate that we have some input source let contents = match (instructions, input_text) {
if instructions.is_none() && input_text.is_none() { (Some(file), _) if file == "-" => {
eprintln!("Error: Must provide either --instructions or --text"); let mut stdin = String::new();
std::process::exit(1); std::io::stdin()
} .read_to_string(&mut stdin)
.expect("Failed to read from stdin");
let contents = if let Some(file_name) = instructions { stdin
let file_path = std::path::Path::new(&file_name); }
std::fs::read_to_string(file_path).expect("Failed to read the instruction file") (Some(file), _) => std::fs::read_to_string(&file).unwrap_or_else(|err| {
} else if let Some(input_text) = input_text { eprintln!(
input_text "Instruction file not found — did you mean to use goose run --text?\n{}",
} else { err
let mut stdin = String::new(); );
io::stdin() std::process::exit(1);
.read_to_string(&mut stdin) }),
.expect("Failed to read from stdin"); (None, Some(text)) => text,
stdin (None, None) => {
eprintln!("Error: Must provide either --instructions (-i) or --text (-t). Use -i - for stdin.");
std::process::exit(1);
}
}; };
let mut session = build_session( let mut session = build_session(
identifier.map(extract_identifier), identifier.map(extract_identifier),
resume, resume,
@@ -386,6 +390,7 @@ async fn main() -> Result<()> {
debug, debug,
) )
.await; .await;
setup_logging( setup_logging(
session.session_file().file_stem().and_then(|s| s.to_str()), session.session_file().file_stem().and_then(|s| s.to_str()),
None, None,
@@ -396,6 +401,7 @@ async fn main() -> Result<()> {
} else { } else {
session.headless(contents).await?; session.headless(contents).await?;
} }
return Ok(()); return Ok(());
} }
Some(Command::Agents(cmd)) => { Some(Command::Agents(cmd)) => {