Skip to content

Commit

Permalink
Take two values for -p and -b instead of using = as a delimiter.
Browse files Browse the repository at this point in the history
Fixes #6.
  • Loading branch information
jimmycuadra committed Jul 28, 2016
1 parent 53579f8 commit 0f25199
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = "0.3.0"

[dependencies]
base64 = "0.1.1"
clap = "2.5.1"
clap = "2.9.3"
lazy_static = "0.2.1"
regex = "0.1.71"
yaml-rust = "0.3.2"
19 changes: 9 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fn real_main() -> Result<(), String> {
.version(env!("CARGO_PKG_VERSION"))
.about("Produces a Kubernetes manifest from a parameterized template")
.setting(AppSettings::ArgRequiredElseHelp)
.setting(AppSettings::AllowExternalSubcommands)
.arg(
Arg::with_name("template")
.help("Path to the template file to be processed")
Expand All @@ -42,35 +41,35 @@ fn real_main() -> Result<(), String> {
)
.arg(
Arg::with_name("parameter")
.help("One or more key-value pairs used to fill in the template's parameters, \
formatted as: KEY=VALUE [KEY=VALUE ...]")
.help("Supplies a value for the named parameter")
.next_line_help(true)
.long("parameter")
.short("p")
.multiple(true)
.takes_value(true)
.number_of_values(2)
.value_names(&["NAME", "VALUE"])
)
.arg(
Arg::with_name("base64-parameter")
.help("Same as --parameter, but for values already encoded in Base64")
.next_line_help(true)
.long("base64-parameter")
.short("b")
.value_name("parameter")
.multiple(true)
.takes_value(true)
.number_of_values(2)
.value_names(&["NAME", "VALUE"])
)
.get_matches();

let mut values = match matches.values_of("parameter") {
Some(parameters) => try!(
user_values(parameters.map(|s| s.to_string()).collect(), false)
),
Some(parameters) => user_values(parameters, false),
None => HashMap::new(),
};

if let Some(parameters) = matches.values_of("base64-parameter") {
let encoded_values = try!(
user_values(parameters.map(|s| s.to_string()).collect(), true)
);
let encoded_values = user_values(parameters, true);

values.extend(encoded_values);
}
Expand Down
22 changes: 10 additions & 12 deletions src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::error::Error;
use std::str::FromStr;

use base64::encode;
use clap::Values;
use yaml::Yaml;

pub struct Parameter {
Expand Down Expand Up @@ -117,26 +118,23 @@ impl FromStr for ParameterType {
}
}

pub fn user_values(parameters: Vec<String>, base64_encoded: bool) -> Result<UserValues, String> {
pub fn user_values(mut parameters: Values, base64_encoded: bool) -> UserValues {
let mut user_values = UserValues::new();

for parameter in parameters {
let mut parts: Vec<String> = parameter.split('=').map(|s| s.to_string()).collect();

if parts.len() < 2 {
return Err("Parameters must be supplied in the form KEY=VALUE.".to_string());
} else {
let key = parts.remove(0);
let value = parts.join("=");
loop {
if let Some(name) = parameters.next() {
let value = parameters.next().expect("Parameter was missing its value.");

let user_value = UserValue {
base64_encoded: base64_encoded,
value: value,
value: value.to_string(),
};

user_values.insert(key, user_value);
user_values.insert(name.to_string(), user_value);
} else {
break;
}
}

Ok(user_values)
user_values
}

0 comments on commit 0f25199

Please sign in to comment.