From 82319c0b4051bd061a7b6542a5b49e497a84ce3f Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 19 Dec 2024 17:06:11 +0300 Subject: [PATCH] fix: rename actions to jobs --- .../runner/{action => jobs}/build_command.go | 16 +++--- .../{action => jobs}/build_command_test.go | 22 ++++---- .../runner/{action => jobs}/build_script.go | 6 +-- .../runner/{action/action.go => jobs/jobs.go} | 14 ++--- .../runner/{action => jobs}/skip_error.go | 2 +- internal/lefthook/runner/run_jobs.go | 52 +++++++++---------- internal/lefthook/runner/runner.go | 16 +++--- 7 files changed, 64 insertions(+), 64 deletions(-) rename internal/lefthook/runner/{action => jobs}/build_command.go (92%) rename internal/lefthook/runner/{action => jobs}/build_command_test.go (95%) rename internal/lefthook/runner/{action => jobs}/build_script.go (95%) rename internal/lefthook/runner/{action/action.go => jobs/jobs.go} (88%) rename internal/lefthook/runner/{action => jobs}/skip_error.go (92%) diff --git a/internal/lefthook/runner/action/build_command.go b/internal/lefthook/runner/jobs/build_command.go similarity index 92% rename from internal/lefthook/runner/action/build_command.go rename to internal/lefthook/runner/jobs/build_command.go index 9d51bebe..a4ce0739 100644 --- a/internal/lefthook/runner/action/build_command.go +++ b/internal/lefthook/runner/jobs/build_command.go @@ -1,4 +1,4 @@ -package action +package jobs import ( "fmt" @@ -22,7 +22,7 @@ type template struct { cnt int } -func buildCommand(params *Params) (*Action, error) { +func buildCommand(params *Params) (*Job, error) { if err := params.validateCommand(); err != nil { return nil, err } @@ -124,7 +124,7 @@ func buildCommand(params *Params) (*Action, error) { } if config.HookUsesStagedFiles(params.HookName) { - ok, err := canSkipAction(params, filterParams, templates[config.SubStagedFiles], params.Repo.StagedFiles) + ok, err := canSkipJob(params, filterParams, templates[config.SubStagedFiles], params.Repo.StagedFiles) if err != nil { return nil, err } @@ -134,7 +134,7 @@ func buildCommand(params *Params) (*Action, error) { } if config.HookUsesPushFiles(params.HookName) { - ok, err := canSkipAction(params, filterParams, templates[config.SubPushFiles], params.Repo.PushFiles) + ok, err := canSkipJob(params, filterParams, templates[config.SubPushFiles], params.Repo.PushFiles) if err != nil { return nil, err } @@ -146,7 +146,7 @@ func buildCommand(params *Params) (*Action, error) { return result, nil } -func canSkipAction(params *Params, filterParams filters.Params, template *template, filesFn func() ([]string, error)) (bool, error) { +func canSkipJob(params *Params, filterParams filters.Params, template *template, filesFn func() ([]string, error)) (bool, error) { if template != nil { return len(template.files) == 0, nil } @@ -184,9 +184,9 @@ func escapeFiles(files []string) []string { return filesEsc } -func replaceInChunks(str string, templates map[string]*template, maxlen int) *Action { +func replaceInChunks(str string, templates map[string]*template, maxlen int) *Job { if len(templates) == 0 { - return &Action{ + return &Job{ Execs: []string{str}, } } @@ -232,7 +232,7 @@ func replaceInChunks(str string, templates map[string]*template, maxlen int) *Ac } } - return &Action{ + return &Job{ Execs: commands, Files: allFiles, } diff --git a/internal/lefthook/runner/action/build_command_test.go b/internal/lefthook/runner/jobs/build_command_test.go similarity index 95% rename from internal/lefthook/runner/action/build_command_test.go rename to internal/lefthook/runner/jobs/build_command_test.go index 67d25c14..7e654fd2 100644 --- a/internal/lefthook/runner/action/build_command_test.go +++ b/internal/lefthook/runner/jobs/build_command_test.go @@ -1,4 +1,4 @@ -package action +package jobs import ( "fmt" @@ -70,7 +70,7 @@ func Test_replaceInChunks(t *testing.T) { str string templates map[string]*template maxlen int - action *Action + job *Job }{ { str: "echo {staged_files}", @@ -81,7 +81,7 @@ func Test_replaceInChunks(t *testing.T) { }, }, maxlen: 300, - action: &Action{ + job: &Job{ Execs: []string{"echo file1 file2 file3"}, Files: []string{"file1", "file2", "file3"}, }, @@ -95,7 +95,7 @@ func Test_replaceInChunks(t *testing.T) { }, }, maxlen: 10, - action: &Action{ + job: &Job{ Execs: []string{ "echo file1", "echo file2", @@ -113,7 +113,7 @@ func Test_replaceInChunks(t *testing.T) { }, }, maxlen: 49, // (49 - 17(len of command without templates)) / 2 = 16, but we need 17 (3 words + 2 spaces) - action: &Action{ + job: &Job{ Execs: []string{ "echo file1 file2 && git add file1 file2", "echo file3 && git add file3", @@ -130,7 +130,7 @@ func Test_replaceInChunks(t *testing.T) { }, }, maxlen: 51, - action: &Action{ + job: &Job{ Execs: []string{ "echo file1 file2 file3 && git add file1 file2 file3", }, @@ -150,7 +150,7 @@ func Test_replaceInChunks(t *testing.T) { }, }, maxlen: 10, - action: &Action{ + job: &Job{ Execs: []string{ "echo push-file && git add file1", "echo push-file && git add file2", @@ -171,7 +171,7 @@ func Test_replaceInChunks(t *testing.T) { }, }, maxlen: 27, - action: &Action{ + job: &Job{ Execs: []string{ "echo push1 && git add file1", "echo push2 && git add file2", @@ -183,10 +183,10 @@ func Test_replaceInChunks(t *testing.T) { } { t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) { assert := assert.New(t) - action := replaceInChunks(tt.str, tt.templates, tt.maxlen) + job := replaceInChunks(tt.str, tt.templates, tt.maxlen) - assert.ElementsMatch(action.Files, tt.action.Files) - assert.Equal(action.Execs, tt.action.Execs) + assert.ElementsMatch(job.Files, tt.job.Files) + assert.Equal(job.Execs, tt.job.Execs) }) } } diff --git a/internal/lefthook/runner/action/build_script.go b/internal/lefthook/runner/jobs/build_script.go similarity index 95% rename from internal/lefthook/runner/action/build_script.go rename to internal/lefthook/runner/jobs/build_script.go index 2a504d05..356583fa 100644 --- a/internal/lefthook/runner/action/build_script.go +++ b/internal/lefthook/runner/jobs/build_script.go @@ -1,4 +1,4 @@ -package action +package jobs import ( "fmt" @@ -24,7 +24,7 @@ func (s scriptNotExistsError) Error() string { return fmt.Sprintf("script does not exist: %s", s.scriptPath) } -func buildScript(params *Params) (*Action, error) { +func buildScript(params *Params) (*Job, error) { if err := params.validateScript(); err != nil { return nil, err } @@ -73,7 +73,7 @@ func buildScript(params *Params) (*Action, error) { return nil, scriptNotExistsError{params.Script} } - return &Action{ + return &Job{ Execs: execs, Files: []string{}, }, nil diff --git a/internal/lefthook/runner/action/action.go b/internal/lefthook/runner/jobs/jobs.go similarity index 88% rename from internal/lefthook/runner/action/action.go rename to internal/lefthook/runner/jobs/jobs.go index 005080a5..44363443 100644 --- a/internal/lefthook/runner/action/action.go +++ b/internal/lefthook/runner/jobs/jobs.go @@ -1,4 +1,4 @@ -package action +package jobs import ( "github.com/evilmartians/lefthook/internal/config" @@ -28,12 +28,12 @@ type Params struct { Skip interface{} } -type Action struct { +type Job struct { Execs []string Files []string } -func New(name string, params *Params) (*Action, error) { +func New(name string, params *Params) (*Job, error) { if params.skip() { return nil, SkipError{"settings"} } @@ -47,18 +47,18 @@ func New(name string, params *Params) (*Action, error) { } var err error - var action *Action + var job *Job if len(params.Run) != 0 { - action, err = buildCommand(params) + job, err = buildCommand(params) } else { - action, err = buildScript(params) + job, err = buildScript(params) } if err != nil { return nil, err } - return action, nil + return job, nil } func (p *Params) skip() bool { diff --git a/internal/lefthook/runner/action/skip_error.go b/internal/lefthook/runner/jobs/skip_error.go similarity index 92% rename from internal/lefthook/runner/action/skip_error.go rename to internal/lefthook/runner/jobs/skip_error.go index 66e68cd2..5358c021 100644 --- a/internal/lefthook/runner/action/skip_error.go +++ b/internal/lefthook/runner/jobs/skip_error.go @@ -1,4 +1,4 @@ -package action +package jobs // SkipError implements error interface but indicates that the execution needs to be skipped. type SkipError struct { diff --git a/internal/lefthook/runner/run_jobs.go b/internal/lefthook/runner/run_jobs.go index 31fa8ad8..827eeccb 100644 --- a/internal/lefthook/runner/run_jobs.go +++ b/internal/lefthook/runner/run_jobs.go @@ -9,9 +9,9 @@ import ( "sync/atomic" "github.com/evilmartians/lefthook/internal/config" - "github.com/evilmartians/lefthook/internal/lefthook/runner/action" "github.com/evilmartians/lefthook/internal/lefthook/runner/exec" "github.com/evilmartians/lefthook/internal/lefthook/runner/filters" + "github.com/evilmartians/lefthook/internal/lefthook/runner/jobs" "github.com/evilmartians/lefthook/internal/log" ) @@ -95,12 +95,12 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con return failed(job.PrintableName(id), "don't know how to run job") } -func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, act *config.Job) Result { - name := act.PrintableName(id) +func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, job *config.Job) Result { + name := job.PrintableName(id) - root := first(act.Root, domain.root) - glob := first(act.Glob, domain.glob) - runAction, err := action.New(name, &action.Params{ + root := first(job.Root, domain.root) + glob := first(job.Glob, domain.glob) + executionJob, err := jobs.New(name, &jobs.Params{ Repo: r.Repo, Hook: r.Hook, HookName: r.HookName, @@ -108,22 +108,22 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, ac Force: r.Force, SourceDirs: r.SourceDirs, GitArgs: r.GitArgs, - Run: act.Run, + Run: job.Run, Root: root, - Runner: act.Runner, - Script: act.Script, + Runner: job.Runner, + Script: job.Script, Glob: glob, - Files: act.Files, - FileTypes: act.FileTypes, - Tags: act.Tags, - Exclude: act.Exclude, - Only: act.Only, - Skip: act.Skip, + Files: job.Files, + FileTypes: job.FileTypes, + Tags: job.Tags, + Exclude: job.Exclude, + Only: job.Only, + Skip: job.Skip, }) if err != nil { r.logSkip(name, err.Error()) - var skipErr action.SkipError + var skipErr jobs.SkipError if errors.As(err, &skipErr) { return skipped(name) } @@ -134,20 +134,20 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, ac ok := r.run(ctx, exec.Options{ Name: name, - Root: filepath.Join(r.Repo.RootPath, act.Root), - Commands: runAction.Execs, - Interactive: act.Interactive && !r.DisableTTY, - UseStdin: act.UseStdin, - Env: act.Env, + Root: filepath.Join(r.Repo.RootPath, root), + Commands: executionJob.Execs, + Interactive: job.Interactive && !r.DisableTTY, + UseStdin: job.UseStdin, + Env: job.Env, }, r.Hook.Follow) if !ok { domain.failed.Store(true) - return failed(name, act.FailText) + return failed(name, job.FailText) } - if config.HookUsesStagedFiles(r.HookName) && act.StageFixed { - files := runAction.Files + if config.HookUsesStagedFiles(r.HookName) && job.StageFixed { + files := executionJob.Files if len(files) == 0 { var err error @@ -160,8 +160,8 @@ func (r *Runner) runSingleJob(ctx context.Context, domain *domain, id string, ac files = filters.Apply(r.Repo.Fs, files, filters.Params{ Glob: glob, Root: root, - Exclude: act.Exclude, - FileTypes: act.FileTypes, + Exclude: job.Exclude, + FileTypes: job.FileTypes, }) } diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index cb54babd..aa88e913 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -21,9 +21,9 @@ import ( "github.com/evilmartians/lefthook/internal/config" "github.com/evilmartians/lefthook/internal/git" - "github.com/evilmartians/lefthook/internal/lefthook/runner/action" "github.com/evilmartians/lefthook/internal/lefthook/runner/exec" "github.com/evilmartians/lefthook/internal/lefthook/runner/filters" + "github.com/evilmartians/lefthook/internal/lefthook/runner/jobs" "github.com/evilmartians/lefthook/internal/log" "github.com/evilmartians/lefthook/internal/system" ) @@ -332,7 +332,7 @@ func (r *Runner) runScripts(ctx context.Context, dir string) []Result { } func (r *Runner) runScript(ctx context.Context, script *config.Script, file os.FileInfo) Result { - scriptAction, err := action.New(file.Name(), &action.Params{ + job, err := jobs.New(file.Name(), &jobs.Params{ Repo: r.Repo, Hook: r.Hook, HookName: r.HookName, @@ -349,7 +349,7 @@ func (r *Runner) runScript(ctx context.Context, script *config.Script, file os.F if err != nil { r.logSkip(file.Name(), err.Error()) - var skipErr action.SkipError + var skipErr jobs.SkipError if errors.As(err, &skipErr) { return skipped(file.Name()) } @@ -366,7 +366,7 @@ func (r *Runner) runScript(ctx context.Context, script *config.Script, file os.F ok := r.run(ctx, exec.Options{ Name: file.Name(), Root: r.Repo.RootPath, - Commands: scriptAction.Execs, + Commands: job.Execs, Interactive: script.Interactive && !r.DisableTTY, UseStdin: script.UseStdin, Env: script.Env, @@ -450,7 +450,7 @@ func (r *Runner) runCommands(ctx context.Context) []Result { } func (r *Runner) runCommand(ctx context.Context, name string, command *config.Command) Result { - runAction, err := action.New(name, &action.Params{ + job, err := jobs.New(name, &jobs.Params{ Repo: r.Repo, Hook: r.Hook, HookName: r.HookName, @@ -470,7 +470,7 @@ func (r *Runner) runCommand(ctx context.Context, name string, command *config.Co if err != nil { r.logSkip(name, err.Error()) - var skipErr action.SkipError + var skipErr jobs.SkipError if errors.As(err, &skipErr) { return skipped(name) } @@ -487,7 +487,7 @@ func (r *Runner) runCommand(ctx context.Context, name string, command *config.Co ok := r.run(ctx, exec.Options{ Name: name, Root: filepath.Join(r.Repo.RootPath, command.Root), - Commands: runAction.Execs, + Commands: job.Execs, Interactive: command.Interactive && !r.DisableTTY, UseStdin: command.UseStdin, Env: command.Env, @@ -501,7 +501,7 @@ func (r *Runner) runCommand(ctx context.Context, name string, command *config.Co result := succeeded(name) if config.HookUsesStagedFiles(r.HookName) && command.StageFixed { - files := runAction.Files + files := job.Files if len(files) == 0 { var err error