#dockerfile #docker #parser #pest #env-var #syntax-error

dockerfile-parser

一个用于解析、验证和修改 Dockerfile 的 Rust 库

18 个版本

0.9.0-alpha.12022年7月20日
0.8.0 2021年12月14日
0.7.1 2020年11月2日
0.5.0 2020年6月25日
0.2.1 2020年3月31日

解析器实现 中排名第 373

Download history 3761/week @ 2024-03-14 2768/week @ 2024-03-21 1285/week @ 2024-03-28 2477/week @ 2024-04-04 3274/week @ 2024-04-11 3083/week @ 2024-04-18 2118/week @ 2024-04-25 4858/week @ 2024-05-02 2420/week @ 2024-05-09 794/week @ 2024-05-16 1687/week @ 2024-05-23 2949/week @ 2024-05-30 2252/week @ 2024-06-06 3690/week @ 2024-06-13 3155/week @ 2024-06-20 2277/week @ 2024-06-27

每月下载量 12,076
用于 3 crates

MIT 许可证

130KB
3.5K SLoC

dockerfile-parser-rs

CircleCI docs.rs

一个用于解析和检查 Dockerfile 的纯 Rust 库,适用于执行静态分析、编写 linters 和围绕 Dockerfile 创建自动化工具。它使用正确的语法并提供完整的语法树以及有用的语法错误。

限制

  • 完全未处理 Buildkit 解析器指令。
  • 未知指令被解析为 MiscInstruction 而不是产生显式错误。一些有效但不太有趣的 Docker 指令以此方式处理,例如 ONBUILDMAINTAINER 等。有关详细信息,请参阅 语法 中的说明。

用法

有关使用示例,包括用于输出 Dockerfile 结构的小型实用程序的示例,请参阅 ./examples

$ cargo run --example stages Dockerfile.test
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/dockerfile Dockerfile.test`
global arg: ArgInstruction { name: "foo", value: None }
stages:
  stage #0
    From(FromInstruction { image: "foo:443/bar", index: 0, alias: None })
  stage #1
    From(FromInstruction { image: "localhost/foo", index: 1, alias: None })
  stage #2
    From(FromInstruction { image: "example.com/foo:bar", index: 2, alias: None })
  stage #3
    From(FromInstruction { image: "alpine:3.10", index: 3, alias: None })
  stage #4
    From(FromInstruction { image: "foo/bar", index: 4, alias: None })
  stage #5
    From(FromInstruction { image: "foo/bar:baz", index: 5, alias: None })
  stage #6
    From(FromInstruction { image: "hello-world:test", index: 6, alias: Some("foo") })
  stage #7
    From(FromInstruction { image: "fooasdf", index: 7, alias: Some("bar-baz") })
    Run(Exec(["foo", "bar", "echo \"hello $world\""]))
    Run(Shell("foo bar baz"))
    Arg(ArgInstruction { name: "image", value: Some("alpine:3.10") })
  stage #8
    From(FromInstruction { image: "$image", index: 8, alias: None })
  stage #9
    From(FromInstruction { image: "alpine:3.10", index: 9, alias: Some("foo") })
    Run(Exec(["foo", "bar"]))
    Run(Shell("foo bar baz     qux     qup"))
    Copy(CopyInstruction { flags: [CopyFlag { name: "from", value: "foo" }], sources: ["/foo/bar", "/foo/baz"], destination: "/qux/" })
    Entrypoint(Shell("foo bar baz"))
    Entrypoint(Exec(["foo", "bar", "baz"]))
    Cmd(Shell("foo bar"))
    Cmd(Exec(["foo", "bar"]))
    Copy(CopyInstruction { flags: [], sources: ["foo"], destination: "bar" })
    Copy(CopyInstruction { flags: [CopyFlag { name: "from", value: "0" }], sources: ["/foo"], destination: "/bar" })
    Misc(MiscInstruction { instruction: "other", arguments: "foo bar" })
    Misc(MiscInstruction { instruction: "other", arguments: "foo bar" })
    Env(EnvInstruction([EnvVar { key: "foo", value: "bar baz   qux" }]))
    Env(EnvInstruction([EnvVar { key: "foo", value: "bar" }, EnvVar { key: "baz", value: "qux" }]))
    Env(EnvInstruction([EnvVar { key: "zxcv", value: "asdf" }]))
    Env(EnvInstruction([EnvVar { key: "foo", value: "bar zxcv" }, EnvVar { key: "baz", value: "qux" }, EnvVar { key: "zxcv", value: "asdf\"qwerty" }, EnvVar { key: "zxcv", value: "zxcvzxvb" }]))
    Label(LabelInstruction([Label { name: "foo", value: "bar" }]))
    Label(LabelInstruction([Label { name: "foo", value: "bar" }]))
    Label(LabelInstruction([Label { name: "foo bar", value: "baz qux" }]))
    Label(LabelInstruction([Label { name: "foo  bar", value: "baz\"  qux" }]))
    Misc(MiscInstruction { instruction: "foo", arguments: "bar" })

切片

某些指令结构还包括各种属性(或整个指令)的字符跨度。包含的切片实用程序可用于重写这些跨度,同时保留文件中的其他用户格式。例如,这可以用于实现自动更新镜像版本的工具,或者为检测到的 lint 提供自动化修复。

请参阅 examples/splice.rs 中的示例,该示例重新编写图像引用。

贡献

欢迎提交错误报告、功能请求和拉取请求!请确保阅读 行为准则 以获取一些开始指南。

请注意,- 如行为准则中所述 - 代码贡献必须表明您接受开发者起源证书,基本上断言您有权提交在项目许可证(MIT)下贡献的代码。如果您同意,只需将 -s 传递给 git commit

git commit -s [...]

... Git 将自动在您的提交信息末尾附加所需的 Signed-off-by: ...

依赖项

~4.5–6MB
~113K SLoC