Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: cidrNetmask #374

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
30 changes: 15 additions & 15 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -269,6 +268,7 @@ var genericMap = map[string]interface{}{

// Network:
"getHostByName": getHostByName,
"cidrNetmask": cidrNetmask,

// Paths:
"base": path.Base,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
8 changes: 8 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Equal(t, "255.255.255.255", netmask)
}