Skip to content

Commit

Permalink
feat: add ability to specify job names to run command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Dec 21, 2024
1 parent 0a15c5e commit 94e3757
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (run) New(opts *lefthook.Options) *cobra.Command {
"run only specified commands",
)

runCmd.Flags().StringSliceVar(
&runArgs.RunOnlyJobs, "jobs", nil,
"run only specified jobs",
)

err := runCmd.Flags().MarkDeprecated("files", "use --file flag instead")
if err != nil {
log.Warn("Unexpected error:", err)
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type RunArgs struct {
SkipLFS bool
Files []string
RunOnlyCommands []string
RunOnlyJobs []string
}

func Run(opts *Options, args RunArgs, hookName string, gitArgs []string) error {
Expand Down Expand Up @@ -170,6 +171,7 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
Files: args.Files,
Force: args.Force,
RunOnlyCommands: args.RunOnlyCommands,
RunOnlyJobs: args.RunOnlyJobs,
SourceDirs: sourceDirs,
},
)
Expand Down
15 changes: 15 additions & 0 deletions internal/lefthook/runner/run_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"path/filepath"
"slices"
"strconv"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -81,6 +82,10 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con
}

if len(job.Run) != 0 || len(job.Script) != 0 {
if len(r.RunOnlyJobs) != 0 && !slices.Contains(r.RunOnlyJobs, job.Name) {
return skipped(job.PrintableName(id))
}

return r.runSingleJob(ctx, domain, id, job)
}

Expand All @@ -89,6 +94,16 @@ func (r *Runner) runJob(ctx context.Context, domain *domain, id string, job *con
inheritedDomain.glob = first(job.Glob, domain.glob)
inheritedDomain.root = first(job.Root, domain.root)
groupName := first(job.Name, "["+id+"]")

if len(r.RunOnlyJobs) != 0 && slices.Contains(r.RunOnlyJobs, job.Name) {
children := make([]string, len(job.Group.Jobs), 0)
for _, child := range job.Group.Jobs {
children = append(children, child.Name)
}

r.RunOnlyJobs = append(r.RunOnlyJobs, children...)
}

return r.runGroup(ctx, groupName, &inheritedDomain, job.Group)
}

Expand Down
1 change: 1 addition & 0 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Options struct {
Force bool
Files []string
RunOnlyCommands []string
RunOnlyJobs []string
SourceDirs []string
}

Expand Down

0 comments on commit 94e3757

Please sign in to comment.