Skip to content

Commit

Permalink
chore: rename actions to jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Dec 19, 2024
1 parent 6857d66 commit 31458a6
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 117 deletions.
2 changes: 1 addition & 1 deletion internal/config/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Hook struct {
Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`

Actions []*Action `json:"actions,omitempty" mapstructure:"actions" toml:"actions,omitempty" yaml:",omitempty"`
Jobs []*Job `json:"jobs,omitempty" mapstructure:"jobs" toml:"jobs,omitempty" yaml:",omitempty"`

Commands map[string]*Command `json:"commands,omitempty" mapstructure:"-" toml:"commands,omitempty" yaml:",omitempty"`
Scripts map[string]*Script `json:"scripts,omitempty" mapstructure:"-" toml:"scripts,omitempty" yaml:",omitempty"`
Expand Down
18 changes: 9 additions & 9 deletions internal/config/actions.go → internal/config/job.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package config

type Action struct {
type Job struct {
Name string `json:"name,omitempty" mapstructure:"name" toml:"name,omitempty" yaml:",omitempty"`
Run string `json:"run,omitempty" mapstructure:"run" toml:"run,omitempty" yaml:",omitempty"`
Script string `json:"script,omitempty" mapstructure:"script" toml:"script,omitempty" yaml:",omitempty"`
Expand Down Expand Up @@ -31,18 +31,18 @@ type Group struct {
Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Parallel bool `json:"parallel,omitempty" mapstructure:"parallel" toml:"parallel,omitempty" yaml:",omitempty"`
Piped bool `json:"piped,omitempty" mapstructure:"piped" toml:"piped,omitempty" yaml:",omitempty"`
Actions []*Action `json:"actions,omitempty" mapstructure:"actions" toml:"actions,omitempty" yaml:",omitempty"`
Jobs []*Job `json:"jobs,omitempty" mapstructure:"jobs" toml:"jobs,omitempty" yaml:",omitempty"`
}

func (action *Action) PrintableName(id string) string {
if len(action.Name) != 0 {
return action.Name
func (job *Job) PrintableName(id string) string {
if len(job.Name) != 0 {
return job.Name
}
if len(action.Run) != 0 {
return action.Run
if len(job.Run) != 0 {
return job.Run
}
if len(action.Script) != 0 {
return action.Script
if len(job.Script) != 0 {
return job.Script
}

return "[" + id + "]"
Expand Down
106 changes: 53 additions & 53 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (
".toml": toml.Parser(),
}

mergeActionsOption = koanf.WithMergeFunc(mergeActions)
mergeJobsOption = koanf.WithMergeFunc(mergeJobs)
)

// ConfigNotFoundError.
Expand All @@ -65,7 +65,7 @@ func loadOne(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) e
continue
}

if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], mergeActionsOption); err != nil {
if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], mergeJobsOption); err != nil {
return err
}

Expand Down Expand Up @@ -177,7 +177,7 @@ func loadRemotes(k *koanf.Koanf, filesystem afero.Fs, repo *git.Repository, remo
panic("TODO: unknown extension to parse")
}

if err := k.Load(kfs.Provider(newIOFS(filesystem), configPath), parser, mergeActionsOption); err != nil {
if err := k.Load(kfs.Provider(newIOFS(filesystem), configPath), parser, mergeJobsOption); err != nil {
return err
}

Expand Down Expand Up @@ -225,15 +225,15 @@ func extendRecursive(k *koanf.Koanf, filesystem afero.Fs, root string, extends [
if !ok {
panic("TODO: unknown extension for extent " + path)
}
if err := extent.Load(kfs.Provider(newIOFS(filesystem), path), parser, mergeActionsOption); err != nil {
if err := extent.Load(kfs.Provider(newIOFS(filesystem), path), parser, mergeJobsOption); err != nil {
return err
}

if err := extendRecursive(extent, filesystem, root, extent.Strings("extends"), visited); err != nil {
return err
}

if err := k.Load(koanfProvider{extent}, nil, mergeActionsOption); err != nil {
if err := k.Load(koanfProvider{extent}, nil, mergeJobsOption); err != nil {
return err
}
}
Expand Down Expand Up @@ -328,19 +328,19 @@ func addHook(name string, main, secondary *koanf.Koanf, c *Config) error {
default:
}

var destActions, srcActions []interface{}
switch actions := dest["actions"].(type) {
var destJobs, srcJobs []interface{}
switch jobs := dest["jobs"].(type) {
case []interface{}:
destActions = actions
destJobs = jobs
default:
}
switch actions := src["actions"].(type) {
switch jobs := src["jobs"].(type) {
case []interface{}:
srcActions = actions
srcJobs = jobs
default:
}

destActions = mergeActionsSlice(srcActions, destActions)
destJobs = mergeJobsSlice(srcJobs, destJobs)

maps.Merge(src, dest)

Expand All @@ -363,8 +363,8 @@ func addHook(name string, main, secondary *koanf.Koanf, c *Config) error {
}
}

if len(destActions) > 0 {
dest["actions"] = destActions
if len(destJobs) > 0 {
dest["jobs"] = destJobs
}

return nil
Expand Down Expand Up @@ -417,56 +417,56 @@ func (k koanfProvider) ReadBytes() ([]byte, error) {
panic("not implemented")
}

func mergeActions(src, dest map[string]interface{}) error {
srcActions := make(map[string][]interface{})
func mergeJobs(src, dest map[string]interface{}) error {
srcJobs := make(map[string][]interface{})

for name, maybeHook := range src {
switch hook := maybeHook.(type) {
case map[string]interface{}:
switch actions := hook["actions"].(type) {
switch jobs := hook["jobs"].(type) {
case []interface{}:
srcActions[name] = actions
srcJobs[name] = jobs
default:
}
default:
}
}

destActions := make(map[string][]interface{})
destJobs := make(map[string][]interface{})
for name, maybeHook := range dest {
switch hook := maybeHook.(type) {
case map[string]interface{}:
switch actions := hook["actions"].(type) {
switch jobs := hook["jobs"].(type) {
case []interface{}:
destActions[name] = actions
destJobs[name] = jobs
default:
}
default:
}
}

if len(srcActions) == 0 || len(destActions) == 0 {
if len(srcJobs) == 0 || len(destJobs) == 0 {
maps.Merge(src, dest)
return nil
}

for hook, newActions := range srcActions {
oldActions, ok := destActions[hook]
for hook, newJobs := range srcJobs {
oldJobs, ok := destJobs[hook]
if !ok {
destActions[hook] = newActions
destJobs[hook] = newJobs
continue
}

destActions[hook] = mergeActionsSlice(newActions, oldActions)
destJobs[hook] = mergeJobsSlice(newJobs, oldJobs)
}

maps.Merge(src, dest)

for name, maybeHook := range dest {
if actions, ok := destActions[name]; ok {
if jobs, ok := destJobs[name]; ok {
switch hook := maybeHook.(type) {
case map[string]interface{}:
hook["actions"] = actions
hook["jobs"] = jobs
default:
}
}
Expand All @@ -475,65 +475,65 @@ func mergeActions(src, dest map[string]interface{}) error {
return nil
}

func mergeActionsSlice(src, dest []interface{}) []interface{} {
func mergeJobsSlice(src, dest []interface{}) []interface{} {
mergeable := make(map[string]map[string]interface{})
result := make([]interface{}, 0, len(dest))

for _, maybeAction := range dest {
switch destAction := maybeAction.(type) {
for _, maybeJob := range dest {
switch destJob := maybeJob.(type) {
case map[string]interface{}:
switch name := destAction["name"].(type) {
switch name := destJob["name"].(type) {
case string:
mergeable[name] = destAction
mergeable[name] = destJob
default:
}

result = append(result, maybeAction)
result = append(result, maybeJob)
default:
}
}

for _, maybeAction := range src {
switch srcAction := maybeAction.(type) {
for _, maybeJob := range src {
switch srcJob := maybeJob.(type) {
case map[string]interface{}:
switch name := srcAction["name"].(type) {
switch name := srcJob["name"].(type) {
case string:
destAction, ok := mergeable[name]
destJob, ok := mergeable[name]
if ok {
var srcSubActions []interface{}
var destSubActions []interface{}
var srcSubJobs []interface{}
var destSubJobs []interface{}

switch srcGroup := srcAction["group"].(type) {
switch srcGroup := srcJob["group"].(type) {
case map[string]interface{}:
switch subActions := srcGroup["actions"].(type) {
switch subJobs := srcGroup["jobs"].(type) {
case []interface{}:
srcSubActions = subActions
srcSubJobs = subJobs
default:
}
default:
}
switch destGroup := destAction["group"].(type) {
switch destGroup := destJob["group"].(type) {
case map[string]interface{}:
switch subActions := destGroup["actions"].(type) {
switch subJobs := destGroup["jobs"].(type) {
case []interface{}:
destSubActions = subActions
destSubJobs = subJobs
default:
}
default:
}

if len(destSubActions) != 0 && len(srcSubActions) != 0 {
destSubActions = mergeActionsSlice(srcSubActions, destSubActions)
if len(destSubJobs) != 0 && len(srcSubJobs) != 0 {
destSubJobs = mergeJobsSlice(srcSubJobs, destSubJobs)
}

maps.Merge(srcAction, destAction)
maps.Merge(srcJob, destJob)

if len(destSubActions) != 0 {
switch destGroup := destAction["group"].(type) {
if len(destSubJobs) != 0 {
switch destGroup := destJob["group"].(type) {
case map[string]interface{}:
switch destGroup["actions"].(type) {
switch destGroup["jobs"].(type) {
case []interface{}:
destGroup["actions"] = destSubActions
destGroup["jobs"] = destSubJobs
default:
}
default:
Expand All @@ -544,7 +544,7 @@ func mergeActionsSlice(src, dest []interface{}) []interface{} {
default:
}

result = append(result, maybeAction)
result = append(result, maybeJob)
default:
}
}
Expand Down
28 changes: 14 additions & 14 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,18 +709,18 @@ pre-commit:
},
},
},
"with actions": {
"with jobs": {
files: map[string]string{
"lefthook.yml": `
pre-commit:
actions:
jobs:
- run: 1
- run: 2
name: second
`,
"lefthook-local.yml": `
pre-commit:
actions:
jobs:
- run: 3
- run: local 2
name: second
Expand All @@ -731,7 +731,7 @@ pre-commit:
SourceDirLocal: ".lefthook-local",
Hooks: map[string]*Hook{
"pre-commit": {
Actions: []*Action{
Jobs: []*Job{
{Run: "1"},
{Run: "local 2", Name: "second"},
{Run: "3"},
Expand All @@ -740,34 +740,34 @@ pre-commit:
},
},
},
"with nested actions": {
"with nested jobs": {
files: map[string]string{
"lefthook.yml": `
pre-commit:
actions:
jobs:
- name: group 1
group:
actions:
jobs:
- run: 1.1
- run: 1.2
- name: nested
group:
actions:
jobs:
- run: 1.nested.1
- run: 1.nested.2
name: nested 2
`,
"lefthook-local.yml": `
pre-commit:
actions:
jobs:
- name: group 1
glob: "*.rb"
group:
parallel: true
actions:
jobs:
- name: nested
group:
actions:
jobs:
- run: 1.nested.2 local
name: nested 2
- run: 1.nested.3
Expand All @@ -780,19 +780,19 @@ pre-commit:
SourceDirLocal: ".lefthook-local",
Hooks: map[string]*Hook{
"pre-commit": {
Actions: []*Action{
Jobs: []*Job{
{
Name: "group 1",
Glob: "*.rb",
Group: &Group{
Parallel: true,
Actions: []*Action{
Jobs: []*Job{
{Run: "1.1"},
{Run: "1.2"},
{
Name: "nested",
Group: &Group{
Actions: []*Action{
Jobs: []*Job{
{Run: "1.nested.1"},
{Run: "1.nested.2 local", Name: "nested 2"},
{Run: "1.nested.3"},
Expand Down
Loading

0 comments on commit 31458a6

Please sign in to comment.