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

Added "@file.txt" style inclusion of files for CLI args #452

Merged
merged 1 commit into from
Oct 17, 2023
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
37 changes: 26 additions & 11 deletions crates/wick/wick-component-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,35 @@ pub fn parse_args(args: &[String], sig: &OperationSignature) -> Result<Vec<Packe
return Err(Error::InvalidInput(name.to_owned()));
}
let input = input.unwrap();
let value: Value = match input.ty() {
// Datetime can be parsed from a string or a number but numbers need to be stringified.
// Strings must be explicit because a bare number will be parsed as a number.
Type::Datetime | Type::String => {
if is_valid(value) {
coerce_string(name, value, input.ty())?
} else {
// if it's not valid JSON then it's a bare string.
value.into()

let value = if value.starts_with('@') {
let path = value.trim_start_matches('@');

match input.ty() {
Type::String => Value::String(std::fs::read_to_string(path)?),
Type::Bytes => {
let bytes: wick_packet::Base64Bytes = std::fs::read(path)?.into();
serde_json::to_value(bytes).unwrap()
}
_ => encode(&std::fs::read_to_string(path)?),
}
} else {
match input.ty() {
// Datetime can be parsed from a string or a number but numbers need to be stringified.
// Strings must be explicit because a bare number will be parsed as a number.
Type::Datetime | Type::String => {
if is_valid(value) {
coerce_string(name, value, input.ty())?
} else {
// if it's not valid JSON then it's a bare string.
value.into()
}
}
// serde_json does an adequate job on the rest.
_ => encode(value),
}
// serde_json does an adequate job on the rest.
_ => encode(value),
};

// Note on above: complex objects with embedded Datetime/Strings
// may not be parsed correctly but that's an edge case we're ignoring for now.

Expand Down
12 changes: 10 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,16 @@ pub(crate) async fn print_stream_json(
pub(crate) fn parse_config_string(source: Option<&str>) -> Result<Option<RuntimeConfig>> {
let component_config = match source {
Some(c) => {
let config = serde_json::from_str::<LiquidJsonValue>(c)
.map_err(|e| anyhow::anyhow!("Failed to parse config argument as JSON: {}", e))?;
let config = if c.starts_with('@') {
let path = c.trim_start_matches('@');
let source = std::fs::read_to_string(path)?;
serde_json::from_str::<LiquidJsonValue>(&source)
.map_err(|e| anyhow::anyhow!("Failed to parse {} as JSON: {}", path, e))?
} else {
serde_json::from_str::<LiquidJsonValue>(c)
.map_err(|e| anyhow::anyhow!("Failed to parse config argument as JSON: {}", e))?
};

let ctx = LiquidJsonConfig::make_context(
None,
None,
Expand Down
Loading