From f6df826afe53b8f9d4cb11b047f6d67e47a54eda Mon Sep 17 00:00:00 2001 From: liana Date: Mon, 31 Jul 2023 20:40:47 +0500 Subject: [PATCH] update yaml parser --- lenses/simpleyaml.aug | 71 +++++++++ lenses/tests/test_simpleyaml.aug | 252 +++++++++++++++++++++++++++++++ tests/Makefile.am | 1 + 3 files changed, 324 insertions(+) create mode 100644 lenses/simpleyaml.aug create mode 100644 lenses/tests/test_simpleyaml.aug diff --git a/lenses/simpleyaml.aug b/lenses/simpleyaml.aug new file mode 100644 index 000000000..7cf59af01 --- /dev/null +++ b/lenses/simpleyaml.aug @@ -0,0 +1,71 @@ +(* +Module: Simpleyaml + +Parses standard and simple .yaml configuration file + +Author: Ostapchuk Liana ssmgroll@gmail.com + +Example of .yaml file: + +> key: value +> key2: +> key3: value3 +> key4: +> key5: value5 +> key6: value6 +> key7: value7:........ +> key8: +> - host:port +> - host2:port2 +> key9: +> key10: value10 +> key11: +> key12: value12 +> key13: +> key14: value14 +> key15: value15_or_empty +*) + +module Simpleyaml = + +(************************************************************************ +* USEFUL PRIMITIVES + *************************************************************************) +autoload xfm + +let colon = Sep.colon +let space = del Rx.space " " +let label = key /[^,# \n\t]+/ +let value = store (/[^ #\r\t\n].*[^ \r\t\n]|[^ \t\n\r]/) +let eol = Util.eol +let indent = Util.indent +let dash = Util.del_str "- " +let comment = Util.comment +let hyphen = Util.del_str "---" . eol+ +let empty = Util.empty + +(************************************************************************ +* `KEY: VALUE` PAIR + *************************************************************************) +let k = label . colon . eol +let v = [ value . eol+ ] +let kv = [ label . colon . space . value . eol+ ] + +(************************************************************************ +* LENS & FILTER + *************************************************************************) + +let entry = (comment . eol* ) | [ indent . dash . label . eol+] | (indent . dash* . kv ) +let record = [ indent . k . Util.empty* . entry* . hyphen*] + +let lns = ( hyphen* . ( (comment . eol* ) | kv )* . record* ) | empty + + +let filter = incl "/etc/*.yaml" + . incl "/etc/*/*.yaml" + . incl "/usr/share/*.yaml" + . incl "/var/lib/*.yaml" + . incl "/usr/share/*/*.yaml" + . incl "/var/lib/*/*.yaml" + +let xfm = transform lns filter \ No newline at end of file diff --git a/lenses/tests/test_simpleyaml.aug b/lenses/tests/test_simpleyaml.aug new file mode 100644 index 000000000..af71af2b6 --- /dev/null +++ b/lenses/tests/test_simpleyaml.aug @@ -0,0 +1,252 @@ +(* +Module: Simpleyaml + +Parses standard and simple .yaml configuration file + +Author: Ostapchuk Liana ssmgroll@gmail.com + +Example of .yaml file: + +> key: value +> key2: +> key3: value3 +> key4: +> key5: value5 +> key6: value6 +> key7: value7:........ +> key8: +> - host:port +> - host2:port2 +> key9: +> key10: value10 +> key11: +> key12: value12 +> key13: +> key14: value14 +> key15: value15_or_empty +*) + +module Test_simpleyaml = + +(* GET *) + +let test1 ="--- +foo: bar +pleh: help +stuff: + foo: bar + bar: foo +" + +test Simpleyaml.lns get test1 = { "foo" = "bar" } + { "pleh" = "help" } + { "stuff" + { "foo" = "bar" } + { "bar" = "foo" } + } + +let test2 ="ssh_service: + enabled: yes + listen_addr: 1.1.1.1 + labels: + team: test + owner: iam + env: some_env + project: + product: + cluster: + + commands: + - name: name1 + command: hostname + period: 1m0s + - name: name2 + command: hostname2 + period: 1m0s2 +" + +test Simpleyaml.lns get test2 = { "ssh_service" + { "enabled" = "yes" } + { "listen_addr" = "1.1.1.1" } + } + { "labels" + { "team" = "test" } + { "owner" = "iam" } + { "env" = "some_env" } + } + { "project" } + { "product" } + { "cluster" + { } + } + { "commands" + { "name" = "name1" } + { "command" = "hostname" } + { "period" = "1m0s" } + { "name" = "name2" } + { "command" = "hostname2" } + { "period" = "1m0s2" } + } + + +let test3 = "version: v2 +teleport: + data_dir: /path/ + log: + output: /path/teleport.log + severity: INFO + format: + output: json + + auth_servers: + - hostname:3030 + - hostname3:30303 + + join_params: + token_name: 111dd2f-0a0f-4444d-aa-s-f7-a825bb878db9 + method: name + +auth_service: + enabled: no +proxy_service: + enabled: no + +ssh_service: + enabled: yes + # disable_create_host_user: true + listen_addr: 1.1.1.1:1111 + pam: + enabled: true + service_name: teleport + + commands: + - name: name + command: [hostname] + period: 1m0s + + labels: + team: name + owner: ostapchuk.liana + env: production + project: may_be_empty + product: may_be_empty + # cluster: may_be_empty +" + +test Simpleyaml.lns get test3 = { "version" = "v2" } + { "teleport" + { "data_dir" = "/path/" } + } + { "log" + { "output" = "/path/teleport.log" } + { "severity" = "INFO" } + } + { "format" + { "output" = "json" } + } + { "auth_servers" + { "hostname:3030" } + { "hostname3:30303" } + } + { "join_params" + { "token_name" = "111dd2f-0a0f-4444d-aa-s-f7-a825bb878db9" } + { "method" = "name" } + } + { "auth_service" + { "enabled" = "no" } + } + { "proxy_service" + { "enabled" = "no" } + } + { "ssh_service" + { "enabled" = "yes" } + { "#comment" = "disable_create_host_user: true" } + { "listen_addr" = "1.1.1.1:1111" } + } + { "pam" + { "enabled" = "true" } + { "service_name" = "teleport" } + } + { "commands" + { "name" = "name" } + { "command" = "[hostname]" } + { "period" = "1m0s" } + } + { "labels" + { "team" = "name" } + { "owner" = "ostapchuk.liana" } + { "env" = "production" } + { "project" = "may_be_empty" } + { "product" = "may_be_empty" } + { "#comment" = "cluster: may_be_empty" } + } + +(* SET *) +test Simpleyaml.lns put test1 after + set "foo" "changed_foo" = "foo: changed_foo +pleh: help +stuff: + foo: bar + bar: foo +" + +test Simpleyaml.lns put test2 after + set "labels/owner" "changed_iam" = "ssh_service: + enabled: yes + listen_addr: 1.1.1.1 + labels: + team: test + owner: changed_iam + env: some_env + project: + product: + cluster: + + commands: + name: name1 + command: hostname + period: 1m0s + name: name2 + command: hostname2 + period: 1m0s2 +" + +test Simpleyaml.lns put test3 after + set "labels/new_label" "new" = "version: v2 +teleport: + data_dir: /path/ + log: + output: /path/teleport.log + severity: INFO + format: + output: json + auth_servers: + - hostname:3030 + - hostname3:30303 + join_params: + token_name: 111dd2f-0a0f-4444d-aa-s-f7-a825bb878db9 + method: name +auth_service: + enabled: no +proxy_service: + enabled: no +ssh_service: + enabled: yes + # disable_create_host_user: true + listen_addr: 1.1.1.1:1111 + pam: + enabled: true + service_name: teleport + commands: + name: name + command: [hostname] + period: 1m0s + labels: + team: name + owner: ostapchuk.liana + env: production + project: may_be_empty + product: may_be_empty + # cluster: may_be_empty +new_label: new +" \ No newline at end of file diff --git a/tests/Makefile.am b/tests/Makefile.am index 112028182..5040d392d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -201,6 +201,7 @@ lens_tests = \ lens-shellvars_list.sh \ lens-simplelines.sh \ lens-simplevars.sh \ + lens-simpleyaml.sh \ lens-sip_conf.sh \ lens-slapd.sh \ lens-smbusers.sh \