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