Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum Builtin {
Vars,
Requests,
Clear,
Timeout(u64),
}

pub enum ControlFlow {
Expand Down Expand Up @@ -97,6 +98,11 @@ pub fn handle(
}
}

Builtin::Timeout(secs) => {
ctx.set_timeout(secs);
println!("Request timeout set to {secs}s");
}

Builtin::Clear => {
ctx.clear();
println!("Session state cleared");
Expand Down
3 changes: 2 additions & 1 deletion src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ pub fn execute(req: Request, ctx: &ShellState) -> Result<String, String> {

let base_url = ctx.get_base_url();
let global_headers = ctx.get_headers();
let response = fetch(&req, base_url, global_headers);
let timeout_secs = ctx.get_timeout();
let response = fetch(&req, base_url, global_headers, timeout_secs);

match response {
Ok((res, duration)) => Ok(display_response(res, duration)),
Expand Down
3 changes: 3 additions & 0 deletions src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn help_text() -> String {
{}:
{} Show help
{} Show version
{} Set request timeout
{}
{}:
{} <path>
Expand Down Expand Up @@ -37,6 +38,7 @@ pub fn help_text() -> String {
headers
history
rerun <id>
timeout <seconds>
clear
help
exit
Expand All @@ -48,6 +50,7 @@ pub fn help_text() -> String {
"Options".yellow().bold(),
"--help, -h".green().bold(),
"--version, -v".green().bold(),
"--timeout <seconds>".green().bold(),
"─".repeat(50).dimmed(),
"Requests".yellow().bold(),
"Method".green().bold(),
Expand Down
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ fn history_path() -> PathBuf {
home.join(".reqsh_history")
}

fn shell_loop() {
fn run_repl(ctx: &mut ShellState) {
let config = Config::builder()
.history_ignore_space(true)
.completion_type(CompletionType::List)
.edit_mode(EditMode::Vi)
.build();

let hist_path = history_path();
let mut ctx = ShellState::new();
let mut rl = Editor::with_config(config).unwrap();
rl.set_helper(Some(ShellHelper));
rl.load_history(&hist_path).unwrap_or_default();
Expand All @@ -54,7 +53,7 @@ fn shell_loop() {

match parse(raw) {
Ok(parsed) => match parsed {
Parsed::Builtin(cmd) => match handle(cmd, &mut ctx, rl.history()) {
Parsed::Builtin(cmd) => match handle(cmd, ctx, rl.history()) {
Ok(ControlFlow::Continue) => {}
Ok(ControlFlow::Exit) => {
break;
Expand All @@ -66,7 +65,7 @@ fn shell_loop() {

Parsed::Request(req) => {
ctx.set_last_request(req.clone());
match execute(req, &ctx) {
match execute(req, ctx) {
Ok(res) => {
println!("{}", res);
}
Expand Down Expand Up @@ -150,7 +149,8 @@ fn main() {

match args.as_slice() {
[] => {
shell_loop();
let mut ctx = ShellState::new();
run_repl(&mut ctx);
}

[arg] if arg == "--help" || arg == "-h" => {
Expand All @@ -161,6 +161,16 @@ fn main() {
println!("reqsh {}", VERSION);
}

[arg, value] if arg == "--timeout" => {
let secs: u64 = value.parse().unwrap_or_else(|_| {
eprintln!("Invalid timeout: {value}");
std::process::exit(1);
});
let mut ctx = ShellState::new();
ctx.set_timeout(secs);
run_repl(&mut ctx);
}

[unknown] => {
eprintln!("Unknown argument: {}", unknown);
eprintln!("Try 'reqsh --help'");
Expand Down
12 changes: 11 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn parse(input: String) -> Result<Parsed, String> {
}

"base" | "set" | "unset" | "header" | "headers" | "vars" | "requests" | "save" | "run"
| "help" | "history" | "rerun" | "clear" => {
| "help" | "history" | "rerun" | "clear" | "timeout" => {
let result = parse_builtin(input)?;
Ok(Parsed::Builtin(result))
}
Expand Down Expand Up @@ -175,6 +175,16 @@ fn parse_builtin(line: String) -> Result<Builtin, String> {
}
"help" => Ok(Builtin::Help),
"clear" => Ok(Builtin::Clear),
"timeout" => {
if tokens.len() != 2 {
Err("usage: timeout <seconds>".to_string())
} else {
let secs = tokens[1]
.parse::<u64>()
.map_err(|e| format!("invalid timeout: {e}"))?;
Ok(Builtin::Timeout(secs))
}
}
"history" => Ok(Builtin::History),
"rerun" => {
if tokens.len() != 2 {
Expand Down
12 changes: 11 additions & 1 deletion src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ pub fn fetch(
request: &Request,
base_url: Option<&str>,
global_headers: &HashMap<String, String>,
timeout_secs: Option<u64>,
) -> Result<(Response, Duration), String> {
// Client
let client = Client::new();
let client = match timeout_secs {
Some(secs) => Client::builder()
.timeout(Duration::from_secs(secs))
.build()
.map_err(|e| format!("failed to build client: {e}"))?,
None => Client::builder()
.timeout(Duration::from_secs(30))
.build()
.map_err(|e| format!("failed to build client: {e}"))?,
};

// Url Constructor
let full_url = if request.path.starts_with("http://") || request.path.starts_with("https://") {
Expand Down
11 changes: 11 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct ShellState {
variables: HashMap<String, String>,
last_request: Option<Request>,
saved_requests: HashMap<String, Request>,
timeout_secs: Option<u64>,
}

impl Default for ShellState {
Expand All @@ -24,6 +25,7 @@ impl ShellState {
variables: HashMap::new(),
last_request: None,
saved_requests: HashMap::new(),
timeout_secs: None,
}
}

Expand Down Expand Up @@ -85,12 +87,21 @@ impl ShellState {
self.variables.remove(name);
}

pub fn get_timeout(&self) -> Option<u64> {
self.timeout_secs
}

pub fn set_timeout(&mut self, secs: u64) {
self.timeout_secs = Some(secs);
}

pub fn clear(&mut self) {
self.base_url = None;
self.headers.clear();
self.variables.clear();
self.last_request = None;
self.saved_requests.clear();
self.timeout_secs = None;
}
}

Expand Down
Loading