diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..33c3c9f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,38 @@ +name: CI + +on: + pull_request: + branches: + - main + - master + +defaults: + run: + shell: bash + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: '^1.17.6' + + - name: Go Format + run: | + if [ -n "$(gofmt -l .)" ]; then + echo "Go code is not formatted:" + gofmt -d . + exit 1 + fi + + - name: Go Test + run: go test -v -race -coverprofile=coverage.txt -covermode=atomic + + - name: Codecov + run: bash <(curl -s https://codecov.io/bash) + diff --git a/.travis.gofmt.sh b/.travis.gofmt.sh deleted file mode 100755 index bfffdca..0000000 --- a/.travis.gofmt.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -if [ -n "$(gofmt -l .)" ]; then - echo "Go code is not formatted:" - gofmt -d . - exit 1 -fi diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 72419ec..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: go - -sudo: false - -go: - - '1.8' - - '1.9' - - '1.10' - - '1.11' - -script: - - ./.travis.gofmt.sh - - go test -v -race -coverprofile=coverage.txt -covermode=atomic - -after_success: - - bash <(curl -s https://codecov.io/bash) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a714c..0839582 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## 1.1.0 (2022-01-24) + +* Support Go 1.17. +* Only pack hostname but ignore numbers in domain. +* Use github actions. + ## 1.0.1 (2018-10-25) * Support go mod. diff --git a/common.go b/common.go index 2c5a08e..b62fee5 100644 --- a/common.go +++ b/common.go @@ -3,6 +3,7 @@ package hostutils import ( "regexp" "strconv" + "strings" ) var reComent = regexp.MustCompile(`#.*`) @@ -48,3 +49,11 @@ func regularizeHosts(hosts []string) []string { } return result } + +func parseFQDN(fqdn string) (hostname, domain string) { + tokens := strings.SplitN(fqdn, ".", 2) + if len(tokens) == 2 { + return tokens[0], "." + tokens[1] + } + return fqdn, "" +} diff --git a/pack.go b/pack.go index 026ac47..249cdcd 100644 --- a/pack.go +++ b/pack.go @@ -26,15 +26,16 @@ func Pack(hosts []string) (packedHosts []string) { func packHosts(uniqHosts []string) []string { hostGroups := make(map[string][]string) var result []string - for _, host := range uniqHosts { - m := reHostname.FindStringSubmatch(host) + for _, fqdn := range uniqHosts { + hostname, domain := parseFQDN(fqdn) + m := reHostname.FindStringSubmatch(hostname) if len(m) == 0 { - result = append(result, host) + result = append(result, fqdn) } else { prefix := m[1] num := m[2] suffix := m[3] - key := fmt.Sprintf("%s%%s%s", prefix, suffix) + key := fmt.Sprintf("%s%%s%s%s", prefix, suffix, domain) hostGroups[key] = append(hostGroups[key], num) } } diff --git a/pack_test.go b/pack_test.go index c0ee3b6..1e4aea6 100644 --- a/pack_test.go +++ b/pack_test.go @@ -94,6 +94,18 @@ func TestPack(t *testing.T) { "example102c.com", "example0003c.com", }) + testPack(t, + []string{"example[101-102]c.grp1.com"}, + []string{ + "example101c.grp1.com", + "example102c.grp1.com", + }) + testPack(t, + []string{"example[101-102]"}, + []string{ + "example101", + "example102", + }) } func TestPackString(t *testing.T) { @@ -124,9 +136,11 @@ func TestPackString(t *testing.T) { } func testPack(t *testing.T, expected []string, input []string) { + t.Helper() assert.Equal(t, expected, Pack(input)) } func testPackString(t *testing.T, expected []string, input string) { + t.Helper() assert.Equal(t, expected, PackString(input)) }