From f5254faa23db4182f840f0e36c540b3349ee4aaf Mon Sep 17 00:00:00 2001 From: Jim Clark Date: Thu, 7 Sep 2023 19:57:33 -0700 Subject: [PATCH] Add dockerfileignore patternmatcher --- dev/user.clj | 5 +++++ docker/ops.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- go.mod | 3 ++- go.sum | 2 ++ gomod2nix.toml | 3 +++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/dev/user.clj b/dev/user.clj index b97a3c3..fe69a81 100644 --- a/dev/user.clj +++ b/dev/user.clj @@ -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") diff --git a/docker/ops.go b/docker/ops.go index d45acb4..2cfed66 100644 --- a/docker/ops.go +++ b/docker/ops.go @@ -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) diff --git a/go.mod b/go.mod index 8bc731a..e4f90a6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e39c812..a039d0a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/gomod2nix.toml b/gomod2nix.toml index 82db174..5eae652 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -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="