Pull request:* all: fix all staticcheck simplification and unused warnings
Merge in DNS/adguard-home from 2270-fix-s-u-warnings to master
Squashed commit of the following:
commit 03e0f78bd471057007c2d4042ee26eda2bbc9b29
Merge: 50dc3ef5c 7e16fda57
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Fri Nov 6 11:54:09 2020 +0300
Merge branch 'master' into 2270-fix-s-u-warnings
commit 50dc3ef5c44a5fdc941794c26784b0c44d7b5aa0
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Nov 5 19:48:54 2020 +0300
* all: improve code quality
commit d6d804f759ce3e47154a389b427550e72c4b9090
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Nov 5 18:03:35 2020 +0300
* all: fix all staticcheck simplification and unused warnings
Closes #2270.
This commit is contained in:
@@ -59,7 +59,7 @@ func (a *AutoHosts) Init(hostsFn string) {
|
||||
a.hostsFn = hostsFn
|
||||
}
|
||||
|
||||
if IsOpenWrt() {
|
||||
if IsOpenWRT() {
|
||||
a.hostsDirs = append(a.hostsDirs, "/tmp/hosts") // OpenWRT: "/tmp/hosts/dhcp.cfg01411c"
|
||||
}
|
||||
|
||||
@@ -106,8 +106,8 @@ func (a *AutoHosts) Close() {
|
||||
}
|
||||
}
|
||||
|
||||
// Process - get the list of IP addresses for the hostname
|
||||
// Return nil if not found
|
||||
// Process returns the list of IP addresses for the hostname or nil if nothing
|
||||
// found.
|
||||
func (a *AutoHosts) Process(host string, qtype uint16) []net.IP {
|
||||
if qtype == dns.TypePTR {
|
||||
return nil
|
||||
@@ -115,11 +115,12 @@ func (a *AutoHosts) Process(host string, qtype uint16) []net.IP {
|
||||
|
||||
var ipsCopy []net.IP
|
||||
a.lock.Lock()
|
||||
ips, _ := a.table[host]
|
||||
if len(ips) != 0 {
|
||||
|
||||
if ips, ok := a.table[host]; ok {
|
||||
ipsCopy = make([]net.IP, len(ips))
|
||||
copy(ipsCopy, ips)
|
||||
}
|
||||
|
||||
a.lock.Unlock()
|
||||
|
||||
log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy)
|
||||
@@ -256,18 +257,16 @@ func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string]string,
|
||||
func (a *AutoHosts) watcherLoop() {
|
||||
for {
|
||||
select {
|
||||
|
||||
case event, ok := <-a.watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// skip duplicate events
|
||||
repeat := true
|
||||
for repeat {
|
||||
select {
|
||||
case _ = <-a.watcher.Events:
|
||||
// skip this event
|
||||
case <-a.watcher.Events:
|
||||
// Skip this duplicating event
|
||||
default:
|
||||
repeat = false
|
||||
}
|
||||
@@ -292,18 +291,15 @@ func (a *AutoHosts) watcherLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
// updateLoop - read static hosts from system files
|
||||
// updateLoop reads static hosts from system files.
|
||||
func (a *AutoHosts) updateLoop() {
|
||||
for {
|
||||
select {
|
||||
case ok := <-a.updateChan:
|
||||
if !ok {
|
||||
log.Debug("Finished AutoHosts update loop")
|
||||
return
|
||||
}
|
||||
|
||||
a.updateHosts()
|
||||
for ok := range a.updateChan {
|
||||
if !ok {
|
||||
log.Debug("Finished AutoHosts update loop")
|
||||
return
|
||||
}
|
||||
|
||||
a.updateHosts()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,26 +10,23 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ContainsString checks if "v" is in the array "arr"
|
||||
func ContainsString(arr []string, v string) bool {
|
||||
for _, i := range arr {
|
||||
if i == v {
|
||||
// ContainsString checks if string is in the slice of strings.
|
||||
func ContainsString(strs []string, str string) bool {
|
||||
for _, s := range strs {
|
||||
if s == str {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// fileExists returns TRUE if file exists
|
||||
// FileExists returns true if file exists.
|
||||
func FileExists(fn string) bool {
|
||||
_, err := os.Stat(fn)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// runCommand runs shell command
|
||||
// RunCommand runs shell command.
|
||||
func RunCommand(command string, arguments ...string) (int, string, error) {
|
||||
cmd := exec.Command(command, arguments...)
|
||||
out, err := cmd.Output()
|
||||
@@ -71,16 +68,8 @@ func SplitNext(str *string, splitBy byte) string {
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
// MinInt - return the minimum value
|
||||
func MinInt(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// IsOpenWrt checks if OS is OpenWRT
|
||||
func IsOpenWrt() bool {
|
||||
// IsOpenWRT checks if OS is OpenWRT.
|
||||
func IsOpenWRT() bool {
|
||||
if runtime.GOOS != "linux" {
|
||||
return false
|
||||
}
|
||||
@@ -92,12 +81,3 @@ func IsOpenWrt() bool {
|
||||
|
||||
return strings.Contains(string(body), "OpenWrt")
|
||||
}
|
||||
|
||||
// IsFreeBSD checks if OS is FreeBSD
|
||||
func IsFreeBSD() bool {
|
||||
if runtime.GOOS == "freebsd" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user