Skip to content

Commit ac57873

Browse files
authored
Clean up shared logic between Repo struct and install command (#15)
1 parent 1aae784 commit ac57873

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

install.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ func listHooks(repo *repo.Repo) ([]string, error) {
7272
}
7373

7474
func promptForInstallShim(stdin io.Reader, repo *repo.Repo, shimPath string) (bool, error) {
75-
exists, err := repo.IsDir(shimPath)
76-
if err != nil {
75+
_, err := os.Stat(path.Join(repo.Root, shimPath))
76+
exists := true
77+
if os.IsNotExist(err) {
78+
exists = false
79+
} else if err != nil {
7780
return false, err
7881
}
7982

repo/repo.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package repo
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io/fs"
66
"os"
77
"os/exec"
88
"path"
@@ -29,23 +29,32 @@ func NewRepo() (*Repo, error) {
2929
func (repo *Repo) FindHookExecutables(hook string) ([]string, error) {
3030
dir := path.Join(".quickhook", hook)
3131

32-
files, err := ioutil.ReadDir(path.Join(repo.Root, dir))
33-
if err != nil {
34-
if os.IsNotExist(err) {
35-
return []string{}, nil
36-
} else {
32+
var infos []fs.FileInfo
33+
{
34+
f, err := os.Open(path.Join(repo.Root, dir))
35+
if err != nil {
36+
if os.IsNotExist(err) {
37+
return []string{}, nil
38+
}
39+
return nil, err
40+
}
41+
defer f.Close()
42+
43+
// Using Readdir since it returns FileInfo's that include permissions, whereas ReadDir
44+
// returns returns DirEntry's which does not.
45+
infos, err = f.Readdir(-1)
46+
if err != nil {
3747
return nil, err
3848
}
3949
}
4050

4151
hooks := []string{}
42-
for _, fileInfo := range files {
43-
if fileInfo.IsDir() {
52+
for _, info := range infos {
53+
if info.IsDir() {
4454
continue
4555
}
46-
47-
name := fileInfo.Name()
48-
if (fileInfo.Mode() & 0111) > 0 {
56+
name := info.Name()
57+
if (info.Mode() & 0111) != 0 {
4958
hooks = append(hooks, path.Join(dir, name))
5059
} else {
5160
fmt.Fprintf(os.Stderr, "Warning: Non-executable file found in %v: %v\n", dir, name)
@@ -85,14 +94,3 @@ func (repo *Repo) isFile(name string) (bool, error) {
8594
}
8695
return !stat.IsDir(), nil
8796
}
88-
89-
func (repo *Repo) IsDir(name string) (bool, error) {
90-
stat, err := os.Stat(path.Join(repo.Root, name))
91-
if err != nil {
92-
if os.IsNotExist(err) {
93-
return false, nil
94-
}
95-
return false, err
96-
}
97-
return stat.IsDir(), nil
98-
}

0 commit comments

Comments
 (0)