Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix linting and code style changes for go 1.23 #23

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions cmd/dns-preload/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
)

var (
//nolint:govet // fieldalignment is not required here.
cli struct {
All Preload `cmd:"" help:"preload all of the following types from the configuration file"`
Cname Preload `cmd:"" help:"preload only the cname entries from the configuration file"`
Expand All @@ -48,27 +49,27 @@ var (
)

type Preload struct {
ConfigFile string `required:"" help:"The configuration file to read the domain list to query from"`
Server string `default:"localhost" help:"The server to query to seed the domain list into"`
Port string `default:"53" help:"The port the DNS server listens for requests on"`
resolver dns.CustomResolver
ConfigFile string `required:"" help:"The configuration file to read the domain list to query from"`
Server string `default:"localhost" help:"The server to query to seed the domain list into"`
Port string `default:"53" help:"The port the DNS server listens for requests on"`
nameserver string
Timeout time.Duration `default:"30s" help:"The timeout for each DNS query to succeed (not implemented)"`
Workers uint8 `default:"2" help:"The number of concurrent goroutines used to query the DNS server"`
Mute bool `default:"false" help:"Suppress the preload task output to the console"`
Quiet bool `default:"false" help:"Suppress the preload response output to the console"`
Full bool `default:"true" help:"For record types that return a Hostname ensure that these are resolved"`
Debug bool `default:"false" help:"Debug mode"`
Timeout time.Duration `default:"30s" help:"The timeout for each DNS query to succeed (not implemented)"`
resolver dns.CustomResolver
nameserver string
}

type Config struct {
Validate struct {
ConfigFile string `required:"" help:"The configuration file to load"`
} `cmd:"" help:"Validate a configuration file"`
Quiet bool `default:"false" help:"Suppress the info output to the console"`
Generate struct {
Generate bool `default:"true" help:"Generate an empty configuration and output it to stdout"`
} `cmd:"" help:"Generate a configuration file"`
Validate struct {
ConfigFile string `required:"" help:"The configuration file to load"`
} `cmd:"" help:"Validate a configuration file"`
}

// Config Run() prints an empty YAML configuration to stdout.
Expand Down Expand Up @@ -394,10 +395,11 @@ func (p *Preload) IntroPrinter(queryType string, hosts []string) {
}

// CompletedPrinter prints out a completion message and a timer to stdout.
func completedPrinter(quiet bool, t time.Time) {
func completedPrinter(quiet bool, t time.Time) string {
if !quiet {
fmt.Printf(completedMessage+"\n", time.Since(t))
return fmt.Sprintf(completedMessage+"\n", time.Since(t))
}
return ""
}

// createErrGroup handles the logic to setup an errgroup for concurrency.
Expand Down
17 changes: 14 additions & 3 deletions cmd/dns-preload/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -956,7 +957,7 @@ func TestPreloadRun(t *testing.T) {
}
}

func Test_completedPrinter(t *testing.T) {
func TestCompletedPrinter(t *testing.T) {
start := time.Now()
type args struct {
quiet bool
Expand All @@ -981,12 +982,22 @@ func Test_completedPrinter(t *testing.T) {
quiet: false,
t: start,
},
want: "",
want: completedMessage,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
completedPrinter(tt.args.quiet, tt.args.t)
s := completedPrinter(tt.args.quiet, tt.args.t)
if tt.args.quiet {
if s != tt.want {
t.Errorf("expected nil, got %s", tt.want)
}
}
if !tt.args.quiet {
if strings.Index(strings.Split(s, "0")[0], strings.Split(tt.want, " ")[0]) != 0 {
t.Errorf("expected return to start with %s, got %s", tt.want, s)
}
}
})
}
}
Expand Down
38 changes: 20 additions & 18 deletions pkg/confighandlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

const (
Mx string = "mx"
Ns string = "ns"
Txt string = "txt"
Cname string = "cname"
Hosts string = "hosts"
Ptr string = "ptr"
Mx string = "mx"
Ns string = "ns"
Txt string = "txt"
Cname string = "cname"
Hosts string = "hosts"
Ptr string = "ptr"
nilRecords uint16 = 0
)

var (
Expand All @@ -27,23 +28,24 @@ type Configuration struct {
// QueryType lsits out the structure for the different domains and their query type
type QueryType struct {
// Cnames for doing a cname lookup
Cname []string `yaml:"cname" json:"cname" validate:"dive,fqdn"`
CnameCount uint16 `yaml:",omitempty"`
Cname []string `yaml:"cname" json:"cname" validate:"dive,fqdn"`
// Hosts for doing a query for type A and AAAA
Hosts []string `yaml:"hosts" json:"hosts" validate:"dive,fqdn"`
HostsCount uint16 `yaml:",omitempty"`
Hosts []string `yaml:"hosts" json:"hosts" validate:"dive,fqdn"`
// ns for doing a query for type NS
NS []string `yaml:"ns" json:"ns" validate:"dive,fqdn"`
NSCount uint16 `yaml:",omitempty"`
NS []string `yaml:"ns" json:"ns" validate:"dive,fqdn"`
// MX for doing a query for type MX
MX []string `yaml:"mx" json:"mx" validate:"dive,fqdn"`
MXCount uint16 `yaml:",omitempty"`
MX []string `yaml:"mx" json:"mx" validate:"dive,fqdn"`
// TXT for doing a query for type TXT
TXT []string `yaml:"txt" json:"txt" validate:"dive,fqdn"`
TXTCount uint16 `yaml:",omitempty"`
TXT []string `yaml:"txt" json:"txt" validate:"dive,fqdn"`
// PTR for doing a query for type PTR
PTR []string `yaml:"ptr" json:"ptr" validate:"dive,ip_addr"`
PTRCount uint16 `yaml:",omitempty"`
PTR []string `yaml:"ptr" json:"ptr" validate:"dive,ip_addr"`
// Metrics values below this point.
CnameCount uint16 `yaml:",omitempty"`
HostsCount uint16 `yaml:",omitempty"`
NSCount uint16 `yaml:",omitempty"`
MXCount uint16 `yaml:",omitempty"`
TXTCount uint16 `yaml:",omitempty"`
PTRCount uint16 `yaml:",omitempty"`
}

// PopulateCounts for how many domains are in each query_type.
Expand Down
2 changes: 1 addition & 1 deletion pkg/confighandlers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func LoadConfigFromFile(cfgfile *string) (*Configuration, error) {
if err != nil {
return &Configuration{}, err
}
if (cfg.QueryType.CnameCount == 0) && (cfg.QueryType.HostsCount == 0) && (cfg.QueryType.MXCount == 0) && (cfg.QueryType.PTRCount == 00) && (cfg.QueryType.TXTCount == 0) {
if (cfg.QueryType.CnameCount == nilRecords) && (cfg.QueryType.HostsCount == nilRecords) && (cfg.QueryType.MXCount == nilRecords) && (cfg.QueryType.PTRCount == nilRecords) && (cfg.QueryType.TXTCount == nilRecords) {
return &Configuration{}, fmt.Errorf("empty configuration or invalid keys")
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type Resolver struct {
client *net.Resolver
}

// NewResolver creates a custom resolver where the DNS servers are pinned.
func NewResolver(nameserver string, timeout time.Duration) *Resolver {
//nolint:revive // address is a returned function, it gets set by the caller.
return &Resolver{
client: &net.Resolver{
PreferGo: true,
Expand Down
Loading