From e2387d09bbd99e1378dfa32e9c85e6d9fcd87887 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 19 Dec 2024 15:08:55 +0300 Subject: [PATCH] chore: rename actions to jobs --- internal/config/hook.go | 2 +- internal/config/{actions.go => job.go} | 24 +++--- internal/config/load.go | 106 ++++++++++++------------ internal/config/load_test.go | 28 +++---- internal/lefthook/runner/run_actions.go | 74 ++++++++--------- internal/lefthook/runner/runner.go | 2 +- lefthook.yml | 4 +- 7 files changed, 120 insertions(+), 120 deletions(-) rename internal/config/{actions.go => job.go} (75%) diff --git a/internal/config/hook.go b/internal/config/hook.go index 71eca2b8..75d28a47 100644 --- a/internal/config/hook.go +++ b/internal/config/hook.go @@ -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"` diff --git a/internal/config/actions.go b/internal/config/job.go similarity index 75% rename from internal/config/actions.go rename to internal/config/job.go index c32f4fc1..9dd27116 100644 --- a/internal/config/actions.go +++ b/internal/config/job.go @@ -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"` @@ -28,21 +28,21 @@ type Action struct { } 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"` + 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"` + 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 + "]" diff --git a/internal/config/load.go b/internal/config/load.go index 24c9d225..e4faff7a 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -45,7 +45,7 @@ var ( ".toml": toml.Parser(), } - mergeActionsOption = koanf.WithMergeFunc(mergeActions) + mergeJobsOption = koanf.WithMergeFunc(mergeJobs) ) // ConfigNotFoundError. @@ -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 } @@ -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 } @@ -225,7 +225,7 @@ 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 } @@ -233,7 +233,7 @@ func extendRecursive(k *koanf.Koanf, filesystem afero.Fs, root string, extends [ return err } - if err := k.Load(koanfProvider{extent}, nil, mergeActionsOption); err != nil { + if err := k.Load(koanfProvider{extent}, nil, mergeJobsOption); err != nil { return err } } @@ -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) @@ -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 @@ -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: } } @@ -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: @@ -544,7 +544,7 @@ func mergeActionsSlice(src, dest []interface{}) []interface{} { default: } - result = append(result, maybeAction) + result = append(result, maybeJob) default: } } diff --git a/internal/config/load_test.go b/internal/config/load_test.go index 672bdb81..f35e9069 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -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 @@ -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"}, @@ -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 @@ -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"}, diff --git a/internal/lefthook/runner/run_actions.go b/internal/lefthook/runner/run_actions.go index e0d55627..cb325f25 100644 --- a/internal/lefthook/runner/run_actions.go +++ b/internal/lefthook/runner/run_actions.go @@ -16,9 +16,9 @@ import ( ) var ( - errActionContainsBothRunAndScript = errors.New("both `run` and `script` are not permitted") - errEmptyAction = errors.New("no execution instructions") - errEmptyGroup = errors.New("empty groups are not permitted") + errJobContainsBothRunAndScript = errors.New("both `run` and `script` are not permitted") + errEmptyJob = errors.New("no execution instructions") + errEmptyGroup = errors.New("empty groups are not permitted") ) type domain struct { @@ -28,33 +28,33 @@ type domain struct { root string } -func (r *Runner) runActions(ctx context.Context) []Result { +func (r *Runner) runJobs(ctx context.Context) []Result { var wg sync.WaitGroup - results := make([]Result, 0, len(r.Hook.Actions)) - resultsChan := make(chan Result, len(r.Hook.Actions)) + results := make([]Result, 0, len(r.Hook.Jobs)) + resultsChan := make(chan Result, len(r.Hook.Jobs)) var failed atomic.Bool domain := &domain{failed: &failed} - for i, action := range r.Hook.Actions { + for i, job := range r.Hook.Jobs { id := strconv.Itoa(i) if domain.failed.Load() && r.Hook.Piped { - r.logSkip(action.PrintableName(id), "broken pipe") + r.logSkip(job.PrintableName(id), "broken pipe") continue } if !r.Hook.Parallel { - results = append(results, r.runAction(ctx, domain, id, action)) + results = append(results, r.runJob(ctx, domain, id, job)) continue } wg.Add(1) - go func(action *config.Action) { + go func(job *config.Job) { defer wg.Done() - resultsChan <- r.runAction(ctx, domain, id, action) - }(action) + resultsChan <- r.runJob(ctx, domain, id, job) + }(job) } wg.Wait() @@ -66,36 +66,36 @@ func (r *Runner) runActions(ctx context.Context) []Result { return results } -func (r *Runner) runAction(ctx context.Context, domain *domain, id string, action *config.Action) Result { - // Check if do action is properly configured - if len(action.Run) > 0 && len(action.Script) > 0 { - return failed(action.PrintableName(id), errActionContainsBothRunAndScript.Error()) +func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *config.Job) Result { + // Check if do job is properly configured + if len(job.Run) > 0 && len(job.Script) > 0 { + return failed(job.PrintableName(id), errJobContainsBothRunAndScript.Error()) } - if len(action.Run) == 0 && len(action.Script) == 0 && action.Group == nil { - return failed(action.PrintableName(id), errEmptyAction.Error()) + if len(job.Run) == 0 && len(job.Script) == 0 && job.Group == nil { + return failed(job.PrintableName(id), errEmptyJob.Error()) } - if action.Interactive && !r.DisableTTY && !r.Hook.Follow { + if job.Interactive && !r.DisableTTY && !r.Hook.Follow { log.StopSpinner() defer log.StartSpinner() } - if len(action.Run) != 0 || len(action.Script) != 0 { - return r.runSingleAction(ctx, domain, id, action) + if len(job.Run) != 0 || len(job.Script) != 0 { + return r.runSingleJob(ctx, domain, id, job) } - if action.Group != nil { + if job.Group != nil { inheritedDomain := *domain - inheritedDomain.glob = first(action.Glob, domain.glob) - inheritedDomain.root = first(action.Root, domain.root) - groupName := first(action.Name, "["+id+"]") - return r.runGroup(ctx, groupName, &inheritedDomain, action.Group) + inheritedDomain.glob = first(job.Glob, domain.glob) + inheritedDomain.root = first(job.Root, domain.root) + groupName := first(job.Name, "["+id+"]") + return r.runGroup(ctx, groupName, &inheritedDomain, job.Group) } - return failed(action.PrintableName(id), "don't know how to run action") + return failed(job.PrintableName(id), "don't know how to run job") } -func (r *Runner) runSingleAction(ctx context.Context, domain *domain, id string, act *config.Action) Result { +func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, act *config.Job) Result { name := act.PrintableName(id) runAction, err := action.New(name, &action.Params{ @@ -176,32 +176,32 @@ func (r *Runner) runSingleAction(ctx context.Context, domain *domain, id string, } func (r *Runner) runGroup(ctx context.Context, groupName string, domain *domain, group *config.Group) Result { - if len(group.Actions) == 0 { + if len(group.Jobs) == 0 { return failed(groupName, errEmptyGroup.Error()) } - results := make([]Result, 0, len(group.Actions)) - resultsChan := make(chan Result, len(group.Actions)) + results := make([]Result, 0, len(group.Jobs)) + resultsChan := make(chan Result, len(group.Jobs)) var wg sync.WaitGroup - for i, action := range group.Actions { + for i, job := range group.Jobs { id := strconv.Itoa(i) if domain.failed.Load() && group.Piped { - r.logSkip(action.PrintableName(id), "broken pipe") + r.logSkip(job.PrintableName(id), "broken pipe") continue } if !group.Parallel { - results = append(results, r.runAction(ctx, domain, id, action)) + results = append(results, r.runJob(ctx, domain, id, job)) continue } wg.Add(1) - go func(action *config.Action) { + go func(job *config.Job) { defer wg.Done() - resultsChan <- r.runAction(ctx, domain, id, action) - }(action) + resultsChan <- r.runJob(ctx, domain, id, job) + }(job) } wg.Wait() diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index 452d2ca7..cb54babd 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -91,7 +91,7 @@ func (r *Runner) RunAll(ctx context.Context) ([]Result, error) { defer log.StopSpinner() } - results = append(results, r.runActions(ctx)...) + results = append(results, r.runJobs(ctx)...) scriptDirs := make([]string, 0, len(r.SourceDirs)) for _, sourceDir := range r.SourceDirs { diff --git a/lefthook.yml b/lefthook.yml index d1e390ad..1a753d89 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -2,11 +2,11 @@ skip_lfs: true pre-commit: parallel: true - actions: + jobs: - name: lint & test glob: "*.go" group: - actions: + jobs: - run: make lint stage_fixed: true