1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

CHORE: Fix golint and staticcheck errors/warnings (#2717)

This commit is contained in:
Tom Limoncelli
2023-12-11 16:24:11 -05:00
committed by GitHub
parent 6e90946b15
commit 790513a170
10 changed files with 56 additions and 13 deletions

View File

@@ -53,6 +53,7 @@ type Change struct {
HintRecordSetLen1 bool HintRecordSetLen1 bool
} }
// GetType returns the type of a change.
func (c Change) GetType() dnsgraph.NodeType { func (c Change) GetType() dnsgraph.NodeType {
if c.Type == REPORT { if c.Type == REPORT {
return dnsgraph.Report return dnsgraph.Report
@@ -61,10 +62,12 @@ func (c Change) GetType() dnsgraph.NodeType {
return dnsgraph.Change return dnsgraph.Change
} }
// GetName returns the FQDN of the host being changed.
func (c Change) GetName() string { func (c Change) GetName() string {
return c.Key.NameFQDN return c.Key.NameFQDN
} }
// GetDependencies returns the depenencies of a change.
func (c Change) GetDependencies() []dnsgraph.Dependency { func (c Change) GetDependencies() []dnsgraph.Dependency {
var dependencies []dnsgraph.Dependency var dependencies []dnsgraph.Dependency

View File

@@ -1,5 +1,6 @@
package dnsgraph package dnsgraph
// CreateDependencies creates a dependency list from a list of FQDNs and a DepenendyType.
func CreateDependencies(dependencyFQDNs []string, dependencyType DependencyType) []Dependency { func CreateDependencies(dependencyFQDNs []string, dependencyType DependencyType) []Dependency {
var dependencies []Dependency var dependencies []Dependency

View File

@@ -5,18 +5,23 @@ import "github.com/StackExchange/dnscontrol/v4/pkg/dnstree"
type edgeDirection uint8 type edgeDirection uint8
const ( const (
// IncomingEdge is an edge inbound.
IncomingEdge edgeDirection = iota IncomingEdge edgeDirection = iota
// OutgoingEdge is an edge outbound.
OutgoingEdge OutgoingEdge
) )
// DNSGraphEdge an edge on the graph.
type DNSGraphEdge[T Graphable] struct { type DNSGraphEdge[T Graphable] struct {
Dependency Dependency Dependency Dependency
Node *DNSGraphNode[T] Node *DNSGraphNode[T]
Direction edgeDirection Direction edgeDirection
} }
// DNSGraphEdges a list of edges.
type DNSGraphEdges[T Graphable] []DNSGraphEdge[T] type DNSGraphEdges[T Graphable] []DNSGraphEdge[T]
// DNSGraphNode a node in the graph.
type DNSGraphNode[T Graphable] struct { type DNSGraphNode[T Graphable] struct {
Data T Data T
Edges DNSGraphEdges[T] Edges DNSGraphEdges[T]
@@ -24,11 +29,13 @@ type DNSGraphNode[T Graphable] struct {
type dnsGraphNodes[T Graphable] []*DNSGraphNode[T] type dnsGraphNodes[T Graphable] []*DNSGraphNode[T]
// DNSGraph a graph.
type DNSGraph[T Graphable] struct { type DNSGraph[T Graphable] struct {
All dnsGraphNodes[T] All dnsGraphNodes[T]
Tree *dnstree.DomainTree[dnsGraphNodes[T]] Tree *dnstree.DomainTree[dnsGraphNodes[T]]
} }
// CreateGraph returns a graph.
func CreateGraph[T Graphable](entries []T) *DNSGraph[T] { func CreateGraph[T Graphable](entries []T) *DNSGraph[T] {
graph := &DNSGraph[T]{ graph := &DNSGraph[T]{
All: dnsGraphNodes[T]{}, All: dnsGraphNodes[T]{},
@@ -48,6 +55,7 @@ func CreateGraph[T Graphable](entries []T) *DNSGraph[T] {
return graph return graph
} }
// RemoveNode removes a node from a graph.
func (graph *DNSGraph[T]) RemoveNode(toRemove *DNSGraphNode[T]) { func (graph *DNSGraph[T]) RemoveNode(toRemove *DNSGraphNode[T]) {
for _, edge := range toRemove.Edges { for _, edge := range toRemove.Edges {
edge.Node.Edges = edge.Node.Edges.RemoveNode(toRemove) edge.Node.Edges = edge.Node.Edges.RemoveNode(toRemove)
@@ -62,6 +70,7 @@ func (graph *DNSGraph[T]) RemoveNode(toRemove *DNSGraphNode[T]) {
} }
} }
// AddNode adds a node to a graph.
func (graph *DNSGraph[T]) AddNode(data T) { func (graph *DNSGraph[T]) AddNode(data T) {
nodes := graph.Tree.Get(data.GetName()) nodes := graph.Tree.Get(data.GetName())
node := &DNSGraphNode[T]{ node := &DNSGraphNode[T]{
@@ -77,6 +86,7 @@ func (graph *DNSGraph[T]) AddNode(data T) {
graph.Tree.Set(data.GetName(), nodes) graph.Tree.Set(data.GetName(), nodes)
} }
// AddEdge adds an edge to a graph.
func (graph *DNSGraph[T]) AddEdge(sourceNode *DNSGraphNode[T], dependency Dependency) { func (graph *DNSGraph[T]) AddEdge(sourceNode *DNSGraphNode[T], dependency Dependency) {
destinationNodes := graph.Tree.Get(dependency.NameFQDN) destinationNodes := graph.Tree.Get(dependency.NameFQDN)
@@ -107,6 +117,7 @@ func (graph *DNSGraph[T]) AddEdge(sourceNode *DNSGraphNode[T], dependency Depend
} }
} }
// RemoveNode removes a node from a graph.
func (nodes dnsGraphNodes[T]) RemoveNode(toRemove *DNSGraphNode[T]) dnsGraphNodes[T] { func (nodes dnsGraphNodes[T]) RemoveNode(toRemove *DNSGraphNode[T]) dnsGraphNodes[T] {
var newNodes dnsGraphNodes[T] var newNodes dnsGraphNodes[T]
@@ -119,6 +130,7 @@ func (nodes dnsGraphNodes[T]) RemoveNode(toRemove *DNSGraphNode[T]) dnsGraphNode
return newNodes return newNodes
} }
// RemoveNode removes a node from a graph.
func (edges DNSGraphEdges[T]) RemoveNode(toRemove *DNSGraphNode[T]) DNSGraphEdges[T] { func (edges DNSGraphEdges[T]) RemoveNode(toRemove *DNSGraphNode[T]) DNSGraphEdges[T] {
var newEdges DNSGraphEdges[T] var newEdges DNSGraphEdges[T]
@@ -131,6 +143,7 @@ func (edges DNSGraphEdges[T]) RemoveNode(toRemove *DNSGraphNode[T]) DNSGraphEdge
return newEdges return newEdges
} }
// Contains returns true if a node is in the graph AND is in that direction.
func (edges DNSGraphEdges[T]) Contains(toFind *DNSGraphNode[T], direction edgeDirection) bool { func (edges DNSGraphEdges[T]) Contains(toFind *DNSGraphNode[T], direction edgeDirection) bool {
for _, edge := range edges { for _, edge := range edges {

View File

@@ -1,30 +1,39 @@
package dnsgraph package dnsgraph
// NodeType enumerates the node types.
type NodeType uint8 type NodeType uint8
const ( const (
// Change is the type of change.
Change NodeType = iota Change NodeType = iota
// Report is a Report.
Report Report
) )
// DependencyType enumerates the dependency types.
type DependencyType uint8 type DependencyType uint8
const ( const (
// ForwardDependency is a forward dependency.
ForwardDependency DependencyType = iota ForwardDependency DependencyType = iota
// BackwardDependency is a backwards dependency.
BackwardDependency BackwardDependency
) )
// Dependency is a dependency.
type Dependency struct { type Dependency struct {
NameFQDN string NameFQDN string
Type DependencyType Type DependencyType
} }
// Graphable is an interface for things that can be in a graph.
type Graphable interface { type Graphable interface {
GetType() NodeType GetType() NodeType
GetName() string GetName() string
GetDependencies() []Dependency GetDependencies() []Dependency
} }
// GetRecordsNamesForGraphables returns names in a graph.
func GetRecordsNamesForGraphables[T Graphable](graphables []T) []string { func GetRecordsNamesForGraphables[T Graphable](graphables []T) []string {
var names []string var names []string

View File

@@ -2,24 +2,29 @@ package testutils
import "github.com/StackExchange/dnscontrol/v4/pkg/dnsgraph" import "github.com/StackExchange/dnscontrol/v4/pkg/dnsgraph"
// StubRecord stub
type StubRecord struct { type StubRecord struct {
NameFQDN string NameFQDN string
Dependencies []dnsgraph.Dependency Dependencies []dnsgraph.Dependency
Type dnsgraph.NodeType Type dnsgraph.NodeType
} }
// GetType stub
func (record StubRecord) GetType() dnsgraph.NodeType { func (record StubRecord) GetType() dnsgraph.NodeType {
return record.Type return record.Type
} }
// GetName stub
func (record StubRecord) GetName() string { func (record StubRecord) GetName() string {
return record.NameFQDN return record.NameFQDN
} }
// GetDependencies stub
func (record StubRecord) GetDependencies() []dnsgraph.Dependency { func (record StubRecord) GetDependencies() []dnsgraph.Dependency {
return record.Dependencies return record.Dependencies
} }
// StubRecordsAsGraphable stub
func StubRecordsAsGraphable(records []StubRecord) []dnsgraph.Graphable { func StubRecordsAsGraphable(records []StubRecord) []dnsgraph.Graphable {
sortableRecords := make([]dnsgraph.Graphable, len(records)) sortableRecords := make([]dnsgraph.Graphable, len(records))

View File

@@ -2,7 +2,10 @@ package dnssort
import "github.com/StackExchange/dnscontrol/v4/pkg/dnsgraph" import "github.com/StackExchange/dnscontrol/v4/pkg/dnsgraph"
// SortResult is the result of a sort function.
type SortResult[T dnsgraph.Graphable] struct { type SortResult[T dnsgraph.Graphable] struct {
SortedRecords []T // SortedRecords is the sorted records.
SortedRecords []T
// UnresolvedRecords is the records that could not be resolved.
UnresolvedRecords []T UnresolvedRecords []T
} }

View File

@@ -36,14 +36,24 @@ func EncodeQuoted(t string) string {
return txtEncode(ToChunks(t)) return txtEncode(ToChunks(t))
} }
// State denotes the parser state.
type State int type State int
const ( const (
StateStart State = iota // Looking for a non-space // StateStart indicates parser is looking for a non-space
StateUnquoted // A run of unquoted text StateStart State = iota
StateQuoted // Quoted text
StateBackslash // last char was backlash in a quoted string // StateUnquoted indicates parser is in a run of unquoted text
StateWantSpace // expect space after closing quote StateUnquoted
// StateQuoted indicates parser is in quoted text
StateQuoted
// StateBackslash indicates the last char was backlash in a quoted string
StateBackslash
// StateWantSpace indicates parser expects a space (the previous token was a closing quote)
StateWantSpace
) )
func isRemaining(s string, i, r int) bool { func isRemaining(s string, i, r int) bool {

View File

@@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"path" "path"
@@ -69,7 +69,7 @@ func dnssdkDo(ctx context.Context, c *dnssdk.Client, apiKey string, method, uri
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= http.StatusMultipleChoices { if resp.StatusCode >= http.StatusMultipleChoices {
all, _ := ioutil.ReadAll(resp.Body) all, _ := io.ReadAll(resp.Body)
e := dnssdk.APIError{ e := dnssdk.APIError{
StatusCode: resp.StatusCode, StatusCode: resp.StatusCode,
} }

View File

@@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
@@ -85,7 +84,7 @@ func (n *mythicBeastsProvider) GetZoneRecords(domain string, meta map[string]str
return nil, err return nil, err
} }
if got, want := resp.StatusCode, 200; got != want { if got, want := resp.StatusCode, 200; got != want {
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("got HTTP %v, want %v: %v", got, want, string(body)) return nil, fmt.Errorf("got HTTP %v, want %v: %v", got, want, string(body))
} }
return zoneFileToRecords(resp.Body, domain) return zoneFileToRecords(resp.Body, domain)
@@ -138,7 +137,7 @@ func (n *mythicBeastsProvider) GetZoneRecordsCorrections(dc *models.DomainConfig
return err return err
} }
if got, want := resp.StatusCode, 200; got != want { if got, want := resp.StatusCode, 200; got != want {
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("got HTTP %v, want %v: %v", got, want, string(body)) return fmt.Errorf("got HTTP %v, want %v: %v", got, want, string(body))
} }
return nil return nil

View File

@@ -1,7 +1,7 @@
package porkbun package porkbun
func (client *porkbunProvider) ListZones() ([]string, error) { func (c *porkbunProvider) ListZones() ([]string, error) {
zones, err := client.listAllDomains() zones, err := c.listAllDomains()
if err != nil { if err != nil {
return nil, err return nil, err
} }