Add utils.
This commit is contained in:
30
editorconfig/utils.go
Normal file
30
editorconfig/utils.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package editorconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var filePathSeparatorRegex = regexp.QuoteMeta(string(filepath.Separator))
|
||||||
|
|
||||||
|
var endOfPathRegex, _ = regexp.Compile(`[` + filePathSeparatorRegex + `][^` + filePathSeparatorRegex + `]+$`)
|
||||||
|
|
||||||
|
func GetParentDir(path string) string {
|
||||||
|
return endOfPathRegex.ReplaceAllString(path, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ContainsString(haystack []string, needle string) bool {
|
||||||
|
for _, s := range haystack {
|
||||||
|
if s == needle {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExitBecauseOfInternalError(err string) {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
17
editorconfig/utils_test.go
Normal file
17
editorconfig/utils_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package editorconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetParentDir(t *testing.T) {
|
||||||
|
if GetParentDir("a/b/c.txt") != "a/b" {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
if GetParentDir("/") != "/" {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
if GetParentDir("/a/b") != "/a" {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user