Skip to content

Commit

Permalink
add after-clone-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
MadridianFox committed Aug 28, 2022
1 parent 9547681 commit 10b3762
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
4 changes: 2 additions & 2 deletions actions/component_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func SetGitHooksAction(scriptsFolder string, elcBinary string) error {
return nil
}

func CloneComponentAction(options *core.GlobalOptions, svcNames []string) error {
func CloneComponentAction(options *core.GlobalOptions, svcNames []string, noHook bool) error {
ws, err := core.GetWorkspaceConfig()
if err != nil {
return err
Expand All @@ -308,7 +308,7 @@ func CloneComponentAction(options *core.GlobalOptions, svcNames []string) error
return err
}

err = comp.Clone(options)
err = comp.Clone(options, noHook)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/elc.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func NewFixUpdateCommand(parentCommand *cobra.Command) {
}

func NewServiceCloneCommand(parentCommand *cobra.Command) {
var noHook bool
var command = &cobra.Command{
Use: "clone [NAME]",
Short: "Clone component to its path",
Expand All @@ -325,8 +326,10 @@ func NewServiceCloneCommand(parentCommand *cobra.Command) {
SilenceErrors: false,
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return actions.CloneComponentAction(&globalOptions, args)
return actions.CloneComponentAction(&globalOptions, args, noHook)
},
}

command.Flags().BoolVar(&noHook, "no-hook", false, "do not execute hook script after cloning")
parentCommand.AddCommand(command)
}
30 changes: 28 additions & 2 deletions core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,15 @@ func (comp *Component) DumpVars() error {
return nil
}

func (comp *Component) Clone(options *GlobalOptions) error {
func (comp *Component) getAfterCloneHook() string {
if comp.Config.AfterCloneHook != "" {
return comp.Config.AfterCloneHook
}

return comp.Template.AfterCloneHook
}

func (comp *Component) Clone(options *GlobalOptions, noHook bool) error {
if comp.Config.Repository == "" {
return errors.New(fmt.Sprintf("repository of component %s is not defined. Check workspace.yaml", comp.Name))
}
Expand All @@ -330,6 +338,24 @@ func (comp *Component) Clone(options *GlobalOptions) error {
return nil
} else {
_, err := comp.execInteractive([]string{"git", "clone", comp.Config.Repository, svcPath}, options)
return err
if err != nil {
return err
}

if !noHook {
afterCloneHook := comp.getAfterCloneHook()
afterCloneHook, err = comp.Context.RenderString(afterCloneHook)
if err != nil {
return err
}

if afterCloneHook != "" {
_, err = comp.execInteractive([]string{afterCloneHook}, options)
if err != nil {
return err
}
}
}
return nil
}
}
30 changes: 17 additions & 13 deletions core/component_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ func (s ModeList) contains(v string) bool {
}

type ComponentConfig struct {
Alias string `yaml:"alias"`
ComposeFile string `yaml:"compose_file"`
Dependencies map[string]ModeList `yaml:"dependencies"`
ExecPath string `yaml:"exec_path"`
Extends string `yaml:"extends"`
HostedIn string `yaml:"hosted_in"`
Hostname string `yaml:"hostname"`
IsTemplate bool `yaml:"is_template"`
Path string `yaml:"path"`
Replace bool `yaml:"replace"`
Variables yaml.MapSlice `yaml:"variables"`
Repository string `yaml:"repository"`
Tags []string `yaml:"tags"`
Alias string `yaml:"alias"`
ComposeFile string `yaml:"compose_file"`
Dependencies map[string]ModeList `yaml:"dependencies"`
ExecPath string `yaml:"exec_path"`
Extends string `yaml:"extends"`
HostedIn string `yaml:"hosted_in"`
Hostname string `yaml:"hostname"`
IsTemplate bool `yaml:"is_template"`
Path string `yaml:"path"`
Replace bool `yaml:"replace"`
Variables yaml.MapSlice `yaml:"variables"`
Repository string `yaml:"repository"`
Tags []string `yaml:"tags"`
AfterCloneHook string `yaml:"after_clone_hook"`
}

func (cc ComponentConfig) merge(cc2 ComponentConfig) ComponentConfig {
Expand Down Expand Up @@ -55,6 +56,9 @@ func (cc ComponentConfig) merge(cc2 ComponentConfig) ComponentConfig {
if cc2.Repository != "" {
cc.Repository = cc2.Repository
}
if cc2.AfterCloneHook != "" {
cc.AfterCloneHook = cc2.AfterCloneHook
}

cc.Variables = append(cc.Variables, cc2.Variables...)
cc.Tags = append(cc.Tags, cc2.Tags...)
Expand Down

0 comments on commit 10b3762

Please sign in to comment.