mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
NEW FEATURE: Set JS variables from command line (#913)
* Add ability to specify variables that are passed to js * Use SplitN to respect a = in value part * Add JSON parsing for cli variables * Remove JSON parsing for cli variables * Add a function to set CLI defaults * Update static.go
This commit is contained in:
committed by
GitHub
parent
e51688c22d
commit
512aa7d4b3
@ -160,6 +160,7 @@ type ExecuteDSLArgs struct {
|
|||||||
JSFile string
|
JSFile string
|
||||||
JSONFile string
|
JSONFile string
|
||||||
DevMode bool
|
DevMode bool
|
||||||
|
Variable cli.StringSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
func (args *ExecuteDSLArgs) flags() []cli.Flag {
|
func (args *ExecuteDSLArgs) flags() []cli.Flag {
|
||||||
@ -182,6 +183,12 @@ func (args *ExecuteDSLArgs) flags() []cli.Flag {
|
|||||||
Destination: &args.DevMode,
|
Destination: &args.DevMode,
|
||||||
Usage: "Use helpers.js from disk instead of embedded copy",
|
Usage: "Use helpers.js from disk instead of embedded copy",
|
||||||
},
|
},
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "variable",
|
||||||
|
Aliases: []string{"v"},
|
||||||
|
Destination: &args.Variable,
|
||||||
|
Usage: "Add variable that is passed to JS",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
@ -102,7 +103,7 @@ func ExecuteDSL(args ExecuteDSLArgs) (*models.DNSConfig, error) {
|
|||||||
return nil, fmt.Errorf("no config specified")
|
return nil, fmt.Errorf("no config specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
dnsConfig, err := js.ExecuteJavascript(args.JSFile, args.DevMode)
|
dnsConfig, err := js.ExecuteJavascript(args.JSFile, args.DevMode, stringSliceToMap(args.Variable))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("executing %s: %w", args.JSFile, err)
|
return nil, fmt.Errorf("executing %s: %w", args.JSFile, err)
|
||||||
}
|
}
|
||||||
@ -139,3 +140,15 @@ func exit(err error) error {
|
|||||||
}
|
}
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stringSliceToMap converts cli.StringSlice to map[string]string for further processing
|
||||||
|
func stringSliceToMap(stringSlice cli.StringSlice) map[string]string {
|
||||||
|
mapString := make(map[string]string, len(stringSlice.Value()))
|
||||||
|
for _, values := range stringSlice.Value() {
|
||||||
|
parts := strings.SplitN(values, "=", 2)
|
||||||
|
if len(parts) == 2 {
|
||||||
|
mapString[parts[0]] = parts[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mapString
|
||||||
|
}
|
||||||
|
@ -938,3 +938,12 @@ function require_glob() {
|
|||||||
}
|
}
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set default values for CLI variables
|
||||||
|
function CLI_DEFAULTS(defaults) {
|
||||||
|
for (var key in defaults) {
|
||||||
|
if (typeof this[key] === "undefined") {
|
||||||
|
this[key] = defaults[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
var currentDirectory string
|
var currentDirectory string
|
||||||
|
|
||||||
// ExecuteJavascript accepts a javascript string and runs it, returning the resulting dnsConfig.
|
// ExecuteJavascript accepts a javascript string and runs it, returning the resulting dnsConfig.
|
||||||
func ExecuteJavascript(file string, devMode bool) (*models.DNSConfig, error) {
|
func ExecuteJavascript(file string, devMode bool, variables map[string]string) (*models.DNSConfig, error) {
|
||||||
script, err := ioutil.ReadFile(file)
|
script, err := ioutil.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -40,6 +40,11 @@ func ExecuteJavascript(file string, devMode bool) (*models.DNSConfig, error) {
|
|||||||
vm.Set("REV", reverse)
|
vm.Set("REV", reverse)
|
||||||
vm.Set("glob", listFiles) // used for require_glob()
|
vm.Set("glob", listFiles) // used for require_glob()
|
||||||
|
|
||||||
|
// add cli variables to otto
|
||||||
|
for key, value := range variables {
|
||||||
|
vm.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
helperJs := GetHelpers(devMode)
|
helperJs := GetHelpers(devMode)
|
||||||
// run helper script to prime vm and initialize variables
|
// run helper script to prime vm and initialize variables
|
||||||
if _, err := vm.Run(helperJs); err != nil {
|
if _, err := vm.Run(helperJs); err != nil {
|
||||||
|
@ -212,8 +212,8 @@ var _escData = map[string]*_escFile{
|
|||||||
"/helpers.js": {
|
"/helpers.js": {
|
||||||
name: "helpers.js",
|
name: "helpers.js",
|
||||||
local: "pkg/js/helpers.js",
|
local: "pkg/js/helpers.js",
|
||||||
size: 27737,
|
size: 27948,
|
||||||
modtime: 0,
|
modtime: 1603735487,
|
||||||
compressed: `
|
compressed: `
|
||||||
H4sIAAAAAAAC/+x9aXcbN7Lod/2Kis67adJuU4tjzz3UcN5wtGR0Rtsh6VzP1dPjhdggCbuJ7gHQoplY
|
H4sIAAAAAAAC/+x9aXcbN7Lod/2Kis67adJuU4tjzz3UcN5wtGR0Rtsh6VzP1dPjhdggCbuJ7gHQoplY
|
||||||
+e3vYG2gF0rWmSRfnj8kbKBQKBQKtQAFKCo4Bi4YmYnoaGdnbw/O57DJCsAJESCWhMOcpDhWZauCC2AF
|
+e3vYG2gF0rWmSRfnj8kbKBQKBQKtQAFKCo4Bi4YmYnoaGdnbw/O57DJCsAJESCWhMOcpDhWZauCC2AF
|
||||||
@ -335,7 +335,8 @@ EHNvmM2WBf1sjPHhu3flA7Gj1mtNdviIsYYhw+tBibQc/cie+7MeT8kMd0gsYT3Q8BxgZIfo0j7XDOU5
|
|||||||
ZoqYRZrdd7rqp/d3PCDNkDJZc5JiHZQOeemHOx50CIUfs67kETGvWWdUsCwFRDdrtInVC86ynUlod3eJ
|
ZoqYRZrdd7rqp/d3PCDNkDJZc5JiHZQOeemHOx50CIUfs67kETGvWWdUsCwFRDdrtInVC86ynUlod3eJ
|
||||||
beolR5SIzZvZEs8+m0jxKhO4bwkj3Nz5oyr+ZTJMLWiSzdRxIE5giVM1FpcpO85UQjdRocNG0pStKTDC
|
beolR5SIzZvZEs8+m0jxKhO4bwkj3Nz5oyr+ZTJMLWiSzdRxIE5giVM1FpcpO85UQjdRocNG0pStKTDC
|
||||||
P/f8XFaliaamF7fJY1IpDu9gALuf+O6ROdecYaleFCWEztIiwdD7xC173KPl8hMGinadzNChRZrGJWb/
|
P/f8XFaliaamF7fJY1IpDu9gALuf+O6ROdecYaleFCWEztIiwdD7xC173KPl8hMGinadzNChRZrGJWb/
|
||||||
uX/vJFHjaTlKNLR2FFBLOraq23nc+X8BAAD//9enlT5ZbAAA
|
uX/vJFHjaTlKNLR2FFBLOraqs6KMhdsRNmyX/R1fnEsiifR7uWdWL86n7jFsm7lru3fi+hmrC8zV+sqb
|
||||||
|
sdKu337Gmzu1ebnrTk12q3rVA3Q41XdNzT3u/L8AAAD///wzemcsbQAA
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user