-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
779 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
module github.com/Wing924/hostutils | ||
|
||
go 1.20 | ||
|
||
require github.com/stretchr/testify v1.2.2 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.2.2 | ||
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hostutils | ||
|
||
import ( | ||
"golang.org/x/exp/constraints" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
reComment = regexp.MustCompile(`#.*`) | ||
reSpaces = regexp.MustCompile(`\s+`) | ||
rePackedHost = regexp.MustCompile(`^([^\[]*)\[([-,:0-9\s]+)](.*)$`) | ||
reCondSpace = regexp.MustCompile(`,\s*`) | ||
reCondBlk = regexp.MustCompile(`^(\d+)([-:](\d+))?$`) | ||
) | ||
|
||
func max[T constraints.Ordered](a T, b T) T { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func regularizeHosts(hosts []string) []string { | ||
if hosts == nil { | ||
return nil | ||
} | ||
uniqHosts := make(map[string]bool) | ||
for _, host := range hosts { | ||
noCmtHosts := reComment.ReplaceAllString(host, "") | ||
for _, h := range reSpaces.Split(noCmtHosts, -1) { | ||
if h != "" { | ||
uniqHosts[h] = true | ||
} | ||
} | ||
} | ||
result := make([]string, len(uniqHosts)) | ||
var i = 0 | ||
for host := range uniqHosts { | ||
result[i] = host | ||
i++ | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package hostutils | ||
|
||
// Normalize Unpack and pack hosts | ||
func Normalize(hosts []string) (packedHosts []string) { | ||
return Pack(Unpack(hosts)) | ||
} | ||
|
||
// NormalizeString Unpack and pack hosts | ||
func NormalizeString(hosts string) (packedHosts []string) { | ||
return Pack(Unpack([]string{hosts})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package hostutils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNormalize(t *testing.T) { | ||
testNormalize(t, nil, nil) | ||
testNormalize(t, []string{}, nil) | ||
testNormalize(t, []string{"ww[101]"}, []string{"ww101"}) | ||
testNormalize(t, []string{"ww[101-103]", "ww[104]"}, []string{"ww[101-104]"}) | ||
testNormalize(t, []string{"a[101,103,105]", "a[102,104,107]"}, []string{"a[101-105,107]"}) | ||
testNormalize(t, []string{"[101-103]", "104"}, []string{"[101-104]"}) | ||
} | ||
|
||
func TestNormalizeString(t *testing.T) { | ||
testNormalizeString(t, "", nil) | ||
testNormalizeString(t, "ww[101]", []string{"ww101"}) | ||
testNormalizeString(t, "ww[101-103] ww[104]", []string{"ww[101-104]"}) | ||
testNormalizeString(t, "a[101,103,105] a[102,104,107]", []string{"a[101-105,107]"}) | ||
testNormalizeString(t, "[101-103] 104", []string{"[101-104]"}) | ||
} | ||
|
||
func testNormalize(t *testing.T, input []string, expected []string) { | ||
assert.Equal(t, expected, Normalize(input)) | ||
} | ||
|
||
func testNormalizeString(t *testing.T, input string, expected []string) { | ||
assert.Equal(t, expected, NormalizeString(input)) | ||
} |
Oops, something went wrong.