From 608dd7af56328e864ce1d7966b1d92d5be11ebaf Mon Sep 17 00:00:00 2001 From: Oscar Moreno Garza Date: Thu, 23 May 2019 06:26:40 -0700 Subject: [PATCH] NEW: require() now supports loading JSON too (#474) --- docs/_functions/global/require.md | 28 ++++++++++++++++++++++++- pkg/js/js.go | 14 +++++++++++-- pkg/js/parse_tests/024-json-import.js | 8 +++++++ pkg/js/parse_tests/024-json-import.json | 18 ++++++++++++++++ pkg/js/parse_tests/domain-ip-map.json | 4 ++++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 pkg/js/parse_tests/024-json-import.js create mode 100644 pkg/js/parse_tests/024-json-import.json create mode 100644 pkg/js/parse_tests/domain-ip-map.json diff --git a/docs/_functions/global/require.md b/docs/_functions/global/require.md index 9790e9215..514cd8934 100644 --- a/docs/_functions/global/require.md +++ b/docs/_functions/global/require.md @@ -14,7 +14,7 @@ to split your configuration across multiple files. If the path starts with a // dnsconfig.js require('kubernetes/clusters.js'); -D("mydomain.net", REG, PROVIDER, +D("mydomain.net", REG, PROVIDER, IncludeKubernetes() ); @@ -50,3 +50,29 @@ function includeK8Sdev() { {%endhighlight%} {% include endExample.html %} + +You can also use it to require json files and initialize variables with it: +For Example: + +{% include startExample.html %} +{% highlight js %} + +// dnsconfig.js +var domains = require('./domain-ip-map.json') + +for (var domain in domains) { + D(domain, REG, PROVIDER, + A("@", domains[domain]) + ); +} + +{%endhighlight%} + +{%highlight js %} +// domain-ip-map.json +{ + "mydomain.net": "1.1.1.1", + "myotherdomain.org": "5.5.5.5" +} +{%endhighlight} +{% include endExample.html %} \ No newline at end of file diff --git a/pkg/js/js.go b/pkg/js/js.go index 77f5a9bd3..9013a99f3 100644 --- a/pkg/js/js.go +++ b/pkg/js/js.go @@ -2,6 +2,7 @@ package js import ( "encoding/json" + "fmt" "io/ioutil" "path/filepath" "strings" @@ -95,7 +96,16 @@ func require(call otto.FunctionCall) otto.Value { throw(call.Otto, err.Error()) } - _, err = call.Otto.Run(string(data)) + var value otto.Value = otto.TrueValue() + + // If its a json file return the json value, else default to true + if strings.HasSuffix(filepath.Ext(relFile), "json") { + cmd := fmt.Sprintf(`JSON.parse(JSON.stringify(%s))`, string(data)) + value, err = call.Otto.Run(cmd) + } else { + _, err = call.Otto.Run(string(data)) + } + if err != nil { throw(call.Otto, err.Error()) } @@ -103,7 +113,7 @@ func require(call otto.FunctionCall) otto.Value { // Pop back to the old directory. currentDirectory = currentDirectoryOld - return otto.TrueValue() + return value } func throw(vm *otto.Otto, str string) { diff --git a/pkg/js/parse_tests/024-json-import.js b/pkg/js/parse_tests/024-json-import.js new file mode 100644 index 000000000..26e969088 --- /dev/null +++ b/pkg/js/parse_tests/024-json-import.js @@ -0,0 +1,8 @@ +var domains = require('./domain-ip-map.json') + +var domain = "foo.com" +var ip = domains["foo.com"] + +D(domain,"none", + A("@",ip) +); diff --git a/pkg/js/parse_tests/024-json-import.json b/pkg/js/parse_tests/024-json-import.json new file mode 100644 index 000000000..9c04cd72c --- /dev/null +++ b/pkg/js/parse_tests/024-json-import.json @@ -0,0 +1,18 @@ +{ + "registrars": [], + "dns_providers": [], + "domains": [ + { + "name": "foo.com", + "registrar": "none", + "dnsProviders": {}, + "records": [ + { + "type": "A", + "name": "@", + "target": "1.1.1.1" + } + ] + } + ] +} \ No newline at end of file diff --git a/pkg/js/parse_tests/domain-ip-map.json b/pkg/js/parse_tests/domain-ip-map.json new file mode 100644 index 000000000..a27925aee --- /dev/null +++ b/pkg/js/parse_tests/domain-ip-map.json @@ -0,0 +1,4 @@ +{ + "foo.com": "1.1.1.1", + "bar.com": "5.5.5.5" +}