refactor: use regexp to get root path

This commit is contained in:
2023-03-11 12:26:02 +01:00
parent f50cf1a3ec
commit 51e57f8246
4 changed files with 56 additions and 9 deletions

20
main.go
View File

@@ -1,18 +1,20 @@
package main
import (
"log"
"os/exec"
"strings"
"os"
"regexp"
)
func RootDir() string {
cmdOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
log.Fatalf("exec.Command: %s", err)
func RootDir(projectDirName string) string {
if projectDirName == "" {
return ""
}
rootDir := strings.TrimSpace(string(cmdOut))
return rootDir
projectName := regexp.MustCompile(`^(.*` + projectDirName + `)`)
currentWorkDirectory, _ := os.Getwd()
rootPath := projectName.Find([]byte(currentWorkDirectory))
if len(rootPath) <= 0 {
return ""
}
return string(rootPath)
}