filepath love

* use filepath to get correct result of file path operation on every OS.
 * use filepath.Walk to get a better idea what is being done here
 * simpler way to get the basename of a program; also works on Windows
pull/1/head
Ingo Oeser 7 years ago
parent 4bf421d848
commit d81ea3f35c
  1. 10
      general/gochanges.go
  2. 26
      main.go

@ -2,7 +2,6 @@ package general
import (
"bytes"
"io/ioutil"
"log"
"os/exec"
"path/filepath"
@ -55,15 +54,6 @@ func GetChangedGoFiles() (result []string) {
return result
}
// GetFilesList returns a list of all files in the current directory
func GetFilesList() (result []string) {
files, _ := ioutil.ReadDir("./")
for _, f := range files {
result = append(result, f.Name())
}
return result
}
// GetGitRoot returns the path with the .git directory
func GetGitRoot() string {
gitDir, _ := RunCommand("git", "rev-parse", "--git-dir")

@ -11,20 +11,28 @@ import (
func main() {
gitroot, _ := filepath.Abs(filepath.Dir(general.GetGitRoot()))
os.Chdir(gitroot + "/.git/hooks")
currentFileSplit := strings.Split(os.Args[0], "/")
currentFile := currentFileSplit[len(currentFileSplit)-1]
files := general.GetFilesList()
hookBase := filepath.Base(os.Args[0])
hookPrefix := fmt.Sprintf("%s_", hookBase)
os.Chdir(gitroot)
for _, file := range files {
if strings.HasPrefix(file, fmt.Sprintf("%s_", currentFile)) {
result, errCode := general.RunCommand(gitroot + "/.git/hooks/" + file)
filepath.Walk(gitroot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return filepath.SkipDir
}
file := info.Name()
if strings.HasPrefix(file, hookPrefix) {
result, errCode := general.RunCommand(filepath.Join(gitroot, "/.git/hooks", file))
if errCode != 0 {
fmt.Fprintf(os.Stderr, "Error: %s", result)
os.Exit(errCode)
return
return nil
}
}
}
return nil
})
}