mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
wire into js REVERSE function
This commit is contained in:
@ -1 +0,0 @@
|
|||||||
D("foo.com","reg","dsp")
|
|
28
js/js.go
28
js/js.go
@ -6,9 +6,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/StackExchange/dnscontrol/models"
|
"github.com/StackExchange/dnscontrol/models"
|
||||||
|
"github.com/StackExchange/dnscontrol/transform"
|
||||||
|
|
||||||
"github.com/robertkrimen/otto"
|
"github.com/robertkrimen/otto"
|
||||||
//load underscore js into vm by default
|
//load underscore js into vm by default
|
||||||
|
|
||||||
_ "github.com/robertkrimen/otto/underscore"
|
_ "github.com/robertkrimen/otto/underscore"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,6 +19,7 @@ func ExecuteJavascript(script string, devMode bool) (*models.DNSConfig, error) {
|
|||||||
vm := otto.New()
|
vm := otto.New()
|
||||||
|
|
||||||
vm.Set("require", require)
|
vm.Set("require", require)
|
||||||
|
vm.Set("REVERSE", reverse)
|
||||||
|
|
||||||
helperJs := GetHelpers(devMode)
|
helperJs := GetHelpers(devMode)
|
||||||
// run helper script to prime vm and initialize variables
|
// run helper script to prime vm and initialize variables
|
||||||
@ -50,15 +53,36 @@ func GetHelpers(devMode bool) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func require(call otto.FunctionCall) otto.Value {
|
func require(call otto.FunctionCall) otto.Value {
|
||||||
|
if len(call.ArgumentList) != 1 {
|
||||||
|
throw(call.Otto, "require takes exactly one argument")
|
||||||
|
}
|
||||||
file := call.Argument(0).String()
|
file := call.Argument(0).String()
|
||||||
fmt.Printf("requiring: %s\n", file)
|
fmt.Printf("requiring: %s\n", file)
|
||||||
data, err := ioutil.ReadFile(file)
|
data, err := ioutil.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
throw(call.Otto, err.Error())
|
||||||
}
|
}
|
||||||
_, err = call.Otto.Run(string(data))
|
_, err = call.Otto.Run(string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
throw(call.Otto, err.Error())
|
||||||
}
|
}
|
||||||
return otto.TrueValue()
|
return otto.TrueValue()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func throw(vm *otto.Otto, str string) {
|
||||||
|
panic(vm.MakeCustomError("Error", str))
|
||||||
|
}
|
||||||
|
|
||||||
|
func reverse(call otto.FunctionCall) otto.Value {
|
||||||
|
if len(call.ArgumentList) != 1 {
|
||||||
|
throw(call.Otto, "REVERSE takes exactly one argument")
|
||||||
|
}
|
||||||
|
dom := call.Argument(0).String()
|
||||||
|
rev, err := transform.ReverseDomainName(dom)
|
||||||
|
fmt.Println(dom, rev, err)
|
||||||
|
if err != nil {
|
||||||
|
throw(call.Otto, err.Error())
|
||||||
|
}
|
||||||
|
v, _ := otto.ToValue(rev)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
@ -71,6 +71,7 @@ func TestErrors(t *testing.T) {
|
|||||||
{"old dsp style", `D("foo.com","reg","dsp")`},
|
{"old dsp style", `D("foo.com","reg","dsp")`},
|
||||||
{"MX no priority", `D("foo.com","reg",MX("@","test."))`},
|
{"MX no priority", `D("foo.com","reg",MX("@","test."))`},
|
||||||
{"MX reversed", `D("foo.com","reg",MX("@","test.", 5))`},
|
{"MX reversed", `D("foo.com","reg",MX("@","test.", 5))`},
|
||||||
|
{"Bad cidr", `D(reverse("foo.com"), "reg")`},
|
||||||
}
|
}
|
||||||
for _, tst := range tests {
|
for _, tst := range tests {
|
||||||
t.Run(tst.desc, func(t *testing.T) {
|
t.Run(tst.desc, func(t *testing.T) {
|
||||||
|
@ -27,6 +27,8 @@ func TestReverse(t *testing.T) {
|
|||||||
//Errror Cases:
|
//Errror Cases:
|
||||||
{"0.0.0.0/0", true, ""},
|
{"0.0.0.0/0", true, ""},
|
||||||
{"2001::/0", true, ""},
|
{"2001::/0", true, ""},
|
||||||
|
{"4.5/16", true, ""},
|
||||||
|
{"foo.com", true, ""},
|
||||||
}
|
}
|
||||||
for i, tst := range tests {
|
for i, tst := range tests {
|
||||||
t.Run(fmt.Sprintf("%d--%s", i, tst.in), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%d--%s", i, tst.in), func(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user