refactor: use regexp to get root path
This commit is contained in:
20
main.go
20
main.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user