Skip to content

Commit

Permalink
#105464 component-selector support in hooks generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Koryukov committed Apr 21, 2023
1 parent 9529430 commit 474d626
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
21 changes: 19 additions & 2 deletions actions/component_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/elc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
71 changes: 56 additions & 15 deletions core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"fmt"
"os"
"strings"
)

var hookNames = []string{
Expand Down Expand Up @@ -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 \"<script>\" > %s\n", scriptPath)
}

if !options.DryRun {
err := Pc.WriteFile(scriptPath, []byte(scriptContent), filePermissions)
if err != nil {
return err
}
}
}

_, _ = Pc.Println(fmt.Sprintf("\033[0;32mFiles in %s updated.\033[0m", hooksPath))

return nil
}

0 comments on commit 474d626

Please sign in to comment.