Skip to content
Open
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
15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "dockerfile-parser"
description = "a Rust library for parsing, validating, and modifying Dockerfiles"
authors = ["Tim Buckley <timothy.jas.buckley@hpe.com>"]
edition = "2018"
edition = "2021"
license = "MIT"
keywords = ["parser", "docker", "dockerfile", "pest"]
homepage = "https://github.com/HewlettPackard/dockerfile-parser-rs/"
Expand All @@ -18,16 +18,15 @@ version = "0.1.0"
circle-ci = { repository = "HewlettPackard/dockerfile-parser-rs", branch = "master" }

[dependencies]
pest = "2.1"
pest_derive = "2.1"
snafu = "0.6"
pest = "2.7"
pest_derive = "2.7"
snafu = "0.8"
enquote = "1.1"
regex = "1.5"
lazy_static = "1.4"
regex = "1.11"

[dev-dependencies]
indoc = "1.0"
pretty_assertions = "1.0.0"
indoc = "2.0.5"
pretty_assertions = "1.4.1"

[lib]
name = "dockerfile_parser"
Expand Down
4 changes: 2 additions & 2 deletions src/dockerfile_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub struct Dockerfile {

fn parse_dockerfile(input: &str) -> Result<Dockerfile> {
let dockerfile = DockerfileParser::parse(Rule::dockerfile, input)
.context(ParseError)?
.context(ParseSnafu)?
.next()
.ok_or(Error::UnknownParseError)?;

Expand Down Expand Up @@ -374,7 +374,7 @@ impl Dockerfile {
{
let mut buf = String::new();
let mut buf_reader = BufReader::new(reader);
buf_reader.read_to_string(&mut buf).context(ReadError)?;
buf_reader.read_to_string(&mut buf).context(ReadSnafu)?;

Dockerfile::parse(&buf)
}
Expand Down
20 changes: 7 additions & 13 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::iter::FromIterator;
use std::sync::LazyLock;

use lazy_static::lazy_static;
use regex::Regex;

use crate::{Dockerfile, Span, Splicer};
Expand Down Expand Up @@ -58,9 +58,9 @@ pub fn substitute<'a, 'b>(
used_vars: &mut HashSet<String>,
max_recursion_depth: u8
) -> Option<String> {
lazy_static! {
static ref VAR: Regex = Regex::new(r"\$(?:([A-Za-z0-9_]+)|\{([A-Za-z0-9_]+)\})").unwrap();
}
static VAR: LazyLock<Regex> = LazyLock::new(||
Regex::new(r"\$(?:([A-Za-z0-9_]+)|\{([A-Za-z0-9_]+)\})").unwrap()
);

// note: docker also allows defaults in FROMs, e.g.
// ARG tag
Expand Down Expand Up @@ -156,19 +156,13 @@ impl ImageRef {
let vars: HashMap<&'a str, &'a str> = HashMap::from_iter(
dockerfile.global_args
.iter()
.filter_map(|a| match a.value.as_ref() {
Some(v) => Some((a.name.as_ref(), v.as_ref())),
None => None
})
.filter_map(|a| a.value.as_ref().map(|v| (a.name.as_ref(), v.as_ref())))
);

let mut used_vars = HashSet::new();

if let Some(s) = substitute(&self.to_string(), &vars, &mut used_vars, 16) {
Some((ImageRef::parse(&s), used_vars))
} else {
None
}
substitute(&self.to_string(), &vars, &mut used_vars, 16)
.map(|s| (ImageRef::parse(&s), used_vars))
}

/// Given a Dockerfile (and its global `ARG`s), perform any necessary
Expand Down
2 changes: 1 addition & 1 deletion src/instructions/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl CopyInstruction {

ensure!(
paths.len() >= 2,
GenericParseError {
GenericParseSnafu {
message: "copy requires at least one source and a destination"
}
);
Expand Down
6 changes: 3 additions & 3 deletions src/instructions/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl EnvVar {
pub fn new(span: Span, key: SpannedString, value: impl Into<BreakableString>) -> Self {
EnvVar {
span,
key: key,
key,
value: value.into(),
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ fn parse_env_pair(record: Pair) -> Result<EnvVar> {
);
},
Rule::env_pair_quoted_value => {
let v = unquote(field.as_str()).context(UnescapeError)?;
let v = unquote(field.as_str()).context(UnescapeSnafu)?;

value = Some(
BreakableString::new(&field).add_string(&field, v)
Expand Down Expand Up @@ -116,7 +116,7 @@ impl EnvInstruction {
Rule::env_name => key = Some(parse_string(&field)?),
Rule::env_single_value => value = Some(parse_any_breakable(field)?),
Rule::env_single_quoted_value => {
let v = unquote(field.as_str()).context(UnescapeError)?;
let v = unquote(field.as_str()).context(UnescapeSnafu)?;

value = Some(
BreakableString::new(&field).add_string(&field, v)
Expand Down
11 changes: 4 additions & 7 deletions src/instructions/from.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// (C) Copyright 2019-2020 Hewlett Packard Enterprise Development LP

use std::convert::TryFrom;
use std::sync::LazyLock;

use crate::dockerfile_parser::Instruction;
use crate::image::ImageRef;
Expand All @@ -10,7 +11,6 @@ use crate::SpannedString;
use crate::splicer::*;
use crate::error::*;

use lazy_static::lazy_static;
use regex::Regex;

/// A key/value pair passed to a `FROM` instruction as a flag.
Expand Down Expand Up @@ -71,10 +71,7 @@ pub struct FromInstruction {

impl FromInstruction {
pub(crate) fn from_record(record: Pair, index: usize) -> Result<FromInstruction> {
lazy_static! {
static ref HEX: Regex =
Regex::new(r"[0-9a-fA-F]+").unwrap();
}
static HEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[0-9a-fA-F]+").unwrap());

let span = Span::from_pair(&record);
let mut image_field = None;
Expand All @@ -83,7 +80,7 @@ impl FromInstruction {

for field in record.into_inner() {
match field.as_rule() {
Rule::from_flag => flags.push(FromFlag::from_record(field)?),
Rule::from_flag => flags.push(FromFlag::from_record(field)?),
Rule::from_image => image_field = Some(field),
Rule::from_alias => alias_field = Some(field),
Rule::comment => continue,
Expand All @@ -99,7 +96,7 @@ impl FromInstruction {
});
};

let image_parsed = ImageRef::parse(&image.as_ref());
let image_parsed = ImageRef::parse(image.as_ref());

if let Some(hash) = &image_parsed.hash {
let parts: Vec<&str> = hash.split(":").collect();
Expand Down
4 changes: 2 additions & 2 deletions src/instructions/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Label {
Rule::label_quoted_name | Rule::label_single_quoted_name => {
// label seems to be uniquely able to span multiple lines when quoted
let v = unquote(&clean_escaped_breaks(field.as_str()))
.context(UnescapeError)?;
.context(UnescapeSnafu)?;

name = Some(SpannedString {
content: v,
Expand All @@ -51,7 +51,7 @@ impl Label {
Rule::label_value => value = Some(parse_string(&field)?),
Rule::label_quoted_value => {
let v = unquote(&clean_escaped_breaks(field.as_str()))
.context(UnescapeError)?;
.context(UnescapeSnafu)?;

value = Some(SpannedString {
content: v,
Expand Down
2 changes: 1 addition & 1 deletion src/splicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl From<(usize, usize)> for Span {

impl From<&Pair<'_>> for Span {
fn from(pair: &Pair<'_>) -> Self {
Span::from_pair(&pair)
Span::from_pair(pair)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'a> Ord for Stage<'a> {

impl<'a> PartialOrd for Stage<'a> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
Some(self.cmp(other))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::parser::{DockerfileParser, Pair, Rule};
/// per-instruction unit tests.
pub fn parse_single(input: &str, rule: Rule) -> Result<Instruction> {
let record = DockerfileParser::parse(rule, input)
.context(ParseError)?
.context(ParseSnafu)?
.next()
.ok_or(Error::UnknownParseError)?;

Expand All @@ -28,7 +28,7 @@ where
F: Fn(Pair) -> Result<T>
{
let pair = DockerfileParser::parse(rule, input)
.context(ParseError)?
.context(ParseSnafu)?
.next()
.ok_or(Error::UnknownParseError)?;

Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn parse_string(field: &Pair) -> Result<SpannedString> {
let str_span = Span::from_pair(field);
let field_str = field.as_str();
let content = if matches!(field_str.chars().next(), Some('"' | '\'' | '`')) {
unquote(field_str).context(UnescapeError)?
unquote(field_str).context(UnescapeSnafu)?
} else {
field_str.to_string()
};
Expand Down