Skip to content

Commit

Permalink
Review include hooks logic
Browse files Browse the repository at this point in the history
The `include_hooks_files` attribute uses
SshCInclude structure that permits to
define if append or prepend the list of hooks.

The default behavior is `append`.

Example:

```yaml
include_hooks_files:
  - mode: prepend
    files:
      - hooks/file1.yml
      - hooks/file2.yml
  - files:
      - hooks/file3.yml
      - hooks/file4.yml
```

This commit breaks existing ABI.
  • Loading branch information
geaaru committed Dec 8, 2024
1 parent 00b7823 commit 3432a0e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 40 deletions.
22 changes: 14 additions & 8 deletions pkg/loader/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,16 +627,22 @@ func (i *SshCInstance) ApplyCommand(c *specs.SshCCommand, proj *specs.SshCProjec

if len(c.IncludeHooksFiles) > 0 {

for _, hfile := range c.IncludeHooksFiles {
for _, hinclude := range c.IncludeHooksFiles {

// Load project included hooks
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, proj)
if err != nil {
return err
}
for _, hfile := range hinclude.GetFiles() {
// Load project included hooks
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, proj)
if err != nil {
return err
}

proj.AddHooks(hooks)
if hinclude.IncludeInAppend() {
proj.AddHooks(hooks)
} else {
proj.PrependHooks(hooks)
}
}
}
}

