Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support stdin and text input in cli #335

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions crates/goose-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ mod systems;

use anyhow::Result;
use clap::{Parser, Subcommand};
use commands::configure::handle_configure;
use commands::session::build_session;
use commands::version::print_version;
use std::io::{self, Read};

use crate::systems::system_handler::{add_system, remove_system};
use commands::configure::handle_configure;

use commands::version::print_version;

#[derive(Parser)]
#[command(author, about, long_about = None)]
Expand Down Expand Up @@ -82,6 +82,8 @@ enum Command {
Run {
#[arg(short, long)]
instructions: Option<String>,
#[arg(short = 't', long = "text")]
input_text: Option<String>,
#[arg(short, long)]
profile: Option<String>,
#[arg(short, long)]
Expand Down Expand Up @@ -145,15 +147,23 @@ async fn main() -> Result<()> {
}
Some(Command::Run {
instructions,
input_text,
profile,
session,
resume,
}) => {
let file_name =
instructions.expect("Instruction file is required (--instructions <file_path>)");
let file_path = std::path::Path::new(&file_name);
let contents = std::fs::read_to_string(file_path).expect("Failed to read the file");

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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this works great when used in a pipe, but if someone does not use a pipe, pass --text or pass --instructions, then it sites there reading input until you hit CTRL-D or CTRL-C which is confusing.

Is there a way to detect not piping and error maybe?

This is also how old goose worked in the same setting, so maybe we can skip solving this for now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it sounds easy, but i think the way to do it would be to spawn a thread and then poll the channel. I'll skip this for now.

https://stackoverflow.com/questions/30012995/how-can-i-read-non-blocking-from-stdin

stdin
};
let mut session = build_session(session, profile, resume);
let _ = session.headless_start(contents.clone()).await;
return Ok(());
Expand Down
Loading