2017-09-13 22:00:41 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-09-01 04:01:56 +08:00
|
|
|
"log"
|
2017-09-13 22:00:41 +08:00
|
|
|
"os"
|
2020-10-27 22:43:00 +08:00
|
|
|
"strings"
|
2017-09-13 22:00:41 +08:00
|
|
|
|
2020-02-04 01:44:11 +08:00
|
|
|
"github.com/urfave/cli/v2"
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
|
2020-04-15 04:47:30 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v3/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/js"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/normalize"
|
2017-09-13 22:00:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = cmd(catDebug, func() *cli.Command {
|
|
|
|
var args PrintIRArgs
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "print-ir",
|
|
|
|
Usage: "Output intermediate representation (IR) after running validation and normalization logic.",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return exit(PrintIR(args))
|
|
|
|
},
|
|
|
|
Flags: args.flags(),
|
|
|
|
}
|
|
|
|
}())
|
|
|
|
|
|
|
|
var _ = cmd(catDebug, func() *cli.Command {
|
|
|
|
var args PrintIRArgs
|
2017-09-15 23:52:30 +08:00
|
|
|
// This is the same as print-ir with the following changes:
|
|
|
|
// - output defaults to /dev/null.
|
|
|
|
// - prints "No errors." if there were no errors.
|
2017-09-13 22:00:41 +08:00
|
|
|
return &cli.Command{
|
|
|
|
Name: "check",
|
|
|
|
Usage: "Check and validate dnsconfig.js. Do not access providers.",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
if args.Output == "" {
|
|
|
|
args.Output = os.DevNull
|
|
|
|
}
|
2017-09-15 23:52:30 +08:00
|
|
|
err := exit(PrintIR(args))
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "No errors.\n")
|
|
|
|
}
|
|
|
|
return err
|
2017-09-13 22:00:41 +08:00
|
|
|
},
|
|
|
|
Flags: args.flags(),
|
|
|
|
}
|
|
|
|
}())
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// PrintIRArgs encapsulates the flags/arguments for the print-ir command.
|
2017-09-13 22:00:41 +08:00
|
|
|
type PrintIRArgs struct {
|
|
|
|
GetDNSConfigArgs
|
|
|
|
PrintJSONArgs
|
|
|
|
Raw bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (args *PrintIRArgs) flags() []cli.Flag {
|
|
|
|
flags := append(args.GetDNSConfigArgs.flags(), args.PrintJSONArgs.flags()...)
|
|
|
|
flags = append(flags, &cli.BoolFlag{
|
|
|
|
Name: "raw",
|
|
|
|
Usage: "Skip validation and normalization. Just print js result.",
|
|
|
|
Destination: &args.Raw,
|
|
|
|
})
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// PrintIR implements the print-ir subcommand.
|
2017-09-13 22:00:41 +08:00
|
|
|
func PrintIR(args PrintIRArgs) error {
|
|
|
|
cfg, err := GetDNSConfig(args.GetDNSConfigArgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !args.Raw {
|
2020-03-11 04:53:17 +08:00
|
|
|
errs := normalize.ValidateAndNormalizeConfig(cfg)
|
2017-09-13 22:00:41 +08:00
|
|
|
if PrintValidationErrors(errs) {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("exiting due to validation errors")
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return PrintJSON(args.PrintJSONArgs, cfg)
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// PrintValidationErrors formats and prints the validation errors and warnings.
|
2017-09-13 22:00:41 +08:00
|
|
|
func PrintValidationErrors(errs []error) (fatal bool) {
|
|
|
|
if len(errs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2020-09-01 04:01:56 +08:00
|
|
|
log.Printf("%d Validation errors:\n", len(errs))
|
2017-09-13 22:00:41 +08:00
|
|
|
for _, err := range errs {
|
|
|
|
if _, ok := err.(normalize.Warning); ok {
|
2020-09-01 04:01:56 +08:00
|
|
|
log.Printf("WARNING: %s\n", err)
|
2017-09-13 22:00:41 +08:00
|
|
|
} else {
|
|
|
|
fatal = true
|
2020-09-01 04:01:56 +08:00
|
|
|
log.Printf("ERROR: %s\n", err)
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// ExecuteDSL executes the dnsconfig.js contents.
|
2017-09-13 22:00:41 +08:00
|
|
|
func ExecuteDSL(args ExecuteDSLArgs) (*models.DNSConfig, error) {
|
|
|
|
if args.JSFile == "" {
|
2020-08-31 07:52:37 +08:00
|
|
|
return nil, fmt.Errorf("no config specified")
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
2019-01-29 23:29:00 +08:00
|
|
|
|
2020-10-27 22:43:00 +08:00
|
|
|
dnsConfig, err := js.ExecuteJavascript(args.JSFile, args.DevMode, stringSliceToMap(args.Variable))
|
2017-09-13 22:00:41 +08:00
|
|
|
if err != nil {
|
2020-09-20 03:01:33 +08:00
|
|
|
return nil, fmt.Errorf("executing %s: %w", args.JSFile, err)
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
return dnsConfig, nil
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// PrintJSON outputs/prettyprints the IR data.
|
2017-09-13 22:00:41 +08:00
|
|
|
func PrintJSON(args PrintJSONArgs, config *models.DNSConfig) (err error) {
|
|
|
|
var dat []byte
|
|
|
|
if args.Pretty {
|
|
|
|
dat, err = json.MarshalIndent(config, "", " ")
|
|
|
|
} else {
|
|
|
|
dat, err = json.Marshal(config)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if args.Output != "" {
|
|
|
|
f, err := os.Create(args.Output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.Write(dat)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(dat))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func exit(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-10-27 22:43:00 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|