mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Refactor to use better cli command framework (#177)
* starting to refactor commands * work * not sure * all commands working! * actually add file * work in delay flag again * start to refactor out console printing * i hate line endings * simple travis test to find direct output * remove all direct printing from push/preview * checkin vendor * don't need this yet * forgot to commit these * make version explicit command * some code review * Add "check" subcommand. * move stuff to commands package * fix * comment out check for printlns. for now * alphabet hax * activedir flags gone. use creds instead * active dir doc update * remove bind specific flags. creds instead * default to zones dir * fix linux build * fix test * cleanup random global* vars * Clean up PowerShell docs * rename dump-ir to print-ir. combine with print-js
This commit is contained in:
213
commands/commands.go
Normal file
213
commands/commands.go
Normal file
@ -0,0 +1,213 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/StackExchange/dnscontrol/models"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// categories of commands
|
||||
const (
|
||||
catMain = "\b main" // screwed up to alphebatize first
|
||||
catDebug = "debug"
|
||||
catUtils = "utility"
|
||||
)
|
||||
|
||||
var commands = []cli.Command{}
|
||||
var version string
|
||||
|
||||
func cmd(cat string, c *cli.Command) bool {
|
||||
c.Category = cat
|
||||
commands = append(commands, *c)
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = cmd(catDebug, &cli.Command{
|
||||
Name: "version",
|
||||
Usage: "Print version information",
|
||||
Action: func(c *cli.Context) {
|
||||
fmt.Println(version)
|
||||
},
|
||||
})
|
||||
|
||||
// Run will execute the CLI
|
||||
func Run(v string) error {
|
||||
version = v
|
||||
app := cli.NewApp()
|
||||
app.Version = version
|
||||
app.Name = "dnscontrol"
|
||||
app.HideVersion = true
|
||||
app.Usage = "dnscontrol is a compiler and DSL for managing dns zones"
|
||||
sort.Sort(cli.CommandsByName(commands))
|
||||
app.Commands = commands
|
||||
app.EnableBashCompletion = true
|
||||
app.Run(os.Args)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shared config types
|
||||
|
||||
// GetDNSConfigArgs contains what we need to get a valid dns config.
|
||||
// Could come from parsing js, or from stored json
|
||||
type GetDNSConfigArgs struct {
|
||||
ExecuteDSLArgs
|
||||
JSONFile string
|
||||
}
|
||||
|
||||
func (args *GetDNSConfigArgs) flags() []cli.Flag {
|
||||
return append(args.ExecuteDSLArgs.flags(),
|
||||
cli.StringFlag{
|
||||
Destination: &args.JSONFile,
|
||||
Name: "ir",
|
||||
Usage: "Read IR (json) directly from this file. Do not process DSL at all",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Destination: &args.JSONFile,
|
||||
Name: "json",
|
||||
Hidden: true,
|
||||
Usage: "same as -ir. only here for backwards compatibility, hence hidden",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func GetDNSConfig(args GetDNSConfigArgs) (*models.DNSConfig, error) {
|
||||
if args.JSONFile != "" {
|
||||
f, err := os.Open(args.JSONFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
dec := json.NewDecoder(f)
|
||||
cfg := &models.DNSConfig{}
|
||||
if err = dec.Decode(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
return ExecuteDSL(args.ExecuteDSLArgs)
|
||||
}
|
||||
|
||||
// ExecuteDSLArgs are used anytime we need to read and execute dnscontrol DSL
|
||||
type ExecuteDSLArgs struct {
|
||||
JSFile string
|
||||
JSONFile string
|
||||
DevMode bool
|
||||
}
|
||||
|
||||
func (args *ExecuteDSLArgs) flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "dnsconfig.js",
|
||||
Destination: &args.JSFile,
|
||||
Usage: "File containing dns config in javascript DSL",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "js",
|
||||
Value: "dnsconfig.js",
|
||||
Hidden: true,
|
||||
Destination: &args.JSFile,
|
||||
Usage: "same as config. for back compatibility",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "dev",
|
||||
Destination: &args.DevMode,
|
||||
Usage: "Use helpers.js from disk instead of embedded copy",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// PrintJSONArgs are used anytime a command may print some json
|
||||
type PrintJSONArgs struct {
|
||||
Pretty bool
|
||||
Output string
|
||||
}
|
||||
|
||||
func (args *PrintJSONArgs) flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "pretty",
|
||||
Destination: &args.Pretty,
|
||||
Usage: "Pretty print IR JSON",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "out",
|
||||
Destination: &args.Output,
|
||||
Usage: "File to write IR JSON to (default stdout)",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type GetCredentialsArgs struct {
|
||||
CredsFile string
|
||||
}
|
||||
|
||||
func (args *GetCredentialsArgs) flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "creds",
|
||||
Destination: &args.CredsFile,
|
||||
Usage: "Provider credentials JSON file",
|
||||
Value: "creds.json",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type FilterArgs struct {
|
||||
Providers string
|
||||
Domains string
|
||||
}
|
||||
|
||||
func (args *FilterArgs) flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "providers",
|
||||
Destination: &args.Providers,
|
||||
Usage: `Providers to enable (comma separated list); default is all. Can exclude individual providers from default by adding '"_exclude_from_defaults": "true"' to the credentials file for a provider`,
|
||||
Value: "",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "domains",
|
||||
Destination: &args.Domains,
|
||||
Usage: `Comma separated list of domain names to include`,
|
||||
Value: "",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (args *FilterArgs) shouldRunProvider(p string, dc *models.DomainConfig, nonDefaultProviders []string) bool {
|
||||
if args.Providers == "all" {
|
||||
return true
|
||||
}
|
||||
if args.Providers == "" {
|
||||
for _, pr := range nonDefaultProviders {
|
||||
if pr == p {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
for _, prov := range strings.Split(args.Providers, ",") {
|
||||
if prov == p {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (args *FilterArgs) shouldRunDomain(d string) bool {
|
||||
if args.Domains == "" {
|
||||
return true
|
||||
}
|
||||
for _, dom := range strings.Split(args.Domains, ",") {
|
||||
if dom == d {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user