|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package lefthook |
| 6 | + |
| 7 | +// Job is an entry under a hook's or group's jobs: list. Each Job either runs |
| 8 | +// a command directly (via Run or Script) or wraps a nested Group. |
| 9 | +type Job struct { //nolint:govet |
| 10 | + Name string `yaml:"name,omitempty"` |
| 11 | + Run string `yaml:"run,omitempty"` |
| 12 | + Script string `yaml:"script,omitempty"` |
| 13 | + Tags []string `yaml:"tags,omitempty"` |
| 14 | + Glob []string `yaml:"glob,omitempty"` |
| 15 | + Exclude []string `yaml:"exclude,omitempty"` |
| 16 | + Root string `yaml:"root,omitempty"` |
| 17 | + Env map[string]string `yaml:"env,omitempty"` |
| 18 | + Skip []string `yaml:"skip,omitempty"` |
| 19 | + Only []string `yaml:"only,omitempty"` |
| 20 | + Interactive bool `yaml:"interactive,omitempty"` |
| 21 | + StageFixed bool `yaml:"stage_fixed,omitempty"` |
| 22 | + Priority int `yaml:"priority,omitempty"` |
| 23 | + Group *Group `yaml:"group,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +// WithName sets the job's display name. |
| 27 | +func (j *Job) WithName(name string) *Job { |
| 28 | + j.Name = name |
| 29 | + |
| 30 | + return j |
| 31 | +} |
| 32 | + |
| 33 | +// WithRun sets the shell command to execute for this job. |
| 34 | +func (j *Job) WithRun(run string) *Job { |
| 35 | + j.Run = run |
| 36 | + |
| 37 | + return j |
| 38 | +} |
| 39 | + |
| 40 | +// WithScript sets a script file to execute (relative to lefthook source_dir). |
| 41 | +func (j *Job) WithScript(script string) *Job { |
| 42 | + j.Script = script |
| 43 | + |
| 44 | + return j |
| 45 | +} |
| 46 | + |
| 47 | +// WithTags attaches selectable tags (lefthook --tags ...). |
| 48 | +func (j *Job) WithTags(tags ...string) *Job { |
| 49 | + j.Tags = tags |
| 50 | + |
| 51 | + return j |
| 52 | +} |
| 53 | + |
| 54 | +// WithGlob restricts the job to files matching the given glob(s). |
| 55 | +func (j *Job) WithGlob(glob ...string) *Job { |
| 56 | + j.Glob = glob |
| 57 | + |
| 58 | + return j |
| 59 | +} |
| 60 | + |
| 61 | +// WithExclude is the inverse of WithGlob: skip files matching these patterns. |
| 62 | +func (j *Job) WithExclude(exclude ...string) *Job { |
| 63 | + j.Exclude = exclude |
| 64 | + |
| 65 | + return j |
| 66 | +} |
| 67 | + |
| 68 | +// WithRoot changes the working directory for the job. |
| 69 | +func (j *Job) WithRoot(root string) *Job { |
| 70 | + j.Root = root |
| 71 | + |
| 72 | + return j |
| 73 | +} |
| 74 | + |
| 75 | +// WithEnv sets an environment variable on the job; safe to call multiple times. |
| 76 | +func (j *Job) WithEnv(name, value string) *Job { |
| 77 | + if j.Env == nil { |
| 78 | + j.Env = map[string]string{} |
| 79 | + } |
| 80 | + |
| 81 | + j.Env[name] = value |
| 82 | + |
| 83 | + return j |
| 84 | +} |
| 85 | + |
| 86 | +// WithSkip lists git states or refs where the job should be skipped (e.g. "merge", "rebase"). |
| 87 | +func (j *Job) WithSkip(skip ...string) *Job { |
| 88 | + j.Skip = skip |
| 89 | + |
| 90 | + return j |
| 91 | +} |
| 92 | + |
| 93 | +// WithOnly is the inverse of WithSkip: job runs only in the listed states. |
| 94 | +func (j *Job) WithOnly(only ...string) *Job { |
| 95 | + j.Only = only |
| 96 | + |
| 97 | + return j |
| 98 | +} |
| 99 | + |
| 100 | +// WithInteractive marks the job as needing a TTY (stdin/stdout passthrough). |
| 101 | +func (j *Job) WithInteractive() *Job { |
| 102 | + j.Interactive = true |
| 103 | + |
| 104 | + return j |
| 105 | +} |
| 106 | + |
| 107 | +// WithStageFixed re-stages files modified by the job (useful for formatters). |
| 108 | +func (j *Job) WithStageFixed() *Job { |
| 109 | + j.StageFixed = true |
| 110 | + |
| 111 | + return j |
| 112 | +} |
| 113 | + |
| 114 | +// WithPriority sets the job's run order within its container (lower runs first). |
| 115 | +func (j *Job) WithPriority(priority int) *Job { |
| 116 | + j.Priority = priority |
| 117 | + |
| 118 | + return j |
| 119 | +} |
| 120 | + |
| 121 | +// AsGroup turns this job into a container for a nested Group, creating the |
| 122 | +// group on first call and returning it for chained configuration. The job's |
| 123 | +// Run/Script fields are typically left empty when AsGroup is used. |
| 124 | +func (j *Job) AsGroup() *Group { |
| 125 | + if j.Group != nil { |
| 126 | + return j.Group |
| 127 | + } |
| 128 | + |
| 129 | + j.Group = &Group{} |
| 130 | + |
| 131 | + return j.Group |
| 132 | +} |
0 commit comments