Skip to content

Commit

Permalink
misc: small fix or general refactoring i did not bother commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Jan 16, 2024
1 parent 5fe6c32 commit 5601c06
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
29 changes: 29 additions & 0 deletions src/recipe/interactive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::io::{stdout, Write};

use crate::session::Error;

pub(crate) fn read_arg_from_user(var_name: &str, default: Option<&str>) -> Result<String, Error> {
let prompt = if let Some(def) = default {
format!("{} ({}): ", var_name, def)
} else {
format!("{}: ", var_name)
};

loop {
print!("recipe.{}", &prompt);
let _ = stdout().flush();

let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.map_err(|e| e.to_string())?;

let input = input.trim();
if !input.is_empty() {
return Ok(input.to_owned());
} else if let Some(def) = default {
return Ok(def.to_owned());
}
// keep going
}
}
39 changes: 4 additions & 35 deletions src/recipe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::session::Error;
use self::context::Context;

mod context;
mod interactive;

const ARG_EXPRESSION_ERROR: &str =
"argument expression must be in the form of {$name} or {$name or default_value}";
Expand Down Expand Up @@ -60,57 +61,25 @@ impl Recipe {
Ok(recipe)
}

fn read_arg_from_user(&self, var_name: &str, default: Option<&str>) -> Result<String, Error> {
use std::io::{stdout, Write};

let prompt = if let Some(def) = default {
format!("{} ({}): ", var_name, def)
} else {
format!("{}: ", var_name)
};

loop {
print!("recipe.{}", &prompt);
let _ = stdout().flush();

let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.map_err(|e| e.to_string())?;

let input = input.trim();
if !input.is_empty() {
return Ok(input.to_owned());
} else if let Some(def) = default {
return Ok(def.to_owned());
}
// keep going
}
}

fn parse_arg(&self, expr: &str, ctx: &mut Context) -> Result<String, Error> {
let mut parsed = expr.to_owned();

for cap in ARG_VALUE_PARSER.captures_iter(expr) {
let expr: &str = cap.get(0).ok_or(ARG_EXPRESSION_ERROR)?.as_str();
let var_name = cap.get(1).ok_or(ARG_EXPRESSION_ERROR)?.as_str();
let var_default = if let Some(m) = cap.get(3) {
Some(m.as_str())
} else {
None
};
let var_default = cap.get(3).map(|m| m.as_str());

let var_value = if let Some(val) = ctx.get(var_name) {
// get variable from context
val.to_owned()
} else if self.interactive {
// get from user if interactive
self.read_arg_from_user(var_name, var_default)?
interactive::read_arg_from_user(var_name, var_default)?
} else if let Some(def) = var_default {
// get variable from default if provided
def.to_owned()
} else {
return Err(format!("no '{}' specified for recipe", var_name));
return Err(format!("no '{}' variable specified for recipe", var_name));
};

// cache value in context
Expand Down
1 change: 1 addition & 0 deletions test-servers/joomla-docker
Submodule joomla-docker added at 4da79f

0 comments on commit 5601c06

Please sign in to comment.