GithubHelp home page GithubHelp logo

classicvalues / dockerfile-parser-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hewlettpackard/dockerfile-parser-rs

0.0 1.0 0.0 91 KB

a Rust library for parsing, validating, and modifying Dockerfiles

License: MIT License

Rust 100.00%

dockerfile-parser-rs's Introduction

dockerfile-parser-rs

CircleCI docs.rs

A pure Rust library for parsing and inspecting Dockerfiles, useful for performing static analysis, writing linters, and creating automated tooling around Dockerfiles. It uses a proper grammar and can provide useful syntax errors in addition to a full syntax tree.

Limitations

  • Buildkit parser directives are not handled at all.
  • Unknown instructions are parsed as MiscInstruction rather than producing an explicit error. A number of valid but less interesting Docker instructions are handled this way, e.g. ONBUILD, MAINTAINER, etc. See notes in the grammar for details.

Usage

See ./examples for a few usage examples, including a small utility to dump a Dockerfile's structure:

$ 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" })

Splicing

Some instruction structs also include character spans for various attributes (or the entire instruction). The included splicing utility can be used to rewrite these spans while preserving other user formatting within the file. For example, this can be used to implement a utility that automatically updates image versions, or to provide automated fixes for detected lints.

See examples/splice.rs for an example that rewrites image references.

Contributing

Bug reports, feature requests, and pull requests are welcome! Be sure to read though the code of conduct for some pointers to get started.

Note that - as mentioned in the code of conduct - code contributions must indicate that you accept the Developer Certificate of Origin, essentially asserting you have the necessary rights to submit the code you're contributing under the project's license (MIT). If you agree, simply pass -s to git commit:

git commit -s [...]

... and Git will automatically append the required Signed-off-by: ... to the end of your commit message.

dockerfile-parser-rs's People

Contributors

timothyb89 avatar dsherret avatar classicvalues avatar dependabot[bot] avatar jonmcquillan avatar mend-bolt-for-github[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.