Skip to content

Commit

Permalink
refactor: move cli parsing into cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Nov 5, 2023
1 parent 4c3eccb commit eb0147b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
53 changes: 52 additions & 1 deletion cli/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package cli

import "time"
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"

"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

type Flags struct {
Name string `short:"q" long:"qname" description:"Query name"`
Expand Down Expand Up @@ -81,3 +90,45 @@ type Flags struct {
Trace bool `long:"trace" description:"Show trace log messages"`
ShowVersion bool `short:"V" long:"version" description:"Show version and exit"`
}

// ParsePlusFlags parses a list of flags notated by +[no]flag and sets the corresponding opts fields
func ParsePlusFlags(opts *Flags, args []string) {
for _, arg := range args {
if strings.HasPrefix(arg, "+") && len(arg) > 3 {
state := arg[1:3] != "no"
flag := strings.ToLower(arg[3:])
if state {
flag = strings.ToLower(arg[1:])
}

v := reflect.Indirect(reflect.ValueOf(opts))
vT := v.Type()
for i := 0; i < v.NumField(); i++ {
fieldTag := vT.Field(i).Tag.Get("long")
if vT.Field(i).Type == reflect.TypeOf(true) && fieldTag == flag {
reflect.ValueOf(opts).Elem().Field(i).SetBool(state)
break
}
}
}
}
}

// ParseRRTypes parses a list of RR types in string format ("A", "AAAA", etc.) or integer format (1, 28, etc.)
func ParseRRTypes(t []string) (map[uint16]bool, error) {
rrTypes := make(map[uint16]bool, len(t))
for _, rrType := range t {
typeCode, ok := dns.StringToType[strings.ToUpper(rrType)]
if ok {
rrTypes[typeCode] = true
} else {
typeCode, err := strconv.Atoi(rrType)
if err != nil {
return nil, fmt.Errorf("%s is not a valid RR type", rrType)
}
log.Debugf("using RR type %d as integer", typeCode)
rrTypes[uint16(typeCode)] = true
}
}
return rrTypes, nil
}
43 changes: 4 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"io"
"net/url"
"os"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -57,29 +55,6 @@ func clearOpts() {
util.UseColor = opts.Color
}

// parsePlusFlags parses a list of flags notated by +[no]flag and sets the corresponding opts fields
func parsePlusFlags(args []string) {
for _, arg := range args {
if strings.HasPrefix(arg, "+") && len(arg) > 3 {
state := arg[1:3] != "no"
flag := strings.ToLower(arg[3:])
if state {
flag = strings.ToLower(arg[1:])
}

v := reflect.ValueOf(opts)
vT := v.Type()
for i := 0; i < v.NumField(); i++ {
fieldTag := vT.Field(i).Tag.Get("long")
if vT.Field(i).Type == reflect.TypeOf(true) && fieldTag == flag {
reflect.ValueOf(&opts).Elem().Field(i).SetBool(state)
break
}
}
}
}
}

func txtConcat(m *dns.Msg) {
var answers []dns.RR
for _, answer := range m.Answer {
Expand Down Expand Up @@ -238,7 +213,7 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation
}
os.Exit(1)
}
parsePlusFlags(args)
cli.ParsePlusFlags(&opts, args)
util.UseColor = opts.Color

if opts.Verbose {
Expand All @@ -262,19 +237,9 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation
}

// Parse requested RR types
rrTypes := make(map[uint16]bool)
for _, rrType := range opts.Types {
typeCode, ok := dns.StringToType[strings.ToUpper(rrType)]
if ok {
rrTypes[typeCode] = true
} else {
typeCode, err := strconv.Atoi(rrType)
if err != nil {
return fmt.Errorf("%s is not a valid RR type", rrType)
}
log.Debugf("using RR type %d as integer", typeCode)
rrTypes[uint16(typeCode)] = true
}
rrTypes, err := cli.ParseRRTypes(opts.Types)
if err != nil {
return err
}

// Add non-flag RR types
Expand Down

0 comments on commit eb0147b

Please sign in to comment.