diff --git a/actions/component_actions.go b/actions/component_actions.go index 7ce4f72..f3f4d7e 100644 --- a/actions/component_actions.go +++ b/actions/component_actions.go @@ -325,12 +325,29 @@ func RunAction(options *core.GlobalOptions) error { return nil } -func SetGitHooksAction(scriptsFolder string, elcBinary string) error { - err := core.GenerateHookScripts(elcBinary, scriptsFolder) +func SetGitHooksAction(options *core.GlobalOptions, scriptsFolder string, elcBinary string) error { + ws, err := core.GetWorkspaceConfig(options.WorkspaceName) if err != nil { return err } + compNames, err := resolveCompNames(ws, options, []string{}) + if err != nil { + return err + } + + for _, compName := range compNames { + comp, err := ws.ComponentByName(compName) + if err != nil { + return err + } + + err = comp.UpdateHooks(options, elcBinary, scriptsFolder) + if err != nil { + return err + } + } + return nil } diff --git a/cmd/elc.go b/cmd/elc.go index ec985ce..586becd 100644 --- a/cmd/elc.go +++ b/cmd/elc.go @@ -316,10 +316,10 @@ func NewServiceSetHooksCommand(parentCommand *cobra.Command) { var command = &cobra.Command{ Use: "set-hooks [HOOKS_DIR]", Short: "Install hooks from specified folder to .git/hooks", - Long: "Install hooks from specified folder to .git/hooks.\nHOOKS_PATH must contain subdirectories with names as git hooks, eg. 'pre-commit'.\nOne subdirectory can contain one or many scripts with .sh extension.\nEvery script wil be wrapped with 'elc --tag=hook' command.", + Long: "Install hooks from specified folder to .git/hooks.\nHOOKS_PATH must contain subdirectories with names as git hooks, eg. 'pre-commit'.\nOne subdirectory can contain one or many scripts with .sh extension.\nEvery script will be wrapped with 'elc --tag=hook' command.", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return actions.SetGitHooksAction(args[0], os.Args[0]) + return actions.SetGitHooksAction(&globalOptions, args[0], os.Args[0]) }, } parentCommand.AddCommand(command) diff --git a/core/component.go b/core/component.go index c609b11..6f9fddd 100644 --- a/core/component.go +++ b/core/component.go @@ -402,3 +402,12 @@ func (comp *Component) Clone(options *GlobalOptions, noHook bool) error { return nil } } + +func (comp *Component) UpdateHooks(options *GlobalOptions, elcBinary string, scriptsFolder string) error { + svcPath, found := comp.Context.find("SVC_PATH") + if !found { + return errors.New("path of component is not defined.Check workspace.yaml") + } + + return GenerateHookScripts(options, svcPath, elcBinary, scriptsFolder) +} diff --git a/core/git.go b/core/git.go index 53eb538..e04a9f4 100644 --- a/core/git.go +++ b/core/git.go @@ -2,6 +2,8 @@ package core import ( "fmt" + "os" + "strings" ) var hookNames = []string{ @@ -53,35 +55,74 @@ else 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) +func GenerateHookScripts(options *GlobalOptions, svcPath string, elcBinary string, scriptsFolder string) error { + gitPath := fmt.Sprintf("%s/.git", svcPath) + if Pc.FileExists(gitPath) == false { + _, _ = Pc.Println(fmt.Sprintf("\033[0;33mRepository %s is not exists, skip hooks installation.\033[0m", gitPath)) + return nil + } + + hooksPath := fmt.Sprintf("%s/hooks", gitPath) + if Pc.FileExists(hooksPath) == false { + if options.Debug { + _, _ = Pc.Printf("mkdir %s\n", hooksPath) + } - if Pc.FileExists(".git/hooks") == false { - err := Pc.CreateDir(".git/hooks") + if !options.DryRun { + err := Pc.CreateDir(hooksPath) if err != nil { return err } } + } + + scriptsFolder = strings.ReplaceAll(scriptsFolder, "./", "") + scriptsFolder = strings.Trim(scriptsFolder, "/") + + scriptsFolderPath := fmt.Sprintf("%s/%s", svcPath, scriptsFolder) + if Pc.FileExists(scriptsFolderPath) == false { + _, _ = Pc.Println(fmt.Sprintf("\033[0;33mFolder %s is not exists, skip hooks installation.\033[0m", scriptsFolderPath)) + return nil + } + + for _, hookName := range hookNames { + scriptPath := fmt.Sprintf("%s/%s", hooksPath, hookName) + scriptContent := fmt.Sprintf(hookScript, elcBinary, scriptsFolder, hookName) + + var filePermissions os.FileMode = 0775 if Pc.FileExists(scriptPath) == false { - err := Pc.CreateFile(scriptPath) - if err != nil { - return err + if options.Debug { + _, _ = Pc.Printf("touch %s\n", scriptPath) + _, _ = Pc.Printf("chmod %s %s\n", filePermissions, scriptPath) } - err = Pc.Chmod(scriptPath, 0775) - if err != nil { - return err + if !options.DryRun { + err := Pc.CreateFile(scriptPath) + if err != nil { + return err + } + + err = Pc.Chmod(scriptPath, filePermissions) + if err != nil { + return err + } } } - err := Pc.WriteFile(scriptPath, []byte(scriptContent), 0775) - if err != nil { - return err + if options.Debug { + _, _ = Pc.Printf("echo \"