Add dockerfileignore patternmatcher

This commit is contained in:
Jim Clark
2023-09-07 19:57:33 -07:00
parent b158093722
commit f5254faa23
5 changed files with 58 additions and 4 deletions

View File

@@ -25,11 +25,16 @@
(comment
(pods/load-pod 'docker.tools "0.1.0")
(pods/load-pod "result/bin/babashka-pod-docker")
(pods/load-pod "result/bin/entrypoint")
(require '[docker.tools :as docker])
(pods/unload-pod {:pod/id "docker.tools"})
;; ignore patterns
(def patterns (docker/dockerignore-patterns (slurp "/Users/slim/vonwig/nodejs-service/.dockerignore")))
(docker/dockerignore-matches (assoc patterns :path "node_modules/hey"))
;; parse image names using github.com/docker/distribution
;; turns golang structs into clojure maps
(docker/parse-image-name "gcr.io/whatever:tag")

View File

@@ -3,8 +3,10 @@ package docker
import (
"github.com/docker/distribution/reference"
"github.com/docker/index-cli-plugin/lsp"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/kballard/go-shellquote"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/patternmatcher"
"github.com/moby/patternmatcher/ignorefile"
//"reflect"
"crypto/sha256"
@@ -28,6 +30,22 @@ type Error struct {
Error string `json:"error"`
}
type Ignore struct {
Patterns []string `json:"patterns"`
}
func patterns(s string) (Ignore, error) {
patterns, err := ignorefile.ReadAll(strings.NewReader(s))
if err != nil {
return Ignore{}, err
}
return Ignore{Patterns: patterns}, err
}
func matches(path string, patterns []string) (bool, error) {
return patternmatcher.MatchesOrParentMatches(path, patterns)
}
func parse_uri(s string) (Reference, error) {
tag, domain, path, digest := "", "", "", ""
@@ -60,7 +78,7 @@ func generate_sbom(message *babashka.Message, image string, username string, pas
go func() error {
for {
tx, ok := <-tx_channel
if (ok && tx != "") {
if ok && tx != "" {
err := babashka.WriteNotDoneInvokeResponse(message, tx)
if err != nil {
babashka.WriteErrorResponse(message, err)
@@ -70,7 +88,7 @@ func generate_sbom(message *babashka.Message, image string, username string, pas
break
}
}
babashka.WriteInvokeResponse(message, "done");
babashka.WriteInvokeResponse(message, "done")
return nil
}()
@@ -124,6 +142,12 @@ func ProcessMessage(message *babashka.Message) (any, error) {
{
Name: "parse-shellwords",
},
{
Name: "dockerignore-patterns",
},
{
Name: "dockerignore-matches",
},
{
Name: "sbom",
Code: `
@@ -218,6 +242,25 @@ func ProcessMessage(message *babashka.Message) (any, error) {
return "done", nil
case "docker.tools/dockerignore-patterns":
args := []string{}
if err := json.Unmarshal([]byte(message.Args), &args); err != nil {
return nil, err
}
return patterns(args[0])
case "docker.tools/dockerignore-matches":
type MyType struct {
Path string `json:"path"`
Patterns []string `json:"patterns"`
}
args := []MyType{}
if err := json.Unmarshal([]byte(message.Args), &args); err != nil {
return nil, err
}
return matches(args[0].Path, args[0].Patterns)
default:
return nil, fmt.Errorf("Unknown var %s", message.Var)

3
go.mod
View File

@@ -7,7 +7,9 @@ require (
github.com/docker/distribution v2.8.1+incompatible
github.com/docker/index-cli-plugin v0.0.34-0.20230213201827-11b2a8c1eaa7
github.com/jackpal/bencode-go v1.0.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/moby/buildkit v0.11.4
github.com/moby/patternmatcher v0.6.0
github.com/sirupsen/logrus v1.9.0
)
@@ -108,7 +110,6 @@ require (
github.com/jedib0t/go-pretty/v6 v6.4.0 // indirect
github.com/jinzhu/copier v0.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect

2
go.sum
View File

@@ -1082,6 +1082,8 @@ github.com/moby/buildkit v0.11.4 h1:mleVHr+n7HUD65QNUkgkT3d8muTzhYUoHE9FM3Ej05s=
github.com/moby/buildkit v0.11.4/go.mod h1:P5Qi041LvCfhkfYBHry+Rwoo3Wi6H971J2ggE+PcIoo=
github.com/moby/locker v1.0.1 h1:fOXqR41zeveg4fFODix+1Ch4mj/gT0NE1XJbp/epuBg=
github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=

View File

@@ -361,6 +361,9 @@ schema = 3
[mod."github.com/moby/locker"]
version = "v1.0.1"
hash = "sha256-OcpbO3fLe0WtLDZFF1ntxoEBlEDjyoA8q8mVAQ0TLB8="
[mod."github.com/moby/patternmatcher"]
version = "v0.6.0"
hash = "sha256-ny3L8ktj8cyN41CEJhaRT74Vi8HpXXsbTvlrw/Tl5+g="
[mod."github.com/moby/sys/mountinfo"]
version = "v0.6.2"
hash = "sha256-Dz2dYMPP8dmijrrbG28HIdjXzgkPw4KVsMbdKqhd4Uk="