Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86c5b720d1
|
@@ -1,20 +1,20 @@
|
||||
package envs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
`bytes`
|
||||
`errors`
|
||||
`fmt`
|
||||
`io/ioutil`
|
||||
`os`
|
||||
`reflect`
|
||||
`strings`
|
||||
`sync`
|
||||
|
||||
"r00t2.io/goutils/multierr"
|
||||
"r00t2.io/goutils/structutils"
|
||||
"r00t2.io/sysutils/errs"
|
||||
"r00t2.io/sysutils/internal"
|
||||
"r00t2.io/sysutils/paths"
|
||||
`r00t2.io/goutils/multierr`
|
||||
`r00t2.io/goutils/structutils`
|
||||
`r00t2.io/sysutils/errs`
|
||||
`r00t2.io/sysutils/internal`
|
||||
`r00t2.io/sysutils/paths`
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -45,54 +45,6 @@ func DefEnvBlank(key, fallback string) (value string) {
|
||||
return
|
||||
}
|
||||
|
||||
// GetEnvErr returns the value of key if it exists. If it does not exist, err will be an EnvErrNoVal.
|
||||
func GetEnvErr(key string) (value string, err error) {
|
||||
|
||||
var exists bool
|
||||
|
||||
if value, exists = os.LookupEnv(key); !exists {
|
||||
err = &EnvErrNoVal{
|
||||
VarName: key,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
GetEnvErrNoBlank behaves exactly like GetEnvErr with the
|
||||
additional stipulation that the value must not be empty.
|
||||
|
||||
An error for a value that is non-empty but whitespace only (e.g. VARNM="\t")
|
||||
can be returned if ignoreWhitespace == true.
|
||||
|
||||
(If it is, an EnvErrNoVal will also be returned.)
|
||||
*/
|
||||
func GetEnvErrNoBlank(key string, ignoreWhitespace bool) (value string, err error) {
|
||||
|
||||
var exists bool
|
||||
var e *EnvErrNoVal = &EnvErrNoVal{
|
||||
VarName: key,
|
||||
WasRequiredNonEmpty: true,
|
||||
IgnoreWhiteSpace: ignoreWhitespace,
|
||||
}
|
||||
|
||||
if value, exists = os.LookupEnv(key); !exists {
|
||||
err = e
|
||||
return
|
||||
} else {
|
||||
e.WasFound = true
|
||||
e.WasWhitespace = (strings.TrimSpace(value) == "") && (value != "")
|
||||
if ignoreWhitespace && e.WasWhitespace {
|
||||
err = e
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetEnvMap returns a map of all environment variables. All values are strings.
|
||||
func GetEnvMap() (envVars map[string]string) {
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package envs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Error conforms to a stdlib error interface.
|
||||
func (e *EnvErrNoVal) Error() (errStr string) {
|
||||
|
||||
var sb *strings.Builder = new(strings.Builder)
|
||||
|
||||
sb.WriteString("the variable '")
|
||||
sb.WriteString(e.VarName)
|
||||
sb.WriteString("' was ")
|
||||
if sb.WasFound {
|
||||
sb.WriteString("found")
|
||||
} else {
|
||||
sb.WriteString("not found")
|
||||
}
|
||||
if e.WasRequiredNonEmpty && e.WasFound {
|
||||
sb.WriteString(" but is empty and was required to be non-empty")
|
||||
}
|
||||
|
||||
errStr = sb.String()
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package envs
|
||||
|
||||
type (
|
||||
/*
|
||||
EnvErrNoVal is an error containing the variable that does not exist
|
||||
(and information surrounding the errored state).
|
||||
*/
|
||||
EnvErrNoVal struct {
|
||||
// VarName is the variable name/key name originally specified in the function call.
|
||||
VarName string `json:"var" toml:"VariableName" yaml:"Variable Name/Key" xml:"key,attr"`
|
||||
// WasFound is only used for GetEnvErrNoBlank(). It is true if the variable was found/populated.
|
||||
WasFound bool `json:"found" toml:"Found" yaml:"Found" xml:"found,attr"`
|
||||
// WasRequiredNonEmpty indicates that this error was returned in a context where a variable was required to be non-empty (e.g. via GetEnvErrNoBlank()) but was empty.
|
||||
WasRequiredNonEmpty bool `json:"reqd_non_empty" toml:"RequiredNonEmpty" yaml:"Required Non-Empty" xml:"reqNonEmpty,attr"`
|
||||
// IgnoreWhitespace is true if the value was found but its evaluation was done against a whitestripped version.
|
||||
IgnoreWhitespace bool `json:"ignore_ws" toml:"IgnoreWhitespace" yaml:"Ignore Whitespace" xml:"ignoreWhitespace,attr"`
|
||||
// WasWhitespace is true if the value was whitespace-only.
|
||||
WasWhitespace bool `json:"was_ws" toml:"WasWhitespace" yaml:"Was Whitespace Only" xml:"wasWhitespace,attr"`
|
||||
}
|
||||
)
|
||||
8
go.mod
8
go.mod
@@ -6,11 +6,11 @@ require (
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/djherbis/times v1.6.0
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
||||
github.com/shirou/gopsutil/v4 v4.25.6
|
||||
golang.org/x/sync v0.16.0
|
||||
golang.org/x/sys v0.34.0
|
||||
github.com/shirou/gopsutil/v4 v4.25.5
|
||||
golang.org/x/sync v0.15.0
|
||||
golang.org/x/sys v0.33.0
|
||||
honnef.co/go/augeas v0.0.0-20161110001225-ca62e35ed6b8
|
||||
r00t2.io/goutils v1.9.0
|
||||
r00t2.io/goutils v1.8.1
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
8
go.sum
8
go.sum
@@ -21,8 +21,6 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/shirou/gopsutil/v4 v4.25.5 h1:rtd9piuSMGeU8g1RMXjZs9y9luK5BwtnG7dZaQUJAsc=
|
||||
github.com/shirou/gopsutil/v4 v4.25.5/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
|
||||
github.com/shirou/gopsutil/v4 v4.25.6 h1:kLysI2JsKorfaFPcYmcJqbzROzsBWEOAtw6A7dIfqXs=
|
||||
github.com/shirou/gopsutil/v4 v4.25.6/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
|
||||
@@ -33,8 +31,6 @@ github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
|
||||
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
||||
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -42,14 +38,10 @@ golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
|
||||
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/augeas v0.0.0-20161110001225-ca62e35ed6b8 h1:FW42yWB1sGClqswyHIB68wo0+oPrav1IuQ+Tdy8Qp8E=
|
||||
honnef.co/go/augeas v0.0.0-20161110001225-ca62e35ed6b8/go.mod h1:44w9OfBSQ9l3o59rc2w3AnABtE44bmtNnRMNC7z+oKE=
|
||||
r00t2.io/goutils v1.8.1 h1:TQcUycPKsYn0QI4uCqb56utmvu/vVSxlblBg98iXStg=
|
||||
r00t2.io/goutils v1.8.1/go.mod h1:9ObJI9S71wDLTOahwoOPs19DhZVYrOh4LEHmQ8SW4Lk=
|
||||
r00t2.io/goutils v1.9.0 h1:iEwa9LinCzabpTD03/2oUrFE3QinxszTzL48pBV9cD4=
|
||||
r00t2.io/goutils v1.9.0/go.mod h1:9ObJI9S71wDLTOahwoOPs19DhZVYrOh4LEHmQ8SW4Lk=
|
||||
r00t2.io/sysutils v1.1.1/go.mod h1:Wlfi1rrJpoKBOjWiYM9rw2FaiZqraD6VpXyiHgoDo/o=
|
||||
|
||||
@@ -4,10 +4,6 @@ import (
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
const (
|
||||
GenericSeparator rune = '/'
|
||||
)
|
||||
|
||||
// Mostly just for reference.
|
||||
const (
|
||||
// ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
|
||||
|
||||
209
paths/funcs.go
209
paths/funcs.go
@@ -23,7 +23,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"math"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
@@ -43,7 +42,7 @@ import (
|
||||
ExpandHome will take a tilde(~)-prefixed path and resolve it to the actual path in-place.
|
||||
"Nested" user paths (~someuser/somechroot/~someotheruser) are not supported as home directories are expected to be absolute paths.
|
||||
*/
|
||||
func ExpandHome(p *string) (err error) {
|
||||
func ExpandHome(path *string) (err error) {
|
||||
|
||||
var unameSplit []string
|
||||
var uname string
|
||||
@@ -52,10 +51,10 @@ func ExpandHome(p *string) (err error) {
|
||||
|
||||
// Props to this guy.
|
||||
// https://stackoverflow.com/a/43578461/733214
|
||||
if len(*p) == 0 {
|
||||
if len(*path) == 0 {
|
||||
err = errors.New("empty path")
|
||||
return
|
||||
} else if (*p)[0] != '~' {
|
||||
} else if (*path)[0] != '~' {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -71,7 +70,7 @@ func ExpandHome(p *string) (err error) {
|
||||
}
|
||||
*/
|
||||
// K but do it smarter.
|
||||
unameSplit = strings.SplitN(*p, string(os.PathSeparator), 2)
|
||||
unameSplit = strings.SplitN(*path, string(os.PathSeparator), 2)
|
||||
if len(unameSplit) != 2 {
|
||||
unameSplit = append(unameSplit, "")
|
||||
}
|
||||
@@ -87,7 +86,7 @@ func ExpandHome(p *string) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
*p = filepath.Join(u.HomeDir, unameSplit[1])
|
||||
*path = filepath.Join(u.HomeDir, unameSplit[1])
|
||||
|
||||
return
|
||||
}
|
||||
@@ -109,9 +108,9 @@ potentially returning an inaccurate result.
|
||||
|
||||
This is a thin wrapper around GetFirstWithRef.
|
||||
*/
|
||||
func GetFirst(p []string) (content []byte, isDir, ok bool) {
|
||||
func GetFirst(paths []string) (content []byte, isDir, ok bool) {
|
||||
|
||||
content, isDir, ok, _ = GetFirstWithRef(p)
|
||||
content, isDir, ok, _ = GetFirstWithRef(paths)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -120,12 +119,12 @@ func GetFirst(p []string) (content []byte, isDir, ok bool) {
|
||||
GetFirstWithRef is the file equivalent of envs.GetFirstWithRef.
|
||||
|
||||
It behaves exactly like GetFirst, but with an additional returned value, idx,
|
||||
which specifies the index in p in which a path was found.
|
||||
which specifies the index in paths in which a path was found.
|
||||
|
||||
As always, results are not guaranteed due to permissions, etc.
|
||||
potentially returning an inaccurate result.
|
||||
*/
|
||||
func GetFirstWithRef(p []string) (content []byte, isDir, ok bool, idx int) {
|
||||
func GetFirstWithRef(paths []string) (content []byte, isDir, ok bool, idx int) {
|
||||
|
||||
var locPaths []string
|
||||
var exists bool
|
||||
@@ -134,11 +133,11 @@ func GetFirstWithRef(p []string) (content []byte, isDir, ok bool, idx int) {
|
||||
|
||||
idx = -1
|
||||
// We have to be a little less cavalier about this.
|
||||
if p == nil {
|
||||
if paths == nil {
|
||||
return
|
||||
}
|
||||
locPaths = make([]string, len(p))
|
||||
locPaths = p[:] // Create an explicit copy so we don't modify p.
|
||||
locPaths = make([]string, len(paths))
|
||||
locPaths = paths[:] // Create an explicit copy so we don't modify paths.
|
||||
for i, p := range locPaths {
|
||||
if exists, stat, err = RealPathExistsStat(&p); err != nil {
|
||||
err = nil
|
||||
@@ -161,30 +160,6 @@ func GetFirstWithRef(p []string) (content []byte, isDir, ok bool, idx int) {
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Len returns the number of path segments in p, as split with the same param signature to Segment.
|
||||
|
||||
See Segment for details on abs and strict.
|
||||
*/
|
||||
func Len(p string, abs, strict bool) (segments int) {
|
||||
|
||||
segments = len(Segment(p, abs, strict))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
LenSys returns the number of path segments in p, as split with the same param signature to SegmentSys.
|
||||
|
||||
See Segment for details on abs and strict.
|
||||
*/
|
||||
func LenSys(p string, abs, strict bool) (segments int) {
|
||||
|
||||
segments = len(SegmentSys(p, abs, strict))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
MakeDirIfNotExist will create a directory at a given path if it doesn't exist.
|
||||
|
||||
@@ -192,11 +167,11 @@ See also the documentation for RealPath.
|
||||
|
||||
This is a bit more sane option than os.MkdirAll as it will normalize paths a little better.
|
||||
*/
|
||||
func MakeDirIfNotExist(p string) (err error) {
|
||||
func MakeDirIfNotExist(path string) (err error) {
|
||||
|
||||
var stat os.FileInfo
|
||||
var exists bool
|
||||
var locPath string = p
|
||||
var locPath string = path
|
||||
|
||||
if exists, stat, err = RealPathExistsStat(&locPath); err != nil {
|
||||
if !exists {
|
||||
@@ -233,16 +208,14 @@ It is recommended to check err (if not nil) for an invalid path error. If this i
|
||||
path syntax/string itself is not supported on the runtime OS. This can be done via:
|
||||
|
||||
if errors.Is(err, fs.ErrInvalid) {...}
|
||||
|
||||
RealPath is simply a wrapper around ExpandHome(path) and filepath.Abs(*path).
|
||||
*/
|
||||
func RealPath(p *string) (err error) {
|
||||
func RealPath(path *string) (err error) {
|
||||
|
||||
if err = ExpandHome(p); err != nil {
|
||||
if err = ExpandHome(path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if *p, err = filepath.Abs(*p); err != nil {
|
||||
if *path, err = filepath.Abs(*path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -252,25 +225,25 @@ func RealPath(p *string) (err error) {
|
||||
/*
|
||||
RealPathJoin combines RealPath with (path).Join.
|
||||
|
||||
If dst is nil, then p will be updated with the new value.
|
||||
If dst is nil, then rootPath will be updated with the new value.
|
||||
You probably don't want that.
|
||||
*/
|
||||
func RealPathJoin(p, dst *string, subPaths ...string) (err error) {
|
||||
func RealPathJoin(rootPath, dst *string, subPaths ...string) (err error) {
|
||||
|
||||
var newPath string
|
||||
var realDst *string
|
||||
|
||||
if err = RealPath(p); err != nil {
|
||||
if err = RealPath(rootPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if dst == nil {
|
||||
realDst = p
|
||||
realDst = rootPath
|
||||
} else {
|
||||
realDst = dst
|
||||
}
|
||||
|
||||
newPath = path.Join(append([]string{*p}, subPaths...)...)
|
||||
newPath = path.Join(append([]string{*rootPath}, subPaths...)...)
|
||||
if err = RealPath(&newPath); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -286,22 +259,22 @@ RealPathJoinSys combines RealPath with (path/filepath).Join.
|
||||
If dst is nil, then path will be updated with the new value.
|
||||
You probably don't want that.
|
||||
*/
|
||||
func RealPathJoinSys(p, dst *string, subPaths ...string) (err error) {
|
||||
func RealPathJoinSys(path, dst *string, subPaths ...string) (err error) {
|
||||
|
||||
var newPath string
|
||||
var realDst *string
|
||||
|
||||
if err = RealPath(p); err != nil {
|
||||
if err = RealPath(path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if dst == nil {
|
||||
realDst = p
|
||||
realDst = path
|
||||
} else {
|
||||
realDst = dst
|
||||
}
|
||||
|
||||
newPath = filepath.Join(append([]string{*p}, subPaths...)...)
|
||||
newPath = filepath.Join(append([]string{*path}, subPaths...)...)
|
||||
if err = RealPath(&newPath); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -327,13 +300,13 @@ See also the documentation for RealPath.
|
||||
|
||||
In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil.
|
||||
*/
|
||||
func RealPathExists(p *string) (exists bool, err error) {
|
||||
func RealPathExists(path *string) (exists bool, err error) {
|
||||
|
||||
if err = RealPath(p); err != nil {
|
||||
if err = RealPath(path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = os.Stat(*p); err != nil {
|
||||
if _, err = os.Stat(*path); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
@@ -352,13 +325,13 @@ for the path (assuming it exists).
|
||||
If stat is nil, it is highly recommended to check err via the methods suggested
|
||||
in the documentation for RealPath and RealPathExists.
|
||||
*/
|
||||
func RealPathExistsStat(p *string) (exists bool, stat os.FileInfo, err error) {
|
||||
func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) {
|
||||
|
||||
if exists, err = RealPathExists(p); err != nil {
|
||||
if exists, err = RealPathExists(path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if stat, err = os.Stat(*p); err != nil {
|
||||
if stat, err = os.Stat(*path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -525,124 +498,6 @@ func SearchFsPathsAsync(matcher FsSearchCriteriaAsync) {
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Segment returns path p's segments as a slice of strings, using GenericSeparator as a separator.
|
||||
|
||||
If abs is true, the placeholder leading prefix(es) (if any) of GenericSeparator will be kept in-place;
|
||||
otherwise it/they will be trimmed out.
|
||||
e.g.:
|
||||
|
||||
abs == true: //foo/bar/baz => []string{"", "", "foo", "bar", "baz"}
|
||||
abs == false: /foo/bar/baz => []string{"foo", "bar", "baz"}
|
||||
|
||||
If strict is true, any trailing GenericSeparator will be kept in-place;
|
||||
otherwise they will be trimmed out.
|
||||
e.g. (assuming abs == false):
|
||||
|
||||
strict == true: /foo/bar/baz// => []string{"foo", "bar", "baz", "", ""}
|
||||
strict == false: /foo/bar/baz/ => []string{"foo", "bar", "baz"}
|
||||
|
||||
It is recommended to call RealPath for path's ptr first for normalization.
|
||||
*/
|
||||
func Segment(p string, abs, strict bool) (segments []string) {
|
||||
|
||||
if !abs {
|
||||
p = strings.TrimLeft(p, string(GenericSeparator))
|
||||
}
|
||||
if !strict {
|
||||
p = strings.TrimRight(p, string(GenericSeparator))
|
||||
}
|
||||
|
||||
segments = strings.Split(p, string(GenericSeparator))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SegmentSys is exactly like Segment, except using os.PathSeparator instead of GenericSeparator.
|
||||
func SegmentSys(p string, abs, strict bool) (segments []string) {
|
||||
|
||||
if !abs {
|
||||
p = strings.TrimLeft(p, string(os.PathSeparator))
|
||||
}
|
||||
if !strict {
|
||||
p = strings.TrimRight(p, string(os.PathSeparator))
|
||||
}
|
||||
|
||||
segments = strings.Split(p, string(os.PathSeparator))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Strip is like Segment but trims out the leading n number of segments and reassembles the path using path.Join.
|
||||
|
||||
n may be negative, in which case the *trailing* n number of segments will be trimmed out.
|
||||
(i.e. n == -1, p == `foo/bar/baz/quux` would be `foo/bar/baz`, not `bar/baz/quux`)
|
||||
|
||||
If you require more traditional slicing (e.g. with interval),
|
||||
you may want to use path.Join with a sliced result of Segment instead.
|
||||
e.g.: *only* the *last* n segments: path.Join(Segment(p, ...)[Len(p, ...)-n:]...)
|
||||
|
||||
If n == 0 or int(math.Abs(float64(n))) >= len(Segment(p, ...)), no transformation will be done.
|
||||
|
||||
e.g.
|
||||
|
||||
n == 2: foo/bar/baz/foobar/quux => baz/foobar/quux
|
||||
n == -2: foo/bar/baz/foobar/quux => foo/bar/baz
|
||||
*/
|
||||
func Strip(p string, abs, strict bool, n int) (slicedPath string) {
|
||||
|
||||
var pLen int
|
||||
var absN int
|
||||
var segments []string
|
||||
|
||||
segments = Segment(p, abs, strict)
|
||||
pLen = len(segments)
|
||||
|
||||
absN = int(math.Abs(float64(n)))
|
||||
|
||||
if n == 0 || absN >= pLen {
|
||||
slicedPath = p
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
segments = segments[n:]
|
||||
} else {
|
||||
segments = segments[:pLen-absN]
|
||||
}
|
||||
|
||||
slicedPath = path.Join(segments...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// StripSys is exactly like Strip but using (path/filepath).Join and SegmentSys.
|
||||
func StripSys(p string, abs, strict bool, n int) (slicedPath string) {
|
||||
|
||||
var pLen int
|
||||
var absN int
|
||||
var segments []string
|
||||
|
||||
segments = SegmentSys(p, abs, strict)
|
||||
pLen = len(segments)
|
||||
|
||||
absN = int(math.Abs(float64(n)))
|
||||
|
||||
if n == 0 || absN >= pLen {
|
||||
slicedPath = p
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
segments = segments[n:]
|
||||
} else {
|
||||
segments = segments[:pLen-absN]
|
||||
}
|
||||
|
||||
slicedPath = filepath.Join(segments...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
filterTimes checks a times.Timespec of a file using:
|
||||
- an age specified by the caller
|
||||
|
||||
Reference in New Issue
Block a user