From 3b41158aea1e81fe28c8e8f4d9f22c8cdf1e7465 Mon Sep 17 00:00:00 2001 From: Patrik Lundin Date: Mon, 21 Oct 2024 11:55:20 +0200 Subject: [PATCH] Run gofumpt on codebase ``` gofumpt -l -w . ``` --- README.md | 2 +- cmd/root.go | 8 +++++--- pkg/runner/mqtt.go | 2 -- pkg/runner/runner.go | 17 +++-------------- pkg/runner/runner_test.go | 32 +++++++++++--------------------- 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 67e932d..e256a63 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ go tool pprof http://127.0.0.1:2112/debug/pprof/profile?seconds=30 When working with this code at least the following tools are expected to be run at the top level directory prior to commiting: -* `go fmt ./...` +* `gofumpt -l -w .` (see [gofumpt](https://github.com/mvdan/gofumpt)) * `go vet ./...` * `staticcheck ./...` (see [staticcheck](https://staticcheck.io)) * `gosec ./...` (see [gosec](https://github.com/securego/gosec)) diff --git a/cmd/root.go b/cmd/root.go index c59cd2a..267b4ff 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,8 +8,10 @@ import ( "github.com/spf13/viper" ) -var cfgFile string -var edmLogger *slog.Logger +var ( + cfgFile string + edmLogger *slog.Logger +) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -44,7 +46,7 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. - //rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } // initConfig reads in config file and ENV variables if set. diff --git a/pkg/runner/mqtt.go b/pkg/runner/mqtt.go index 699b3b4..ae02789 100644 --- a/pkg/runner/mqtt.go +++ b/pkg/runner/mqtt.go @@ -15,7 +15,6 @@ import ( ) func (edm *dnstapMinimiser) newAutoPahoClientConfig(caCertPool *x509.CertPool, server string, clientID string, clientCert tls.Certificate, mqttKeepAlive uint16) (autopaho.ClientConfig, error) { - u, err := url.Parse(server) if err != nil { return autopaho.ClientConfig{}, fmt.Errorf("newAutoPahoClientConfig: unable to parse URL: %w", err) @@ -46,7 +45,6 @@ func (edm *dnstapMinimiser) newAutoPahoClientConfig(caCertPool *x509.CertPool, s } return cliCfg, nil - } func (edm *dnstapMinimiser) runAutoPaho(cm *autopaho.ConnectionManager, topic string, mqttJWK jwk.Key) { diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index b15f046..564c293 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -59,7 +59,6 @@ const dawgNotFound = -1 type edmStatusBits uint64 func (dsb *edmStatusBits) String() string { - if *dsb >= edmStatusMax { return fmt.Sprintf("unknown flags in status: %b", *dsb) } @@ -275,7 +274,6 @@ func (edm *dnstapMinimiser) reverseLabelsBounded(labels []string, maxLen int) [] boundedReverseLabels = append(boundedReverseLabels, strings.Join(remainderLabels, ".")) } - } return boundedReverseLabels } @@ -344,7 +342,6 @@ func getCryptopanAESKey(key string, salt string) []byte { } func (edm *dnstapMinimiser) setCryptopan(key string, salt string, cacheEntries int) error { - var cpnCache *lru.Cache[netip.Addr, netip.Addr] var err error @@ -719,7 +716,6 @@ func (edm *dnstapMinimiser) registerFSWatcher(filename string, callback func(str } func Run(logger *slog.Logger) { - if viper.GetBool("debug-enable-blockprofiling") { logger.Info("enabling blocking profiling") runtime.SetBlockProfileRate(int(time.Millisecond)) @@ -936,7 +932,7 @@ func Run(logger *slog.Logger) { if debugDnstapFilename != "" { // Make gosec happy debugDnstapFilename := filepath.Clean(debugDnstapFilename) - debugDnstapFile, err = os.OpenFile(debugDnstapFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) + debugDnstapFile, err = os.OpenFile(debugDnstapFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) if err != nil { edm.log.Error("unable to open debug dnstap file", "error", err.Error(), "filename", debugDnstapFilename) os.Exit(1) @@ -1038,7 +1034,6 @@ type dnstapMinimiser struct { } func createCryptopan(key string, salt string) (*cryptopan.Cryptopan, error) { - cryptopanKey := getCryptopanAESKey(key, salt) cpn, err := cryptopan.New(cryptopanKey) @@ -1172,7 +1167,6 @@ type wellKnownDomainsData struct { } func newWellKnownDomainsTracker(dawgFinder dawg.Finder, dawgModTime time.Time) (*wellKnownDomainsTracker, error) { - return &wellKnownDomainsTracker{ wellKnownDomainsData: wellKnownDomainsData{ m: map[int]*histogramData{}, @@ -1225,7 +1219,6 @@ type wkdUpdate struct { } func (wkd *wellKnownDomainsTracker) lookup(msg *dns.Msg) (int, bool, time.Time) { - wkd.mutex.RLock() defer wkd.mutex.RUnlock() @@ -1266,7 +1259,6 @@ func (wkd *wellKnownDomainsTracker) updateRetryer(edm *dnstapMinimiser, wg *sync } func (wkd *wellKnownDomainsTracker) sendUpdate(ipBytes []byte, msg *dns.Msg, dawgIndex int, suffixMatch bool, dawgModTime time.Time) { - wu := wkdUpdate{ dawgIndex: dawgIndex, suffixMatch: suffixMatch, @@ -1319,7 +1311,6 @@ func (wkd *wellKnownDomainsTracker) sendUpdate(ipBytes []byte, msg *dns.Msg, daw } func (wkd *wellKnownDomainsTracker) rotateTracker(edm *dnstapMinimiser, dawgFile string, rotationTime time.Time) (*wellKnownDomainsData, error) { - dawgFileChanged := false var dawgFinder dawg.Finder @@ -1567,7 +1558,6 @@ minimiserLoop: } func (edm *dnstapMinimiser) monitorChannelLen() { - for { edm.promNewQnameChannelLen.Set(float64(len(edm.newQnamePublisherCh))) time.Sleep(time.Second * 1) @@ -1708,7 +1698,7 @@ func (edm *dnstapMinimiser) renameFile(src string, dst string) error { // If the destionation directory does not exist we will // need to create it and then retry the Rename() in the // next iteration of the loop. - err = os.MkdirAll(dstDir, 0750) + err = os.MkdirAll(dstDir, 0o750) if err != nil { return fmt.Errorf("renameFile: unable to create destination dir: %s: %w", dstDir, err) } @@ -1739,7 +1729,7 @@ func (edm *dnstapMinimiser) createFile(dst string) (*os.File, error) { // If the destionation directory does not exist we will // need to create it and then retry the file Create() // the next iteration of the loop. - err = os.MkdirAll(dstDir, 0750) + err = os.MkdirAll(dstDir, 0o750) if err != nil { return nil, fmt.Errorf("createFile: unable to create destination dir: %s: %w", dstDir, err) } @@ -2025,7 +2015,6 @@ func getStartTimeFromRotationTime(rotationTime time.Time) time.Time { } func (edm *dnstapMinimiser) writeHistogramParquet(prevWellKnownDomainsData *wellKnownDomainsData, labelLimit int, outboxDir string) error { - if prevWellKnownDomainsData.dawgIsRotated { defer func() { err := prevWellKnownDomainsData.dawgFinder.Close() diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 9fde94d..ce91014 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -68,7 +68,6 @@ func BenchmarkSetHistogramLabels(b *testing.B) { } func TestWKD(t *testing.T) { - domainList := []string{ "example.com.", // exact match ".example.net.", // suffix match @@ -86,7 +85,7 @@ func TestWKD(t *testing.T) { dFinder := dBuilder.Finish() - var wkdDawgIndexTests = []struct { + wkdDawgIndexTests := []struct { name string domain string found bool @@ -171,7 +170,7 @@ func TestWKD(t *testing.T) { t.Fatalf("unable to set HLL defaults: %s", err) } - var wkdLookupTests = []struct { + wkdLookupTests := []struct { name string domain string known bool @@ -210,7 +209,6 @@ func TestWKD(t *testing.T) { t.Fatalf("%s: unexpected known status, have: %t, want: %t", test.name, known, test.known) } } - } func TestIgnoredClientIPsValid(t *testing.T) { @@ -241,7 +239,7 @@ func TestIgnoredClientIPsValid(t *testing.T) { t.Fatalf("unexpected number of CIDRs parsed from '%s': have: %d, want: %d", testdataFile1, numCIDRs, expectedNumCIDRs) } - var ipLookupTests = []struct { + ipLookupTests := []struct { name string ip netip.Addr ignored bool @@ -322,7 +320,7 @@ func TestIgnoredClientIPsValid(t *testing.T) { t.Fatalf("unexpected number of CIDRs parsed from '%s': have: %d, want: %d", testdataFile2, numCIDRs, expectedNumCIDRs) } - var ipLookupTests2 = []struct { + ipLookupTests2 := []struct { name string ip netip.Addr ignored bool @@ -425,7 +423,7 @@ func TestIgnoredClientIPsEmptyLinesComments(t *testing.T) { t.Fatalf("unexpected number of CIDRs parsed from '%s': have: %d, want: %d", testdataFile, numCIDRs, expectedNumCIDRs) } - var ipLookupTests = []struct { + ipLookupTests := []struct { name string ip netip.Addr ignored bool @@ -513,7 +511,7 @@ func TestIgnoredClientIPsEmpty(t *testing.T) { t.Fatalf("edm.ignoredClientsIPSet should be nil, have: %#v", edm.ignoredClientsIPSet) } - var ipLookupTests = []struct { + ipLookupTests := []struct { name string ip netip.Addr ignored bool @@ -587,7 +585,7 @@ func TestIgnoredClientIPsUnset(t *testing.T) { t.Fatalf("unexpected number of CIDRs parsed from '%s': have: %d, want: %d", "", numCIDRs, expectedNumCIDRs) } - var ipLookupTests = []struct { + ipLookupTests := []struct { name string ip netip.Addr ignored bool @@ -703,7 +701,7 @@ func TestIgnoredQuestionNamesValid(t *testing.T) { t.Fatalf("unexpected number of names parsed from '%s': have: %d, want: %d", testdataFile1, edm.ignoredQuestions.NumAdded(), expectedNumNames) } - var questionLookupTests = []struct { + questionLookupTests := []struct { name string question string ignored bool @@ -770,7 +768,7 @@ func TestIgnoredQuestionNamesValid(t *testing.T) { t.Fatalf("unexpected number of names parsed from '%s': have: %d, want: %d", testdataFile2, edm.ignoredQuestions.NumAdded(), expectedNumNames) } - var questionLookupTests2 = []struct { + questionLookupTests2 := []struct { name string question string ignored bool @@ -866,7 +864,7 @@ func TestIgnoredQuestionNamesEmpty(t *testing.T) { // Try to look for things that was present in the initial valid data // that was loaded, none of it should be considered ignored now. - var questionLookupTests = []struct { + questionLookupTests := []struct { name string question string ignored bool @@ -948,7 +946,7 @@ func TestIgnoredQuestionNamesUnset(t *testing.T) { // Try to look for things that was present in the initial valid data // that was loaded, none of it should be considered ignored now. - var questionLookupTests = []struct { + questionLookupTests := []struct { name string question string ignored bool @@ -1132,7 +1130,6 @@ func TestSetSessionLabels(t *testing.T) { } func TestEDMStatusBitsMulti(t *testing.T) { - expectedString := "well-known-exact|well-known-wildcard" dsb := new(edmStatusBits) @@ -1145,7 +1142,6 @@ func TestEDMStatusBitsMulti(t *testing.T) { } func TestEDMStatusBitsSingle(t *testing.T) { - expectedString := "well-known-exact" dsb := new(edmStatusBits) @@ -1157,7 +1153,6 @@ func TestEDMStatusBitsSingle(t *testing.T) { } func TestEDMStatusBitsMax(t *testing.T) { - expectedString := "unknown flags in status" dsb := new(edmStatusBits) @@ -1169,7 +1164,6 @@ func TestEDMStatusBitsMax(t *testing.T) { } func TestEDMStatusBitsUnknown(t *testing.T) { - expectedString := "unknown flags in status" dsb := new(edmStatusBits) @@ -1181,7 +1175,6 @@ func TestEDMStatusBitsUnknown(t *testing.T) { } func TestEDMIPBytesToInt(t *testing.T) { - ipv4AddrString := "198.51.100.15" ip4Addr, err := netip.ParseAddr(ipv4AddrString) @@ -1209,7 +1202,6 @@ func TestEDMIPBytesToInt(t *testing.T) { } func TestEDMIP6BytesToInt(t *testing.T) { - ipv6AddrString := "2001:db8:1122:3344:5566:7788:99aa:bbcc" ip6Addr, err := netip.ParseAddr(ipv6AddrString) @@ -1558,7 +1550,6 @@ func TestPseudonymiseDnstap(t *testing.T) { if uncachedPseudoRespAddr6 != expectedPseudoRespAddr6 { t.Fatalf("uncached pseudonymised IPv6 resp address %s is not the expected address %s", uncachedPseudoRespAddr6, expectedPseudoRespAddr6) } - } func BenchmarkPseudonymiseDnstapWithCache4(b *testing.B) { @@ -1718,7 +1709,6 @@ func BenchmarkMurmurSum64(b *testing.B) { } func TestCompareMurmurHashing(t *testing.T) { - murmur3Hasher := murmur3.New64() ipAddrs := []string{"198.51.100.20", "198.51.100.21", "198.51.100.22"}