diff --git a/actions/component_actions.go b/actions/component_actions.go index f69acc9..7ce4f72 100644 --- a/actions/component_actions.go +++ b/actions/component_actions.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "github.com/madridianfox/elc/core" - "path" ) func resolveCompNames(ws *core.Workspace, options *core.GlobalOptions, namesFromArgs []string) ([]string, error) { @@ -327,28 +326,10 @@ func RunAction(options *core.GlobalOptions) error { } func SetGitHooksAction(scriptsFolder string, elcBinary string) error { - folders, err := core.Pc.ReadDir(scriptsFolder) + err := core.GenerateHookScripts(elcBinary, scriptsFolder) if err != nil { return err } - for _, folder := range folders { - if !folder.IsDir() { - continue - } - files, err := core.Pc.ReadDir(path.Join(scriptsFolder, folder.Name())) - if err != nil { - return err - } - hookScripts := make([]string, 0) - for _, file := range files { - hookScripts = append(hookScripts, path.Join(scriptsFolder, folder.Name(), file.Name())) - } - script := core.GenerateHookScript(hookScripts, elcBinary) - err = core.Pc.WriteFile(fmt.Sprintf(".git/hooks/%s", folder.Name()), []byte(script), 0755) - if err != nil { - return err - } - } return nil } diff --git a/core/core.go b/core/core.go index e6c166d..9e5a71c 100644 --- a/core/core.go +++ b/core/core.go @@ -88,15 +88,3 @@ func substVars(expr string, ctx *Context) (string, error) { return expr, nil } - -func GenerateHookScript(scripts []string, elcBinary string) string { - result := make([]string, 0) - result = append(result, "#!/bin/bash") - result = append(result, "set -e") - result = append(result, `printf "\x1b[0;34m%s\x1b[39;49;00m\n" "Run hook in ELC"`) - for _, script := range scripts { - result = append(result, fmt.Sprintf("%s --mode=hook --no-tty %s", elcBinary, script)) - } - - return strings.Join(result, "\n") -} diff --git a/core/git.go b/core/git.go new file mode 100644 index 0000000..53eb538 --- /dev/null +++ b/core/git.go @@ -0,0 +1,87 @@ +package core + +import ( + "fmt" +) + +var hookNames = []string{ + "applypatch-msg", + "pre-applypatch", + "post-applypatch", + "pre-commit", + "pre-merge-commit", + "prepare-commit-msg", + "commit-msg", + "post-commit", + "pre-rebase", + "post-checkout", + "post-merge", + "pre-push", + "pre-receive", + "update", + "proc-receive", + "post-receive", + "post-update", + "reference-transaction", + "push-to-checkout", + "pre-auto-gc", + "post-rewrite", + "sendemail-validate", + "fsmonitor-watchman", + "p4-changelist", + "p4-prepare-changelist", + "p4-post-changelist", + "p4-pre-submit", + "post-index-change", +} + +var hookScript = `#!/bin/bash +set -e + +ELC_BINARY="%s" +HOOKS_FOLDER="%s" +HOOK_NAME="%s" + +if command -v $ELC_BINARY &> /dev/null; then + $ELC_BINARY --mode=hook --no-tty $0 +else + for script in ./$HOOKS_FOLDER/$HOOK_NAME/* ; do + if [ -f $script ]; then + $script + fi + done +fi +` + +func GenerateHookScripts(elcBinary string, hooksFolder string) error { + for _, hookName := range hookNames { + scriptContent := fmt.Sprintf(hookScript, elcBinary, hooksFolder, hookName) + scriptPath := fmt.Sprintf(".git/hooks/%s", hookName) + + if Pc.FileExists(".git/hooks") == false { + err := Pc.CreateDir(".git/hooks") + if err != nil { + return err + } + } + + if Pc.FileExists(scriptPath) == false { + err := Pc.CreateFile(scriptPath) + if err != nil { + return err + } + + err = Pc.Chmod(scriptPath, 0775) + if err != nil { + return err + } + } + + err := Pc.WriteFile(scriptPath, []byte(scriptContent), 0775) + if err != nil { + return err + } + } + + return nil +} diff --git a/core/mock_pc.go b/core/mock_pc.go index fcc3fc9..186b10d 100644 --- a/core/mock_pc.go +++ b/core/mock_pc.go @@ -48,6 +48,48 @@ func (mr *MockPCMockRecorder) Args() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Args", reflect.TypeOf((*MockPC)(nil).Args)) } +// Chmod mocks base method. +func (m *MockPC) Chmod(filename string, mode os.FileMode) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Chmod", filename, mode) + ret0, _ := ret[0].(error) + return ret0 +} + +// Chmod indicates an expected call of Chmod. +func (mr *MockPCMockRecorder) Chmod(filename, mode interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Chmod", reflect.TypeOf((*MockPC)(nil).Chmod), filename, mode) +} + +// CreateDir mocks base method. +func (m *MockPC) CreateDir(path string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateDir", path) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateDir indicates an expected call of CreateDir. +func (mr *MockPCMockRecorder) CreateDir(path interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDir", reflect.TypeOf((*MockPC)(nil).CreateDir), path) +} + +// CreateFile mocks base method. +func (m *MockPC) CreateFile(filename string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateFile", filename) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateFile indicates an expected call of CreateFile. +func (mr *MockPCMockRecorder) CreateFile(filename interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateFile", reflect.TypeOf((*MockPC)(nil).CreateFile), filename) +} + // ExecInteractive mocks base method. func (m *MockPC) ExecInteractive(command, env []string) (int, error) { m.ctrl.T.Helper() diff --git a/core/pc.go b/core/pc.go index a10523e..5084cdd 100644 --- a/core/pc.go +++ b/core/pc.go @@ -22,6 +22,9 @@ type PC interface { FileExists(filepath string) bool ReadFile(filename string) ([]byte, error) ReadDir(dirname string) ([]os.FileInfo, error) + CreateFile(filename string) error + Chmod(filename string, mode os.FileMode) error + CreateDir(path string) error WriteFile(filename string, data []byte, perm os.FileMode) error Printf(format string, a ...interface{}) (n int, err error) Println(a ...interface{}) (n int, err error) @@ -94,6 +97,24 @@ func (r *RealPC) ReadDir(dirname string) ([]os.FileInfo, error) { return ioutil.ReadDir(dirname) } +func (r *RealPC) CreateFile(filename string) error { + _, err := os.Create(filename) + + return err +} + +func (r *RealPC) Chmod(filename string, mode os.FileMode) error { + err := os.Chmod(filename, mode) + + return err +} + +func (r *RealPC) CreateDir(path string) error { + err := os.Mkdir(path, 0755) + + return err +} + func (r *RealPC) WriteFile(filename string, data []byte, perm os.FileMode) error { return ioutil.WriteFile(filename, data, perm) }