Files
babashka-pod-docker/README.md

102 lines
3.7 KiB
Markdown
Raw Permalink Normal View History

2022-12-04 12:19:08 -08:00
## Background
2023-02-06 23:23:24 +00:00
This is a [babashka pod](https://github.com/babashka/pods) that binds some golang functions into a clojure namespace. Using this pod, clojure programs can parse dockerfiles and docker images names using the "official" docker golang libraries.
2022-12-04 12:19:08 -08:00
* [`github.com/docker/distribution/reference`](https://github.com/distribution/distribution/blob/main/reference/reference.go) (for image name parsing)
* [`github.com/moby/buildkit/frontend/dockerfile/parser`](https://github.com/moby/buildkit/blob/master/frontend/dockerfile/parser/parser.go) (for generating a Dockerfile AST).
## Usage
```clojure
(require '[babashka.pods :as pods])
2023-02-09 13:26:42 +00:00
(pods/load-pod 'docker/babashka-pod-docker "0.1.0")
2022-12-04 12:19:08 -08:00
; OR use a locally built pod binary
2023-02-09 13:26:42 +00:00
#_(pods/load-pod "./babashka-pod-docker")
2022-12-04 12:19:08 -08:00
;; load-pod will create this namespace with two vars
2023-02-09 13:26:42 +00:00
(require '[docker.babashka-pod-docker :as docker])
2022-12-04 12:19:08 -08:00
2023-02-06 23:23:24 +00:00
;; parse image names using github.com/docker/distribution
2022-12-04 12:19:08 -08:00
;; turns golang structs into clojure maps
2023-02-06 23:23:24 +00:00
(docker/parse-image-name "gcr.io/whatever:tag")
2022-12-04 12:19:08 -08:00
;; automatically turns golang errors into Exceptions
(try
(docker/parse-image-name "gcr.io/whatever/:tag")
2023-02-06 23:23:24 +00:00
(catch Exception e
2022-12-04 12:19:08 -08:00
;; invalid reference format
(println (.getMessage e))))
;; parse dockerfiles using github.com/moby/buildkit
;; returns the Result struct transformed to a clojure map
(docker/parse-dockerfile "FROM \\\n gcr.io/whatever:tag\nCMD [\"run\"]")
```
2023-02-09 13:26:42 +00:00
Loading `'docker/babashka-pod-docker` from the pod registry will download the binary into `${user.home}/.babashka/pods/registry` (the `$BABASHKA_PODS_DIR` environment variable will be used if it exists).
2023-02-09 13:26:42 +00:00
## Building Locally
2022-12-04 12:19:08 -08:00
To build the golang `parser` binary locally, run `go build`.
```bash
2023-02-09 13:26:42 +00:00
go build -o babashka-pod-docker
```
2023-02-06 23:23:24 +00:00
## Releasing
2023-02-09 13:26:42 +00:00
All pushes to main will update the 0.1.0 release. This is becaus maintaining the pod version in the repository directory and in the pod registry is tricky.
We hope to automate all of that in the future.
2023-02-06 23:23:24 +00:00
2023-01-23 09:10:44 -08:00
## Namespace generation
The `pods/load-pod` call is convenient for a repl-session, or a script, but what if you are `aot` compiling, or building a native binary. In the example above, the namespaces emitted by `pods/load-pod` are not available until runtime.
Here is an example of bindings that will resolve at compile-time and go through the same dispatch.
2023-02-02 17:21:34 -08:00
```clj
2023-01-23 09:10:44 -08:00
; require the babashka.pods in a namespace
(require '[babashka.pods.impl :as impl])
; call at runtime to initialize pod system
(defn load-pod
([pod-spec] (load-pod pod-spec nil))
([pod-spec version opts] (load-pod pod-spec (assoc opts :version version)))
([pod-spec opts]
(let [opts (if (string? opts)
{:version opts}
opts)
pod (impl/load-pod
pod-spec
(merge {:remove-ns remove-ns
:resolve (fn [sym]
(or (resolve sym)
(intern
(create-ns (symbol (namespace sym)))
(symbol (name sym)))))}
opts))]
(future (impl/processor pod))
{:pod/id (:pod-id pod)})))
2023-02-02 17:21:34 -08:00
;; statically define dispatch functions - this is synchronous
2023-01-23 09:10:44 -08:00
(defn parse [s]
2023-02-09 13:26:42 +00:00
(impl/invoke-public "babashka-pod-docker" "babashka-pod-docker/parse-dockerfile" [s] {}))
2023-02-02 17:21:34 -08:00
;; async example
(defn generate-sbom [s]
2023-02-09 13:26:42 +00:00
(impl/invoke-public "babashka-pod-docker" "babashka-pod-docker/-generate-sbom"
2023-02-06 23:23:24 +00:00
[s cb]
2023-02-02 17:21:34 -08:00
{:handlers {:done (fn [])
:success cb
:error (fn [err]}})))
2023-01-23 09:10:44 -08:00
```
```
2023-02-09 13:26:42 +00:00
(pods/load-pod "/bin/babashka-pod-docker")
2023-01-23 09:10:44 -08:00
```
This method of dispatch does not require any dynamic namespace generation.
2022-12-04 12:19:08 -08:00
## Contributing
You can find information about contributing to this project in the CONTRIBUTING.md