Skip to content

Commit c723abe

Browse files
Check for broken symlinks before install
1 parent 5fed7c5 commit c723abe

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

exec/helper.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package exec
22

3-
import "strings"
3+
import (
4+
"os"
5+
"strings"
6+
)
47

58
// isILogicCondition checks if the condition is an "AND" or an "OR" condition
69
func isLogicCondition(action string) bool {
@@ -22,3 +25,12 @@ func splitInternalPath(action string) []string {
2225
actionPath := strings.Split(action, "::")[1]
2326
return strings.Split(actionPath, ".")
2427
}
28+
29+
// isSymlink checks if a file is a symlink
30+
func isSymlink(path string) (bool, error) {
31+
info, err := os.Lstat(path)
32+
if err != nil {
33+
return false, err
34+
}
35+
return info.Mode()&os.ModeSymlink != 0, nil
36+
}

exec/installer.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package exec
22

33
import (
4+
"errors"
5+
"fmt"
46
"github.com/captainhook-go/captainhook/configuration"
57
"github.com/captainhook-go/captainhook/git"
68
"github.com/captainhook-go/captainhook/hooks/util"
79
"github.com/captainhook-go/captainhook/info"
810
"github.com/captainhook-go/captainhook/io"
911
"os"
1012
"os/exec"
13+
"path/filepath"
1114
"sort"
1215
"strings"
1316
"text/template"
@@ -59,6 +62,7 @@ func (i *Installer) Run() error {
5962
for _, hook := range keys {
6063
err := i.installHook(hook, hooks[hook] && !i.force)
6164
if err != nil {
65+
i.appIO.Write("<warning>"+err.Error()+"</warning>", true, io.NORMAL)
6266
return err
6367
}
6468
}
@@ -100,6 +104,11 @@ func (i *Installer) writeHookFile(hook string) error {
100104
doIt = io.AnswerToBool(answer)
101105
}
102106

107+
err := i.checkForBrokenSymlink(hook)
108+
if err != nil {
109+
return err
110+
}
111+
103112
if doIt {
104113
vars := make(map[string]interface{})
105114
vars["HOOK_NAME"] = hook
@@ -110,7 +119,10 @@ func (i *Installer) writeHookFile(hook string) error {
110119

111120
tpl, _ := template.New("hook").Parse(i.HookTemplate())
112121

113-
file, _ := os.Create(i.repo.HooksDir() + "/" + hook)
122+
file, fileErr := os.Create(i.repo.HooksDir() + "/" + hook)
123+
if fileErr != nil {
124+
return fileErr
125+
}
114126
defer file.Close()
115127

116128
tplErr := tpl.Execute(file, vars)
@@ -236,6 +248,31 @@ func (i *Installer) HookTemplate() string {
236248
"hook {{ .HOOK_NAME }} \"$@\" <&0\n\n"
237249
}
238250

251+
func (i *Installer) checkForBrokenSymlink(hook string) error {
252+
filePath := i.repo.HooksDir() + "/" + hook
253+
isSymlink, _ := isSymlink(filePath)
254+
if !isSymlink {
255+
return nil
256+
}
257+
target, err := os.Readlink(filePath)
258+
if err != nil {
259+
return err
260+
}
261+
if !filepath.IsAbs(target) {
262+
target = filepath.Join(filepath.Dir(filePath), target)
263+
}
264+
265+
_, err = os.Stat(target)
266+
if err != nil {
267+
if os.IsNotExist(err) {
268+
return errors.New(hook + " is a broken symlink please delete or fix it and try again")
269+
}
270+
fmt.Println("File fucked: ", err.Error())
271+
return err
272+
}
273+
return nil
274+
}
275+
239276
func NewInstaller(appIO io.IO, config *configuration.Configuration, repo git.Repo) *Installer {
240277
return &Installer{
241278
appIO: appIO,

0 commit comments

Comments
 (0)