v1.16.0
ADDED: * tplx/sprigx
This commit is contained in:
155
tplx/sprigx/funcs_tpl_paths.go
Normal file
155
tplx/sprigx/funcs_tpl_paths.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package sprigx
|
||||
|
||||
import (
|
||||
`os`
|
||||
`path`
|
||||
`path/filepath`
|
||||
)
|
||||
|
||||
/*
|
||||
//
|
||||
// GENERIC
|
||||
//
|
||||
*/
|
||||
|
||||
/*
|
||||
pathPipeJoin wraps path.Join with the root element at the *end* instead of the beginning.
|
||||
|
||||
{{ pathPipeJoin "b" "c" "a" }}
|
||||
|
||||
is equivalent to
|
||||
|
||||
path.Join("a", "b", "c")
|
||||
|
||||
This order variation is better suited for pipelines that pass the root path.
|
||||
*/
|
||||
func pathPipeJoin(elems ...string) (out string) {
|
||||
|
||||
var rootIdx int
|
||||
|
||||
if elems == nil || len(elems) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
rootIdx = len(elems) - 1
|
||||
out = elems[rootIdx]
|
||||
|
||||
if len(elems) == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
out = pathSubJoin(out, elems[:rootIdx]...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// pathSliceJoin joins a slice of path segments.
|
||||
func pathSliceJoin(sl []string) (out string) {
|
||||
|
||||
out = path.Join(sl...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
pathSlicePipeJoin behaves like a mix of pathPipeJoin (in that it accepts the root element last)
|
||||
and pathSliceJoin (in that it accepts a slice of subpath segments).
|
||||
|
||||
It's essentially like pathSubJoin in reverse, and with an explicit slice.
|
||||
*/
|
||||
func pathSlicePipeJoin(sl []string, root string) (out string) {
|
||||
|
||||
out = pathSubJoin(root, sl...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
pathSubJoin is like path.Join except it takes an explicit root
|
||||
and additional slice of subpaths to sequentially join to it.
|
||||
*/
|
||||
func pathSubJoin(root string, elems ...string) (out string) {
|
||||
|
||||
if elems == nil || len(elems) == 0 {
|
||||
out = root
|
||||
return
|
||||
}
|
||||
|
||||
out = path.Join(
|
||||
root,
|
||||
path.Join(
|
||||
elems...,
|
||||
),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
//
|
||||
// OS/PLATFORM
|
||||
//
|
||||
*/
|
||||
|
||||
/*
|
||||
osPathPipeJoin is like pathPipeJoin but uses the rendering OS' path separator (os.PathSeparator).
|
||||
*/
|
||||
func osPathPipeJoin(elems ...string) (out string) {
|
||||
|
||||
var rootIdx int
|
||||
|
||||
if elems == nil || len(elems) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
rootIdx = len(elems) - 1
|
||||
out = elems[rootIdx]
|
||||
|
||||
if len(elems) == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
out = osPathSubJoin(out, elems[:rootIdx]...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// osPathSep returns os.PathSeparator.
|
||||
func osPathSep() (out string) {
|
||||
|
||||
out = string(os.PathSeparator)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// osPathSliceJoin is the OS-specific implementation of pathSliceJoin.
|
||||
func osPathSliceJoin(sl []string) (out string) {
|
||||
out = filepath.Join(sl...)
|
||||
return
|
||||
}
|
||||
|
||||
// osPathSlicePipeJoin is the OS-specific implementation of pathSlicePipeJoin.
|
||||
func osPathSlicePipeJoin(sl []string, root string) (out string) {
|
||||
|
||||
out = osPathSubJoin(root, sl...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// osPathSubJoin is the OS-specific implementation of pathSubJoin.
|
||||
func osPathSubJoin(root string, elems ...string) (out string) {
|
||||
|
||||
if elems == nil || len(elems) == 0 {
|
||||
out = root
|
||||
return
|
||||
}
|
||||
|
||||
out = filepath.Join(
|
||||
root,
|
||||
filepath.Join(
|
||||
elems...,
|
||||
),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user