From eb0e62495127026adfa253d7caa73ceeca0840c4 Mon Sep 17 00:00:00 2001 From: TJ Hoplock Date: Sat, 10 Aug 2024 01:19:24 -0400 Subject: [PATCH] ref: make IsIPv4()/IsIPv6() public func in utils pkg --- internal/manager/manager.go | 5 +++-- internal/manager/template.go | 31 ------------------------------- pkg/utils/utils.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/internal/manager/manager.go b/internal/manager/manager.go index cafaefd..e8603aa 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -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 @@ -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, } diff --git a/internal/manager/template.go b/internal/manager/template.go index 1ecd018..7a6cf87 100644 --- a/internal/manager/template.go +++ b/internal/manager/template.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "net" "path/filepath" "sync" "text/template" @@ -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 -} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 8d0c977..9f4f6db 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io/fs" + "net" "os" "path/filepath" "strings" @@ -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 +}