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

Use defaults as answers in headless mode #10

Merged
merged 3 commits into from
Aug 26, 2021
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
60 changes: 33 additions & 27 deletions archetect-core/src/actions/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 22,22 @@ pub fn populate_context(
if let Some(value) = answer.value() {
// If there is an answer for this variable, it has an explicit value, and it is an acceptable answer,
// use that.
if insert_answered_variable(archetect, identifier, value, &variable_info, context)? {
continue;
match insert_answered_variable(archetect, identifier, value, &variable_info, context)? {
None => continue,
Some(warning) => warn!("{}", warning),
}
}
} else {
if let Some(value) = variable_info.value() {
// If no answer was provided, there is an explicit value on the variable definition, and it is an
// acceptable value, use that.
if insert_answered_variable(archetect, identifier, value, &variable_info, context)? {
continue;
match insert_answered_variable(archetect, identifier, value, &variable_info, context)? {
None => continue,
Some(warning) => warn!("{}", warning),
}
}
}

trace!("Attempting to satisfy {} ({:?})", identifier, variable_info);


// No answer or explict value provided. Check to see if we're in headless mode before prompting for a value.
if archetect.headless() {
return Err(ArchetectError::HeadlessMissingAnswer(identifier.to_owned()));
}
// If we've made it this far, there was not an acceptable answer or explicit value provided. We need to prompt
// for a valid value.
let mut prompt = if let Some(prompt) = variable_info.prompt() {
format!("{} ", archetect.render_string(prompt.trim(), context)?)
} else {
format!("{}: ", identifier)
};

// Determine if a default can be provided.
let default = if let Some(answer) = answers.get(identifier) {
if let Some(default) = answer.default() {
Expand All @@ -66,6 53,26 @@ pub fn populate_context(
None
};

// No answer or explict value provided. Check to see if we're in headless mode before prompting for a value.
if archetect.headless() {
if let Some(default) = default {
match insert_answered_variable(archetect, identifier, &default, &variable_info, context)? {
None => continue,
Some(message) => {
return Err(ArchetectError::HeadlessInvalidDefault { identifier: identifier.to_owned(), default, message })
},
}
}
return Err(ArchetectError::HeadlessMissingAnswer(identifier.to_owned()));
}
// If we've made it this far, there was not an acceptable answer or explicit value provided. We need to prompt
// for a valid value.
let mut prompt = if let Some(prompt) = variable_info.prompt() {
format!("{} ", archetect.render_string(prompt.trim(), context)?)
} else {
format!("{}: ", identifier)
};

let value = match variable_info.variable_type() {
VariableType::Enum(values) => prompt_for_enum(&mut prompt, &values, &default),
VariableType::Bool => prompt_for_bool(&mut prompt, &default),
Expand All @@ -83,7 90,7 @@ pub fn populate_context(
}

fn insert_answered_variable(archetect: &mut Archetect, identifier: &str, value: &str, variable_info: &VariableInfo,
context: &mut Context) -> Result<bool, ArchetectError> {
context: &mut Context) -> Result<Option<String>, ArchetectError> {

trace!("Setting variable answer {:?}={:?}", identifier, value);

Expand All @@ -93,7 100,7 @@ fn insert_answered_variable(archetect: &mut Archetect, identifier: &str, value:
// prompt the user for a valid answer
if options.contains(&value.to_owned()) {
context.insert(identifier, &archetect.render_string(value, context)?);
return Ok(true);
return Ok(None);
}
}
VariableType::Bool => {
Expand All @@ -106,35 113,34 @@ fn insert_answered_variable(archetect: &mut Archetect, identifier: &str, value:
_ => false,
};
context.insert(identifier, &value);
return Ok(true);
return Ok(None);
}
}
VariableType::Int => {
// If the provided answer parses to an integer, use that; otherwise, we'll have to prompt the
// user for a proper integer
if let Ok(value) = &archetect.render_string(value, context)?.parse::<i64>() {
context.insert(identifier, &value);
return Ok(true);
return Ok(None);
} else {
trace!("'{}' failed to parse as an int", value);
}
}
VariableType::String => {
context.insert(identifier, &archetect.render_string(value, context)?);
return Ok(true);
return Ok(None);
}
VariableType::Array => {
let values = convert_to_list(archetect, context, value)?;
if !values.is_empty() || !variable_info.required() {
trace!("Inserting {}={:?}", identifier, values);
context.insert(identifier, &Value::Array(values));
return Ok(true);
return Ok(None);
}
}
}

warn!("'{:?}' is not a valid answer for {:?} with type {:?}", value, identifier, variable_info.variable_type());
return Ok(false);
return Ok(Some(format!("{:?} is not a valid answer for {:?} with type {:?}.", value, identifier, variable_info.variable_type())));
}

fn convert_to_list(archetect: &mut Archetect, context: &Context, value: &str) -> Result<Vec<Value>, ArchetectError> {
Expand Down
2 changes: 2 additions & 0 deletions archetect-core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,8 @@ pub enum ArchetectError {
#[error("Headless mode requires answers to be supplied for all variables, but no answer was supplied for the `{0}` \
variable.")]
HeadlessMissingAnswer(String),
#[error("Headless mode attempted to use the default value for the `{identifier}` variable, however, {message}")]
HeadlessInvalidDefault { identifier: String, default: String, message: String },
}

#[derive(Debug, thiserror::Error)]
Expand Down