Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mcorbin committed Dec 22, 2021
1 parent cd1f9be commit 2098d40
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

`ymlgen` lets you generate yaml files in a declarative way. You can for example use it to manage your Kubernetes manifests.

It leverages the [EDN](https://github.com/edn-format/edn) format and the [Aero](https://github.com/juxt/aero) to do so.
It supports including parts of definitions into other ones, variables, generating files with a different shape based on a `profile`, reading values from environment variables... All of this allows you to manage your YAML files in an effective way.

Why `ymlgen` ? It's simple (< 170 lines of code including namespaces declarations, line breaks...), powerful and extensible.
The tool leverages the [EDN](https://github.com/edn-format/edn) format and the [Aero](https://github.com/juxt/aero) library.

Why `ymlgen` ? It's simple, powerful and extensible.

## Install

For Linux (amd64), download the `ymlgen` binary and put it in your PATH. This binary is built using [GraalVM](https://www.graalvm.org/) so more targets may be added soon.
For Linux (x86-64), download the `ymlgen` binary and put it in your PATH. This binary is built using [GraalVM](https://www.graalvm.org/) so more targets may be added soon (help welcome).

You can alternatively download the `jar` file and then run it with `java -jar ymlgen.jar` (Java 17 needed).

Expand Down Expand Up @@ -187,3 +189,39 @@ As you can see, ref allows you to reference another part of your edn file.

Don't hesitate to check the Aero [documentation](https://github.com/juxt/aero#tag-literals) for more examples !

## Generate EDN from YAML

You can use `ymlgen` to convert an existing YAML file to EDN. This can help you to get started with the tool by using existing YAML files. An example with this file named `pod.yml`:

```yaml
---
apiVersion: v1
kind: Pod
metadata:
name: dnsutils
namespace: default
spec:
containers:
- name: dnsutils
image: k8s.gcr.io/e2e-test-images/jessie-dnsutils:1.3
command:
- sleep
- '3600'
imagePullPolicy: ifNotPresent
restartPolicy: Always
```

`ymlgen edn --template pod.yaml` will produce:

```clojure
[{:apiVersion "v1",
:kind "Pod",
:metadata {:name "dnsutils", :namespace "default"},
:spec
{:containers
[{:name "dnsutils",
:image "k8s.gcr.io/e2e-test-images/jessie-dnsutils:1.3",
:command ["sleep" "3600"],
:imagePullPolicy "ifNotPresent"}],
:restartPolicy "Always"}}]
```
2 changes: 1 addition & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ native-image --report-unsupported-elements-at-runtime \
--initialize-at-build-time \
--no-server \
-jar ./target/uberjar/ymlgen-*-standalone.jar \
-H:Name=./target/ymlgen
-H:Name=./target/ymlgen-${tag}
8 changes: 7 additions & 1 deletion src/ymlgen/commands/edn.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
[clojure.java.io :as io]
[clojure.pprint :as pprint]))

(extend-protocol yaml/YAMLCodec
java.util.ArrayList
(decode [data keywords]
(mapv #(yaml/decode % keywords) data))
)

(defn parse-string-all
[^String string & {:keys [unsafe mark keywords max-aliases-for-collections allow-recursive-keys allow-duplicate-keys] :or {keywords true}}]
(for [v (.loadAll (yaml/make-yaml :unsafe unsafe
Expand All @@ -17,7 +23,7 @@
(defn yaml->edn
"Converts a yaml string to edn"
[yaml-string]
(parse-string-all yaml-string :keywords true))
(vec (parse-string-all yaml-string :keywords true)))

(defn gen-edn
"Generates edn from a yaml file"
Expand Down
1 change: 1 addition & 0 deletions test/resources/yaml/f1.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
foo: bar
list: [1, 2, 3]
3 changes: 2 additions & 1 deletion test/ymlgen/commands/edn_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
(is (= [{:foo "bar" :test 1} {:a true}] (edn/yaml->edn "---\nfoo: bar\ntest: 1\n---\na: true"))))

(deftest gen-edn-test
(is (= [{:foo "bar"}]
(is (= [{:foo "bar"
:list [1 2 3]}]
(edn/gen-edn {:template-path (.getPath (io/resource "yaml/f1.yaml"))})))
(is (= [{:foo "bar"}
{:test {:bar "baz" :bool true :int 3}}]
Expand Down

0 comments on commit 2098d40

Please sign in to comment.