commit ff4b332abc7df3c17f6e8b6457ccee382dbb849f Author: Stefan Goppelt Date: Thu Mar 17 10:52:22 2022 +0100 Initial commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fdac399 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bf21e2d --- /dev/null +++ b/go.sum @@ -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= diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..e3a386a --- /dev/null +++ b/utils.go @@ -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 +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..04b8308 --- /dev/null +++ b/utils_test.go @@ -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")) +}