Initial commit
This commit is contained in:
commit
ff4b332abc
|
@ -0,0 +1,10 @@
|
||||||
|
module scm.yoorie.de/go-lib/util
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require gotest.tools v2.2.0+incompatible
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/go-cmp v0.5.7 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
)
|
|
@ -0,0 +1,8 @@
|
||||||
|
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||||
|
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||||
|
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
|
@ -0,0 +1,47 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileExists Checks if a file with the given file name exists
|
||||||
|
func FileExists(fileName string) bool {
|
||||||
|
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// JoiningSlash joining http path elements
|
||||||
|
func JoiningSlash(elem ...string) string {
|
||||||
|
return joiningSlash(elem)
|
||||||
|
}
|
||||||
|
|
||||||
|
func joiningSlash(elem []string) string {
|
||||||
|
path := ""
|
||||||
|
for _, e := range elem {
|
||||||
|
if e != "" {
|
||||||
|
if path == "" {
|
||||||
|
path = e
|
||||||
|
} else {
|
||||||
|
path = singleJoiningSlash(path, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
func singleJoiningSlash(a, b string) string {
|
||||||
|
filepath.Join(a, b)
|
||||||
|
aslash := strings.HasSuffix(a, "/")
|
||||||
|
bslash := strings.HasPrefix(b, "/")
|
||||||
|
switch {
|
||||||
|
case aslash && bslash:
|
||||||
|
return a + b[1:]
|
||||||
|
case !aslash && !bslash:
|
||||||
|
return a + "/" + b
|
||||||
|
}
|
||||||
|
return a + b
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gotest.tools/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFileExist(t *testing.T) {
|
||||||
|
assert.Assert(t, FileExists("utils.go"))
|
||||||
|
assert.Assert(t, !FileExists("Utils2.go"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJoiningSlash(t *testing.T) {
|
||||||
|
assert.Equal(t, "http://my.tld/bla/blub", JoiningSlash("http://my.tld", "bla", "blub"))
|
||||||
|
assert.Equal(t, "http://my.tld/bla/blub", JoiningSlash("http://my.tld/", "bla", "blub"))
|
||||||
|
assert.Equal(t, "http://my.tld/bla/blub", JoiningSlash("http://my.tld", "bla/", "blub"))
|
||||||
|
assert.Equal(t, "http://my.tld/docs/bla/blub", JoiningSlash("http://my.tld/docs", "bla/", "blub"))
|
||||||
|
assert.Equal(t, "http://my.tld/docs/bla/blub", JoiningSlash("http://my.tld/docs/", "bla/", "blub"))
|
||||||
|
}
|
Loading…
Reference in New Issue