Skip to content

Commit 867ef79

Browse files
authored
Only pack hostname but ignore numbers in domain (#5)
1 parent beb699a commit 867ef79

File tree

7 files changed

+72
-27
lines changed

7 files changed

+72
-27
lines changed

.github/workflows/ci.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
- master
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v2
19+
20+
- name: Setup Go
21+
uses: actions/setup-go@v2
22+
with:
23+
go-version: '^1.17.6'
24+
25+
- name: Go Format
26+
run: |
27+
if [ -n "$(gofmt -l .)" ]; then
28+
echo "Go code is not formatted:"
29+
gofmt -d .
30+
exit 1
31+
fi
32+
33+
- name: Go Test
34+
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic
35+
36+
- name: Codecov
37+
run: bash <(curl -s https://codecov.io/bash)
38+

.travis.gofmt.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.1.0 (2022-01-24)
4+
5+
* Support Go 1.17.
6+
* Only pack hostname but ignore numbers in domain.
7+
* Use github actions.
8+
39
## 1.0.1 (2018-10-25)
410

511
* Support go mod.

common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hostutils
33
import (
44
"regexp"
55
"strconv"
6+
"strings"
67
)
78

89
var reComent = regexp.MustCompile(`#.*`)
@@ -48,3 +49,11 @@ func regularizeHosts(hosts []string) []string {
4849
}
4950
return result
5051
}
52+
53+
func parseFQDN(fqdn string) (hostname, domain string) {
54+
tokens := strings.SplitN(fqdn, ".", 2)
55+
if len(tokens) == 2 {
56+
return tokens[0], "." + tokens[1]
57+
}
58+
return fqdn, ""
59+
}

pack.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ func Pack(hosts []string) (packedHosts []string) {
2626
func packHosts(uniqHosts []string) []string {
2727
hostGroups := make(map[string][]string)
2828
var result []string
29-
for _, host := range uniqHosts {
30-
m := reHostname.FindStringSubmatch(host)
29+
for _, fqdn := range uniqHosts {
30+
hostname, domain := parseFQDN(fqdn)
31+
m := reHostname.FindStringSubmatch(hostname)
3132
if len(m) == 0 {
32-
result = append(result, host)
33+
result = append(result, fqdn)
3334
} else {
3435
prefix := m[1]
3536
num := m[2]
3637
suffix := m[3]
37-
key := fmt.Sprintf("%s%%s%s", prefix, suffix)
38+
key := fmt.Sprintf("%s%%s%s%s", prefix, suffix, domain)
3839
hostGroups[key] = append(hostGroups[key], num)
3940
}
4041
}

pack_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ func TestPack(t *testing.T) {
9494
"example102c.com",
9595
"example0003c.com",
9696
})
97+
testPack(t,
98+
[]string{"example[101-102]c.grp1.com"},
99+
[]string{
100+
"example101c.grp1.com",
101+
"example102c.grp1.com",
102+
})
103+
testPack(t,
104+
[]string{"example[101-102]"},
105+
[]string{
106+
"example101",
107+
"example102",
108+
})
97109
}
98110

99111
func TestPackString(t *testing.T) {
@@ -124,9 +136,11 @@ func TestPackString(t *testing.T) {
124136
}
125137

126138
func testPack(t *testing.T, expected []string, input []string) {
139+
t.Helper()
127140
assert.Equal(t, expected, Pack(input))
128141
}
129142

130143
func testPackString(t *testing.T, expected []string, input string) {
144+
t.Helper()
131145
assert.Equal(t, expected, PackString(input))
132146
}

0 commit comments

Comments
 (0)