Expand Down
65 changes: 43 additions & 22 deletions pkg/loader/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,24 @@ func (i *SshCInstance) loadIncludeHooks(env *specs.SshCEnvironment) error {

if len(proj.IncludeHooksFiles) > 0 {

for _, hfile := range proj.IncludeHooksFiles {
for _, hinclude := range proj.IncludeHooksFiles {

// Load project included hooks
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, &proj)
if err != nil {
return err
}
for _, hfile := range hinclude.GetFiles() {

env.Projects[idx].AddHooks(hooks)
// Load project included hooks
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, &proj)
if err != nil {
return err
}

if hinclude.IncludeInAppend() {
env.Projects[idx].AddHooks(hooks)
} else {
env.Projects[idx].PrependHooks(hooks)
}

}
}

} else {
Expand All @@ -253,14 +260,21 @@ func (i *SshCInstance) loadIncludeHooks(env *specs.SshCEnvironment) error {

if len(g.IncludeHooksFiles) > 0 {

for _, hfile := range g.IncludeHooksFiles {
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, &proj)
if err != nil {
return err
}
for _, hinclude := range g.IncludeHooksFiles {
for _, hfile := range hinclude.GetFiles() {

hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, &proj)
if err != nil {
return err
}

env.Projects[idx].Groups[gidx].AddHooks(hooks)
if hinclude.IncludeInAppend() {
env.Projects[idx].Groups[gidx].AddHooks(hooks)
} else {
env.Projects[idx].Groups[gidx].PrependHooks(hooks)
}
}
}

}
Expand All @@ -269,14 +283,21 @@ func (i *SshCInstance) loadIncludeHooks(env *specs.SshCEnvironment) error {
for nidx, n := range g.Nodes {

if len(n.IncludeHooksFiles) > 0 {
for _, hfile := range n.IncludeHooksFiles {
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, &proj)
if err != nil {
return err
}

env.Projects[idx].Groups[gidx].Nodes[nidx].AddHooks(hooks)
for _, hinclude := range n.IncludeHooksFiles {
for _, hfile := range hinclude.GetFiles() {
hf := path.Join(envBaseDir, hfile)
hooks, err := i.getHooks(hfile, hf, &proj)
if err != nil {
return err
}

if hinclude.IncludeInAppend() {
env.Projects[idx].Groups[gidx].Nodes[nidx].AddHooks(hooks)
} else {
env.Projects[idx].Groups[gidx].Nodes[nidx].PrependHooks(hooks)
}
}
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions pkg/specs/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ type SshCProject struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`

IncludeGroupFiles []string `json:"include_groups_files,omitempty" yaml:"include_groups_files,omitempty"`
IncludeEnvFiles []string `json:"include_env_files,omitempty" yaml:"include_env_files,omitempty"`
IncludeHooksFiles []string `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
IncludeGroupFiles []string `json:"include_groups_files,omitempty" yaml:"include_groups_files,omitempty"`
IncludeEnvFiles []string `json:"include_env_files,omitempty" yaml:"include_env_files,omitempty"`
IncludeHooksFiles []*SshCInclude `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`

Environments []SshCEnvVars `json:"vars,omitempty" yaml:"vars,omitempty"`

Expand All @@ -72,9 +72,9 @@ type SshCProjectSanitized struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`

IncludeGroupFiles []string `json:"include_groups_files,omitempty" yaml:"include_groups_files,omitempty"`
IncludeEnvFiles []string `json:"include_env_files,omitempty" yaml:"include_env_files,omitempty"`
IncludeHooksFiles []string `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
IncludeGroupFiles []string `json:"include_groups_files,omitempty" yaml:"include_groups_files,omitempty"`
IncludeEnvFiles []string `json:"include_env_files,omitempty" yaml:"include_env_files,omitempty"`
IncludeHooksFiles []*SshCInclude `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`

Groups []SshCGroup `json:"groups" yaml:"groups"`

Expand All @@ -95,7 +95,7 @@ type SshCGroup struct {
Nodes []SshCNode `json:"nodes" yaml:"nodes"`

Hooks []SshCHook `json:"hooks" yaml:"hooks"`
IncludeHooksFiles []string `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
IncludeHooksFiles []*SshCInclude `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
ConfigTemplates []SshCConfigTemplate `json:"config_templates,omitempty" yaml:"config_templates,omitempty"`
}

Expand All @@ -118,8 +118,8 @@ type SshCNode struct {
ConfigTemplates []SshCConfigTemplate `json:"config_templates,omitempty" yaml:"config_templates,omitempty"`
SyncResources []SshCSyncResource `json:"sync_resources,omitempty" yaml:"sync_resources,omitempty"`

Hooks []SshCHook `json:"hooks" yaml:"hooks"`
IncludeHooksFiles []string `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
Hooks []SshCHook `json:"hooks" yaml:"hooks"`
IncludeHooksFiles []*SshCInclude `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
}

type SshCConfigTemplate struct {
Expand Down Expand Up @@ -150,5 +150,10 @@ type SshCCommand struct {
Envs SshCEnvVars `json:"envs,omitempty" yaml:"envs,omitempty"`
VarFiles []string `json:"vars_files,omitempty" yaml:"vars_files,omitempty"`

IncludeHooksFiles []string `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
IncludeHooksFiles []*SshCInclude `json:"include_hooks_files,omitempty" yaml:"include_hooks_files,omitempty"`
}

type SshCInclude struct {
Type string `json:"mode,omitempty" yaml:"mode,omitempty"`
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
}
6 changes: 6 additions & 0 deletions pkg/specs/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ func (g *SshCGroup) AddHooks(h *SshCHooks) {
g.Hooks = append(g.Hooks, h.Hooks...)
}
}

func (g *SshCGroup) PrependHooks(h *SshCHooks) {
if len(h.Hooks) > 0 {
g.Hooks = append(h.Hooks, g.Hooks...)
}
}
25 changes: 25 additions & 0 deletions pkg/specs/include.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright © 2024 Daniele Rondina <[email protected]>
See AUTHORS and LICENSE for the license details and contributors.
*/
package specs

const (
IncludeTypeAppend = "append"
IncludeTypePrepend = "prepend"
)

func (i *SshCInclude) GetType() string {
ans := i.Type
if ans == "" {
ans = IncludeTypeAppend
}
return ans
}
func (i *SshCInclude) GetFiles() []string { return i.Files }
func (i *SshCInclude) IncludeInAppend() bool {
if i.GetType() == IncludeTypeAppend {
return true
}
return false
}
6 changes: 6 additions & 0 deletions pkg/specs/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ func (n *SshCNode) AddHooks(h *SshCHooks) {
n.Hooks = append(n.Hooks, h.Hooks...)
}
}

func (n *SshCNode) PrependHooks(h *SshCHooks) {
if len(h.Hooks) > 0 {
n.Hooks = append(h.Hooks, n.Hooks...)
}
}
6 changes: 6 additions & 0 deletions pkg/specs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ func (p *SshCProject) AddHooks(h *SshCHooks) {
p.Hooks = append(p.Hooks, h.Hooks...)
}
}

func (p *SshCProject) PrependHooks(h *SshCHooks) {
if len(h.Hooks) > 0 {
p.Hooks = append(h.Hooks, p.Hooks...)
}
}

0 comments on commit 3432a0e

Please sign in to comment.