Compare commits

...
3 Commits
Author SHA1 Message Date
brent saner f002766c74 v1.17.3
ADDED:
* slicesx.First
* slicesx.FirstIndex
* slicesx.Remove
* slicesx.RemoveAll
* slicesx.RemoveReverse
2026-09-03 16:25:31 -04:00
brent saner 67bd30907e v1.17.2
ADDED:
* iox/fsx:
** FsPaths
** HasType
** IsType
2026-08-08 00:30:44 -04:00
brent saner 4e7d474fb3 v1.17.1
FIXED:
* tplx/sprigx would throw errors on dnsx.IsPtr, because it returns a
  bool and net.IP instead of a boolen and optional error. Fixed by
  adding dnsx.IsPtrChk and using that instead.
2026-08-08 00:13:34 -04:00
14 changed files with 580 additions and 294 deletions
+4
View File
@@ -0,0 +1,4 @@
/*
Package fsx provides some enhancements to [io/fs].
*/
package fsx
+118
View File
@@ -0,0 +1,118 @@
package fsx
import (
"go/types"
"io/fs"
"path/filepath"
)
// TODO: FilterFsPaths
/*
FsPaths returns all paths in [io/fs.FS] `fsys`.
If `fsys` is nil, both `fsPaths` and `err` will be nil.
`t` should be an OR'd [io/fs.FileMode] containing all filetypes that should be included.
Use [io/fs.ModeType] to match any non-regular-file object, or 0 to explicitly match regular files.
If `t` is non-zero but you wish to ALSO include regular files, then specify `inclFiles` as true.
(`inclFiles` is ignored if `t` == 0, as this inherently matches regular files only/explicitly.)
See [HasType] for more information on this filtering.
`maxDepth` can be used to limit recursion depth.
- If `maxDepth` == 0, only direct [io/fs.DirEntry] descendants of `fsys`
will be included in `fsPaths`.
- If `maxDepth` < 0, ALL [io/fs.DirEntry] descendants of `fsys`
will be included in `fsPaths`.
- If `maxDepth` > 0, then only children at a depth of this level or lower
("higher" in a hierarchy) will be included in `fsPaths`.
*/
func FsPaths(fsys fs.FS, t fs.FileMode, inclFiles bool, maxDepth int) (fsPaths map[string]fs.DirEntry, err error) {
var idx int
var entries []fs.DirEntry
var t fs.FileMode = types.Type()
if fsys == nil {
return
}
if fsPaths, err = getEntries(fsys, t, inclFiles, maxDepth, "."); err != nil {
return
}
return
}
/*
HasType is just a convenience wrapper for:
hasType = m & t != 0
The type(s) will be explicitly extracted from `m` and `t` first.
Note that if `t` is an OR'd set of filetypes, then HasType will return true if ANY of the types
in `t` match `m`'s type (see [r00t2.io/goutils/bitmask] for more information on bitmasking).
If you wish to check if `m` is A SINGLE type, either ensure that `t` is not OR'd
(i.e. is a single fs.FileMode type) or use [IsType] instead.
*/
func HasType(m, t fs.FileMode) (hasType bool) { return m.Type()&t.Type() != 0 }
/*
IsType is just a convenience wrapper for:
isType = m & t == t
The type(s) will be explicitly extracted from `m` and `t` first.
Note that if `t` is an OR'd set of filetypes, then IsType will *never* return true
as its type is single-valued/not bitmasked (see [r00t2.io/goutils/bitmask] for more information
on bitmasking).
If you wish to check if `m` is ONE OF multiple types in `t`, use [HasType] instead.
*/
func IsType(m, t fs.FileMode) (isType bool) { return m&t == t }
/*
getEntries is used to recursively fetch entries from `fsys`.
It mostly serves as the recursion for [FsPaths], and the arguments
retain their exact meaning except that it requires `relPath`
and `maxDepth` (if > 0) will be decremanted in each recursion.
*/
func getEntries(fsys fs.FS, t fs.FileMode, inclFiles bool, maxDepth int, relPath string) (fsPaths map[string]fs.DirEntry, err error) {
var idx int
var p string
var k string
var v fs.DirEntry
var e []fs.DirEntry
var subs map[string]fs.DirEntry = make(map[string]fs.DirEntry)
if e, err = fs.ReadDir(fsys, relPath); err != nil {
return
}
fsPaths = make(map[string]fs.DirEntry)
for idx = range e {
p = filepath.Join(relPath, e[idx].Name())
if e[idx].IsDir() {
if subs, err = getEntries(fsys, t, inclFiles, maxDepth, p); err != nil {
return
}
for k, v = range subs {
fsPaths[k] = v
}
}
if t != 0 {
if HasType(t, e[idx].Type()) {
fsPaths[p] = e[idx]
}
} else if inclFiles && e[idx].Type() == 0 {
fsPaths[p] = e[idx]
}
}
return
}
+1
View File
@@ -0,0 +1 @@
- does IsAdd4Compat work properly with loopback ::1?
+56 -40
View File
@@ -1,16 +1,16 @@
package dnsx
import (
`bytes`
`encoding/base32`
`fmt`
`math`
`net`
`net/netip`
`strings`
"bytes"
"encoding/base32"
"fmt"
"math"
"net"
"net/netip"
"strings"
`go4.org/netipx`
`r00t2.io/goutils/stringsx`
"go4.org/netipx"
"r00t2.io/goutils/stringsx"
)
/*
@@ -238,19 +238,19 @@ TXT records, etc. - not A/AAAA/CNAME, etc. - see RFC 8553 for details.
See the following functions for allowing additional syntax/rule validation
that have record-type-specific accommodations made:
* [IsFqdnDefinedTxt]
* [IsFqdnNsec3]
* [IsFqdnSrv]
* [IsFqdnWildcard]
- [IsFqdnDefinedTxt]
- [IsFqdnNsec3]
- [IsFqdnSrv]
- [IsFqdnWildcard]
# RFC Coverage
This function should conform properly to:
* RFC 952
* RFC 1034 and RFC 1035
* RFC 1123
* RFC 2181 (selectively, see above)
- RFC 952
- RFC 1034 and RFC 1035
- RFC 1123
- RFC 2181 (selectively, see above)
preferring the most up-to-date rules where relevant (e.g. labels may start with digits, as per RFC 1123).
It enforces/checks label and overall length limits as defined by RFC.
@@ -265,12 +265,12 @@ the caller must perform translation first
To reiterate, IDN/IDNA:
* RFC 3490
* RFC 5890
* RFC 5891
* RFC 5892
* RFC 5893
* RFC 5894
- RFC 3490
- RFC 5890
- RFC 5891
- RFC 5892
- RFC 5893
- RFC 5894
and Punycode (RFC 3492) *MUST* use their ASCII forms, NOT the localized/Unicode formats.
@@ -299,18 +299,18 @@ func IsFqdn(s string) (fqdn bool) {
IsFqdnDefinedTxt is like [IsFqdn] but explicitly *only* allows fully-qualified
RFC-defined TXT "subtypes":
* ACME DNS-01 (RFC 8555)
* BIMI (RFC draft [bimi])
* DKIM (RFC 6376, RFC 8301, RFC 8463)
* DKIM ATPS (RFC 6541)
* DMARC (RFC 7489, RFC 9091, RFC 9616)
* MTA-STS (RFC 8461)
* TLSRPT (RFC 8460)
- ACME DNS-01 (RFC 8555)
- BIMI (RFC draft [bimi])
- DKIM (RFC 6376, RFC 8301, RFC 8463)
- DKIM ATPS (RFC 6541)
- DMARC (RFC 7489, RFC 9091, RFC 9616)
- MTA-STS (RFC 8461)
- TLSRPT (RFC 8460)
Note that the following TXT "subtypes" do not have special formatting in labels/name,
and thus are not covered by this function:
* SPF (RFC 4408, RFC 7208)
- SPF (RFC 4408, RFC 7208)
[bimi]: https://datatracker.ietf.org/doc/html/draft-brand-indicators-for-message-identification
*/
@@ -359,15 +359,15 @@ can't be 100% confirmed with certainty - only basic checks can be done.
NSEC3 can be found via:
* RFC 5155
* RFC 6840
* RFC 6944
* RFC 7129
* RFC 8198
* RFC 9077
* RFC 9157
* RFC 9276
* RFC 9905
- RFC 5155
- RFC 6840
- RFC 6944
- RFC 7129
- RFC 8198
- RFC 9077
- RFC 9157
- RFC 9276
- RFC 9905
At the time of writing, only one hashing algorithm (SHA-1) has been specified.
However, because this function does not check against the IANA registration at runtime,
@@ -524,6 +524,22 @@ func IsPtr(s string) (isPtr bool, addr net.IP) {
return
}
// IsPtrAddr is like IsPtr but only returns the `addr` value.
func IsPtrAddr(s string) (addr net.IP) {
_, addr = IsPtr(s)
return
}
// IsPtrChk is like IsPtr but only returns the `isPtr` value.
func IsPtrChk(s string) (isPtr bool) {
isPtr, _ = IsPtr(s)
return
}
// commonFqdn is used to validate some rules common to all record names.
func commonFqdn(s string) (isOk bool) {
+3
View File
@@ -1,4 +1,7 @@
/*
Package slicesx aims to extend functionality of the stdlib [slices] module.
Most are convenience functions that aid in readability (sometimes at a VERY negligible performance hit),
but others can be quite useful (e.g. [Remove], [RemoveAll], [RemoveReverse]) that will save on significant boilerplate.
*/
package slicesx
+146 -1
View File
@@ -1,10 +1,51 @@
package slicesx
import (
`slices`
)
/*
First returns the first item/element in slice s.
e will be the nil/zero value of the slice element type
if the slice length is 0 (including if s == nil).
*/
func First[S ~[]E, E any](s S) (e E) {
if len(s) == 0 {
return
}
e = s[0]
return
}
/*
FirstIndex returns the index of the first element in slice s.
While at first glance this may seem useless, it may have some usecases as instead of causing panics:
* If s is empty, idx will be -1.
* If s is nil, idx will be -2.
* Otherwise idx will ALWAYS be 0.
*/_
func FirstIndex[S ~[]E, E any](s S) (idx int) {
if s == nil {
idx = -2
} else if len(s) == 0 {
idx = -1
}
return
}
/*
Last returns the last item/element in slice s.
e will be the nil/zero value of the slice element type
if the slice length is 0.
if the slice length is 0 (including if s == nil).
*/
func Last[S ~[]E, E any](s S) (e E) {
@@ -41,3 +82,107 @@ func LastIndex[S ~[]E, E any](s S) (idx int) {
return
}
/*
Remove removes any values of value elem from slice s up to max number of elems.
If maxRemove is negative (< 0), start from the end instead.
This simply wraps [RemoveReverse]:
removed = slicesx.RemoveReverse(s, elem, uint(-maxRemove))
If maxRemove is exactly 0, remove ALL instances of elem.
If s is nil, removed will be nil as well.
Note that s will remain untouched, and removed will be a new slice.
*/
func Remove[S ~[]E, E comparable](s S, elem E, maxRemove int) (removed S) {
var idx int
if s == nil {
return
}
if len(s) == 0 {
removed = []E{}
return
}
removed = make(S, 0, len(s))
defer func() {
if removed != nil {
removed = slices.Clip(removed)
}
}()
if maxRemove < 0 {
removed = RemoveReverse(s, elem, uint(-maxRemove))
return
} else if maxRemove == 0 {
maxRemove = len(s)
}
for idx = range s {
if s[idx] == elem && maxRemove > 0 {
maxRemove--
continue
}
removed = append(removed, s[idx])
}
return
}
/*
RemoveAll is simply a convenience wrapper around [Remove], e.g.:
removed = slicesx.Remove(s, elem, 0)
*/
func RemoveAll[S ~[]E, E comparable](s S, elem E) (removed S) { return Remove(s, elem, 0) }
/*
RemoveReverse removes any values of value elem from slice s up to max number of elems in reverse order
(i.e. starting from the END of s).
If maxRemove is exactly 0, remove all instances of elem (via [RemoveAll]), i.e.:
removed = slicesx.RemoveAll(s, elem)
// or, more directly:
removed = slicesx.Remove(s, elem, 0)
If s is nil, removed will be nil as well.
Note that s will remain untouched, and removed will be a new slice.
*/
func RemoveReverse[S ~[]E, E comparable](s S, elem E, maxRemove uint) (removed S) {
var idx int
if s == nil {
return
}
if len(s) == 0 {
removed = []E{}
return
}
if maxRemove == 0 {
removed = RemoveAll(s, elem)
return
}
removed = make(S, 0, len(s))
for idx = len(s) - 1; idx >= 0; idx-- {
if s[idx] == elem && maxRemove > 0 {
maxRemove--
continue
}
removed = append(removed, s[idx])
}
removed = slices.Clip(removed)
slices.Reverse(removed)
return
}
+1 -1
View File
@@ -636,7 +636,7 @@ pre.rouge .gs {
<div class="details">
<span id="author" class="author">Brent Saner</span><br>
<span id="email" class="email"><a href="mailto:bts@square-r00t.net">bts@square-r00t.net</a></span><br>
<span id="revdate">Last rendered 2026-07-29 15:05:41 -0400</span>
<span id="revdate">Last rendered 2026-09-03 16:25:31 -0400</span>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
+214 -216
View File
File diff suppressed because it is too large Load Diff
+25 -24
View File
@@ -1,30 +1,30 @@
package sprigx
import (
`net`
`net/netip`
`os`
`os/user`
`path`
`path/filepath`
`runtime`
`time`
"net"
"net/netip"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"time"
`github.com/davecgh/go-spew/spew`
`github.com/shirou/gopsutil/v4/cpu`
`github.com/shirou/gopsutil/v4/disk`
`github.com/shirou/gopsutil/v4/host`
`github.com/shirou/gopsutil/v4/load`
`github.com/shirou/gopsutil/v4/mem`
psnet `github.com/shirou/gopsutil/v4/net`
`github.com/shirou/gopsutil/v4/process`
`github.com/shirou/gopsutil/v4/sensors`
`go4.org/netipx`
`r00t2.io/goutils/netx`
`r00t2.io/goutils/netx/dnsx`
`r00t2.io/goutils/stringsx`
`r00t2.io/goutils/timex`
`r00t2.io/sysutils`
"github.com/davecgh/go-spew/spew"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/mem"
psnet "github.com/shirou/gopsutil/v4/net"
"github.com/shirou/gopsutil/v4/process"
"github.com/shirou/gopsutil/v4/sensors"
"go4.org/netipx"
"r00t2.io/goutils/netx"
"r00t2.io/goutils/netx/dnsx"
"r00t2.io/goutils/stringsx"
"r00t2.io/goutils/timex"
"r00t2.io/sysutils"
)
var (
@@ -110,7 +110,8 @@ var (
"dnsxIsSrv": dnsx.IsFqdnSrv,
"dnsxIsWild": dnsx.IsFqdnWildcard,
"dnsxIsLbl": dnsx.IsLabel,
"dnsxIsPtr": dnsx.IsPtr,
"dnsxIsPtrAddr": dnsx.IsPtrAddr,
"dnsxIsPtr": dnsx.IsPtrChk,
/*
Numbers/Math
*/