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
|
||||
JSONFile string
|
||||
DevMode bool
|
||||
Variable cli.StringSlice
|
||||
}
|
||||
|
||||
func (args *ExecuteDSLArgs) flags() []cli.Flag {
|
||||
@ -182,6 +183,12 @@ func (args *ExecuteDSLArgs) flags() []cli.Flag {
|
||||
Destination: &args.DevMode,
|
||||
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"
|
||||
"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
|
||||
}
|
||||
|
Reference in New Issue
Block a user