1
0
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:
Jan-Philipp Benecke
2020-10-27 15:43:00 +01:00
committed by GitHub
parent e51688c22d
commit 512aa7d4b3
5 changed files with 40 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"strings"
"github.com/urfave/cli/v2"
@ -102,7 +103,7 @@ func ExecuteDSL(args ExecuteDSLArgs) (*models.DNSConfig, error) {
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 {
return nil, fmt.Errorf("executing %s: %w", args.JSFile, err)
}
@ -139,3 +140,15 @@ func exit(err error) error {
}
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
}