From 0aeed82d155a6e0552baca6ff4b5f02a2b3058e2 Mon Sep 17 00:00:00 2001 From: agill17 Date: Wed, 7 Jun 2023 12:58:13 -0700 Subject: [PATCH 1/3] feature: cidrNetmask --- functions.go | 30 +++++++++++++++--------------- network.go | 8 ++++++++ network_test.go | 8 ++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/functions.go b/functions.go index 57fcec1d..943cbd3c 100644 --- a/functions.go +++ b/functions.go @@ -22,8 +22,7 @@ import ( // // Use this to pass the functions into the template engine: // -// tpl := template.New("foo").Funcs(sprig.FuncMap())) -// +// tpl := template.New("foo").Funcs(sprig.FuncMap())) func FuncMap() template.FuncMap { return HtmlFuncMap() } @@ -269,6 +268,7 @@ var genericMap = map[string]interface{}{ // Network: "getHostByName": getHostByName, + "cidrNetmask": cidrNetmask, // Paths: "base": path.Base, @@ -336,20 +336,20 @@ var genericMap = map[string]interface{}{ "mustChunk": mustChunk, // Crypto: - "bcrypt": bcrypt, - "htpasswd": htpasswd, - "genPrivateKey": generatePrivateKey, - "derivePassword": derivePassword, - "buildCustomCert": buildCustomCertificate, - "genCA": generateCertificateAuthority, - "genCAWithKey": generateCertificateAuthorityWithPEMKey, - "genSelfSignedCert": generateSelfSignedCertificate, + "bcrypt": bcrypt, + "htpasswd": htpasswd, + "genPrivateKey": generatePrivateKey, + "derivePassword": derivePassword, + "buildCustomCert": buildCustomCertificate, + "genCA": generateCertificateAuthority, + "genCAWithKey": generateCertificateAuthorityWithPEMKey, + "genSelfSignedCert": generateSelfSignedCertificate, "genSelfSignedCertWithKey": generateSelfSignedCertificateWithPEMKey, - "genSignedCert": generateSignedCertificate, - "genSignedCertWithKey": generateSignedCertificateWithPEMKey, - "encryptAES": encryptAES, - "decryptAES": decryptAES, - "randBytes": randBytes, + "genSignedCert": generateSignedCertificate, + "genSignedCertWithKey": generateSignedCertificateWithPEMKey, + "encryptAES": encryptAES, + "decryptAES": decryptAES, + "randBytes": randBytes, // UUIDs: "uuidv4": uuidv4, diff --git a/network.go b/network.go index 108d78a9..aa014d26 100644 --- a/network.go +++ b/network.go @@ -10,3 +10,11 @@ func getHostByName(name string) string { //TODO: add error handing when release v3 comes out return addrs[rand.Intn(len(addrs))] } + +func cidrNetmask(cidr string) string { + _, ipnet, err := net.ParseCIDR(cidr) + if err != nil { + panic(err) + } + return net.IP(ipnet.Mask).String() +} diff --git a/network_test.go b/network_test.go index 9c153f0a..9502a630 100644 --- a/network_test.go +++ b/network_test.go @@ -16,3 +16,11 @@ func TestGetHostByName(t *testing.T) { assert.NotNil(t, ip) assert.NotEmpty(t, ip) } + +func TestCidrNetmask(t *testing.T) { + tpl := `{{"1.2.3.4/32" | cidrNetmask}}` + netmask, _ := runRaw(tpl, nil) + assert.NotNil(t, netmask) + assert.NotEmpty(t, netmask) + assert.Equalf(t, "255.255.255.255", netmask, "") +} From e3d3c1b5c66c10555f582e55a9fb7f3aad02913b Mon Sep 17 00:00:00 2001 From: agill17 Date: Wed, 7 Jun 2023 13:13:45 -0700 Subject: [PATCH 2/3] add usage documentation --- docs/network.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/network.md b/docs/network.md index ea6cfd7f..c2d51491 100644 --- a/docs/network.md +++ b/docs/network.md @@ -9,3 +9,11 @@ The `getHostByName` receives a domain name and returns the ip address. ``` getHostByName "www.google.com" would return the corresponding ip address of www.google.com ``` + +## cidrNetmask + +The `cidrNetmask` takes in a cidr and returns the netmask. + +``` +cidrNetmask "1.2.3.4/32" would return 255.255.255.255 +``` From 4c3a62a6d56d6cfb5fe762b3911a09feb84d50dd Mon Sep 17 00:00:00 2001 From: agill17 Date: Thu, 8 Jun 2023 21:34:19 -0700 Subject: [PATCH 3/3] use assert.Equal over assert.Equalf --- network_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network_test.go b/network_test.go index 9502a630..a9631134 100644 --- a/network_test.go +++ b/network_test.go @@ -22,5 +22,5 @@ func TestCidrNetmask(t *testing.T) { netmask, _ := runRaw(tpl, nil) assert.NotNil(t, netmask) assert.NotEmpty(t, netmask) - assert.Equalf(t, "255.255.255.255", netmask, "") + assert.Equal(t, "255.255.255.255", netmask) }