Skip to content

Commit

Permalink
refactor: update implementation to use StringSliceVar cobra flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mmourick committed Mar 6, 2024
1 parent 479f1d6 commit f2ee460
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions cmd/root.go
Expand Up @@ -45,11 +45,8 @@ var (
typesToQuery := supportedQueryTypes

// If a specific query type is provided, filter the queryTypes slice to only include that type
if flagStore.UserSpecifiedQueryType != "" {
// Parse the user-specified query type(s) into a slice of query types
// Example: "A AAAA" -> []string{"A", "AAAA"}
userSpecifiedQueryTypes := strings.Fields(flagStore.UserSpecifiedQueryType)
typesToQuery = core.FilterQueryTypes(supportedQueryTypes, userSpecifiedQueryTypes)
if len(flagStore.UserSpecifiedQueryTypes) > 0 {
typesToQuery = core.FilterQueryTypes(supportedQueryTypes, flagStore.UserSpecifiedQueryTypes)
}

// Send a DNS query for each query type in the queryTypes slice
Expand All @@ -71,7 +68,7 @@ func init() {
setupCobraUsageTemplate()
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.Flags().StringVar(&flagStore.DNSServerIP, "dns-server-ip", "", "IP address of the DNS server")
rootCmd.Flags().StringVarP(&flagStore.UserSpecifiedQueryType, "query-type", "q", "", "specific query type(s) to filter on")
rootCmd.Flags().StringSliceVarP(&flagStore.UserSpecifiedQueryTypes, "query-types", "q", queryTypesToStrings(core.GetQueryTypes()), "specific query type(s) to filter on")
rootCmd.Flags().BoolVarP(&flagStore.Debug, "debug", "d", false, "verbose logging")
}

Expand All @@ -97,6 +94,14 @@ func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
return []byte(fmt.Sprintf("%s\n", entry.Message)), nil
}

func queryTypesToStrings(queryTypes []model.QueryType) []string {
var strings []string
for _, s := range queryTypes {
strings = append(strings, s.Name)
}
return strings
}

func toggleDebug(cmd *cobra.Command, args []string) {
if flagStore.Debug {
log.SetLevel(log.DebugLevel)
Expand Down
6 changes: 3 additions & 3 deletions pkg/model/flagstore.go
@@ -1,7 +1,7 @@
package model

type Flagstore struct {
DNSServerIP string
UserSpecifiedQueryType string
Debug bool
DNSServerIP string
UserSpecifiedQueryTypes []string
Debug bool
}

0 comments on commit f2ee460

Please sign in to comment.