mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-01-02 21:51:46 +08:00
FEATURE: Enable "require()" function to accept json5 files (#3003)
Co-authored-by: charliez0 <charliez0sp@gmail.com>
This commit is contained in:
parent
2ccef49183
commit
8eb3c65050
7 changed files with 49 additions and 5 deletions
1
commands/types/dnscontrol.d.ts
vendored
1
commands/types/dnscontrol.d.ts
vendored
|
@ -110,6 +110,7 @@ interface ResponseHeaders {
|
|||
|
||||
|
||||
declare function require(name: `${string}.json`): any;
|
||||
declare function require(name: `${string}.json5`): any;
|
||||
declare function require(name: string): true;
|
||||
|
||||
/**
|
||||
|
|
1
commands/types/others.d.ts
vendored
1
commands/types/others.d.ts
vendored
|
@ -1,4 +1,5 @@
|
|||
declare function require(name: `${string}.json`): any;
|
||||
declare function require(name: `${string}.json5`): any;
|
||||
declare function require(name: string): true;
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,14 +5,14 @@ parameters:
|
|||
ts_ignore: true
|
||||
---
|
||||
|
||||
`require(...)` loads the specified JavaScript or JSON file, allowing
|
||||
`require(...)` loads the specified JavaScript, JSON, or JSON5 file, allowing
|
||||
to split your configuration across multiple files.
|
||||
|
||||
A better name for this function might be "include".
|
||||
|
||||
If the supplied `path` string ends with `.js`, the file is interpreted
|
||||
as JavaScript code, almost as though its contents had been included in
|
||||
the currently-executing file. If the path string ends with `.json`,
|
||||
the currently-executing file. If the path string ends with `.json` or `.json5` (case insensitive),
|
||||
`require()` returns the `JSON.parse()` of the file's contents.
|
||||
|
||||
If the path string begins with a `./`, it is interpreted relative to
|
||||
|
@ -81,7 +81,7 @@ function includeK8Sdev() {
|
|||
```
|
||||
{% endcode %}
|
||||
|
||||
### Example 3: JSON
|
||||
### Example 3: JSON and JSON5
|
||||
|
||||
Requiring JSON files initializes variables:
|
||||
|
||||
|
@ -89,8 +89,11 @@ Requiring JSON files initializes variables:
|
|||
```javascript
|
||||
var domains = require("./domain-ip-map.json")
|
||||
|
||||
var REG_MY_PROVIDER = NewRegistrar("none");
|
||||
var DSP_MY_DNSSERVER = NewDnsProvider("none");
|
||||
|
||||
for (var domain in domains) {
|
||||
D(domain, REG_MY_PROVIDER, PROVIDER,
|
||||
D(domain, REG_MY_PROVIDER, DSP_MY_DNSSERVER,
|
||||
A("@", domains[domain])
|
||||
);
|
||||
}
|
||||
|
@ -106,6 +109,11 @@ for (var domain in domains) {
|
|||
```
|
||||
{% endcode %}
|
||||
|
||||
JSON5 works the same way, but the filename ends in `.json5`. (Note: JSON5
|
||||
features are supported whether the filename ends with `.json` or `.json5`.
|
||||
However please don't rely on JSON5 features in a `.json` file as this may
|
||||
change some day.)
|
||||
|
||||
# Notes
|
||||
|
||||
`require()` is *much* closer to PHP's `include()` function than it
|
||||
|
|
|
@ -157,7 +157,8 @@ func require(call otto.FunctionCall) otto.Value {
|
|||
var value = otto.TrueValue()
|
||||
|
||||
// If its a json file return the json value, else default to true
|
||||
if strings.HasSuffix(filepath.Ext(relFile), "json") {
|
||||
var ext = strings.ToLower(filepath.Ext(relFile))
|
||||
if strings.HasSuffix(ext, "json") || strings.HasSuffix(ext, "json5") {
|
||||
cmd := fmt.Sprintf(`JSON.parse(JSON.stringify(%s))`, string(data))
|
||||
value, err = call.Otto.Run(cmd)
|
||||
} else {
|
||||
|
|
8
pkg/js/parse_tests/049-json5-require.js
Normal file
8
pkg/js/parse_tests/049-json5-require.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
var domains = require('./domain-ip-map.json5')
|
||||
|
||||
var domain = "foo.com"
|
||||
var ip = domains["foo.com"]
|
||||
|
||||
D(domain,"none",
|
||||
A("@",ip)
|
||||
);
|
18
pkg/js/parse_tests/049-json5-require.json
Normal file
18
pkg/js/parse_tests/049-json5-require.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"registrars": [],
|
||||
"dns_providers": [],
|
||||
"domains": [
|
||||
{
|
||||
"name": "foo.com",
|
||||
"registrar": "none",
|
||||
"dnsProviders": {},
|
||||
"records": [
|
||||
{
|
||||
"type": "A",
|
||||
"name": "@",
|
||||
"target": "1.1.1.1"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
7
pkg/js/parse_tests/domain-ip-map.json5
Normal file
7
pkg/js/parse_tests/domain-ip-map.json5
Normal file
|
@ -0,0 +1,7 @@
|
|||
// This is a comment.
|
||||
{
|
||||
"foo.com": "1.1.1.1",
|
||||
"bar.com": "5.5.5.5",
|
||||
}
|
||||
|
||||
// The "bar.com" line has a comma at the end.
|
Loading…
Reference in a new issue