Skip to content

Commit

Permalink
Only pack hostname but ignore numbers in domain (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing924 authored Jan 24, 2022
1 parent beb699a commit 867ef79
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 0 additions & 7 deletions .travis.gofmt.sh

This file was deleted.

16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 9 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hostutils
import (
"regexp"
"strconv"
"strings"
)

var reComent = regexp.MustCompile(`#.*`)
Expand Down Expand Up @@ -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, ""
}
9 changes: 5 additions & 4 deletions pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
14 changes: 14 additions & 0 deletions pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}

0 comments on commit 867ef79

Please sign in to comment.