From 25fd34c51459d9b3b9d5fde4060902228a5569ec Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Thu, 9 Dec 2021 18:26:56 +0300 Subject: [PATCH] Pull request: all: get rid of labels Merge in DNS/adguard-home from rm-labels to master Squashed commit of the following: commit 5e3688ed92b0f76a47078a55bc22c401422c914c Author: Eugene Burkov Date: Thu Dec 9 17:46:50 2021 +0300 all: imp code, docs commit 123d1ec52d0037315e8de94ab5a26b48cf0bf984 Author: Eugene Burkov Date: Thu Dec 9 17:14:05 2021 +0300 all: get rid of labels --- internal/aghnet/hostscontainer.go | 15 ++++++++------- internal/aghos/os.go | 24 ++++++++++++++---------- internal/aghos/os_test.go | 6 +++--- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/internal/aghnet/hostscontainer.go b/internal/aghnet/hostscontainer.go index 1e1808bc..ca8631c2 100644 --- a/internal/aghnet/hostscontainer.go +++ b/internal/aghnet/hostscontainer.go @@ -309,21 +309,22 @@ func (hp *hostsParser) parseLine(line string) (ip net.IP, hosts []string) { return nil, nil } -loop: for _, f := range fields[1:] { switch hashIdx := strings.IndexByte(f, '#'); hashIdx { - case 0: - // The rest of the fields are a part of the comment so skip - // immediately. - break loop case -1: hosts = append(hosts, f) + + continue + case 0: + // Go on. default: // Only a part of the field is a comment. hosts = append(hosts, f[:hashIdx]) - - break loop } + + // The rest of the fields are a part of the comment so skip + // immediately. + break } return ip, hosts diff --git a/internal/aghos/os.go b/internal/aghos/os.go index 1723e001..5adb65b1 100644 --- a/internal/aghos/os.go +++ b/internal/aghos/os.go @@ -91,7 +91,7 @@ func PIDByCommand(command string, except ...int) (pid int, err error) { } var instNum int - pid, instNum, err = parsePSOutput(stdout, command, except...) + pid, instNum, err = parsePSOutput(stdout, command, except) if err != nil { return 0, err } @@ -125,9 +125,8 @@ func PIDByCommand(command string, except ...int) (pid int, err error) { // 1230 some/base/path/example-cmd // 3210 example-cmd // -func parsePSOutput(r io.Reader, cmdName string, ignore ...int) (largest, instNum int, err error) { +func parsePSOutput(r io.Reader, cmdName string, ignore []int) (largest, instNum int, err error) { s := bufio.NewScanner(r) -ScanLoop: for s.Scan() { fields := strings.Fields(s.Text()) if len(fields) != 2 || path.Base(fields[1]) != cmdName { @@ -135,16 +134,10 @@ ScanLoop: } cur, aerr := strconv.Atoi(fields[0]) - if aerr != nil || cur < 0 { + if aerr != nil || cur < 0 || intIn(cur, ignore) { continue } - for _, pid := range ignore { - if cur == pid { - continue ScanLoop - } - } - instNum++ if cur > largest { largest = cur @@ -157,6 +150,17 @@ ScanLoop: return largest, instNum, nil } +// intIn returns true if nums contains n. +func intIn(n int, nums []int) (ok bool) { + for _, nn := range nums { + if n == nn { + return true + } + } + + return false +} + // IsOpenWrt returns true if host OS is OpenWrt. func IsOpenWrt() (ok bool) { return isOpenWrt() diff --git a/internal/aghos/os_test.go b/internal/aghos/os_test.go index 0ca567d8..f56a93ac 100644 --- a/internal/aghos/os_test.go +++ b/internal/aghos/os_test.go @@ -63,7 +63,7 @@ func TestLargestLabeled(t *testing.T) { r := bytes.NewReader(tc.data) t.Run(tc.name, func(t *testing.T) { - pid, instNum, err := parsePSOutput(r, comm) + pid, instNum, err := parsePSOutput(r, comm, nil) require.NoError(t, err) assert.Equal(t, tc.wantPID, pid) @@ -76,7 +76,7 @@ func TestLargestLabeled(t *testing.T) { require.NoError(t, err) target := &aghio.LimitReachedError{} - _, _, err = parsePSOutput(lr, "") + _, _, err = parsePSOutput(lr, "", nil) require.ErrorAs(t, err, &target) assert.EqualValues(t, 0, target.Limit) @@ -89,7 +89,7 @@ func TestLargestLabeled(t *testing.T) { `3` + comm + nl, )) - pid, instances, err := parsePSOutput(r, comm, 1, 3) + pid, instances, err := parsePSOutput(r, comm, []int{1, 3}) require.NoError(t, err) assert.Equal(t, 2, pid)