Skip to content

Commit

Permalink
ref: make IsIPv4()/IsIPv6() public func in utils pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhop committed Aug 10, 2024
1 parent 5ddfe49 commit eb0e624
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
5 changes: 3 additions & 2 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/tjhop/mango/internal/inventory"
"github.com/tjhop/mango/internal/shell"
"github.com/tjhop/mango/pkg/utils"
)

type contextKey string
Expand Down Expand Up @@ -51,8 +52,8 @@ func (mgr *Manager) String() string { return mgr.id }
// NewManager returns a new Manager struct instantiated with the given ID
func NewManager(id string) *Manager {
funcs := template.FuncMap{
"isIPv4": isIPv4,
"isIPv6": isIPv6,
"isIPv4": utils.IsIPv4,
"isIPv6": utils.IsIPv6,
"humanizeBytes": humanize.Bytes,
"humanizeIBytes": humanize.IBytes,
}
Expand Down
31 changes: 0 additions & 31 deletions internal/manager/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"net"
"path/filepath"
"sync"
"text/template"
Expand Down Expand Up @@ -121,33 +120,3 @@ func (mgr *Manager) getTemplateData(ctx context.Context, name string, host, mod,
Mango: allTemplateData,
}
}

// custom template functions

// isIPv4 returns true if the given string is an IPv4 address and false otherwise
func isIPv4(s string) bool {
ip := net.ParseIP(s)
if ip == nil {
return false
}

ip4 := ip.To4()
return ip4 != nil
}

// isIPv6 returns true if the given string is an IPv6 address and false otherwise
func isIPv6(s string) bool {
ip := net.ParseIP(s)
if ip == nil {
return false
}

// short circuit if it's an IPv4
ip4 := ip.To4()
if ip4 != nil {
return false
}

ip6 := ip.To16()
return ip6 != nil
}
29 changes: 29 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -82,3 +83,31 @@ func GetHostname() string {
func IsHidden(path string) bool {
return strings.HasPrefix(path, ".")
}

// IsIPv4 returns true if the given string is an IPv4 address and false otherwise
func IsIPv4(s string) bool {
ip := net.ParseIP(s)
if ip == nil {
return false
}

ip4 := ip.To4()
return ip4 != nil
}

// IsIPv6 returns true if the given string is an IPv6 address and false otherwise
func IsIPv6(s string) bool {
ip := net.ParseIP(s)
if ip == nil {
return false
}

// short circuit if it's an IPv4
ip4 := ip.To4()
if ip4 != nil {
return false
}

ip6 := ip.To16()
return ip6 != nil
}

0 comments on commit eb0e624

Please sign in to comment.