Skip to content

Instantly share code, notes, and snippets.

@lawliet89
Last active July 12, 2017 04:12
Show Gist options
  • Save lawliet89/8973ce6081b963637592143513b092fc to your computer and use it in GitHub Desktop.
Save lawliet89/8973ce6081b963637592143513b092fc to your computer and use it in GitHub Desktop.
Condensed borrowing from Arguments
// From https://github.com/Neo-Type/manifest/blob/35bcff0ee504b7a8007bd01cf455554bb4fa51aa/src/main.rs#L56
lazy_static! {
// Regular expression to strip invalid file name characters.
static ref INVALID_FILE_CHARAS: Regex = Regex::new("[^A-Za-z0-9._-]").unwrap();
}
fn main() {
// `clap` library is used to parse arguments
// The details are not relevant to our discussion
let args = make_parser().get_matches();
// ...
// Let's get a vector of borrowed strings from the parsed command line arguments
let images: Vec<&str> = args.values_of("image").unwrap().collect();
let images: Vec<Images> = images.into_iter().map(|s| parse_image(s)).collect();
// ???
// Profit?
}
/// Parse a Docker image name into an `Image` struct
pub fn parse_image<'a>(image: &'a str) -> Image<'a> {
// The `docker_image::parse` function does not allocate memory
let image_parts = docker_image::parse(image);
Image {
image: image,
repository: image_parts.repository,
tag: image_parts.tag.unwrap_or_else(|| "latest"),
tarball: Cow::from(format!(
"images/{}.tar.gz",
INVALID_FILE_CHARAS.replace_all(image, "_")
)),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment