|
| 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 | +// Command represents a single named command under a hook's commands: map. |
| 8 | +type Command struct { //nolint:govet |
| 9 | + Run string `yaml:"run"` |
| 10 | + Tags []string `yaml:"tags,omitempty"` |
| 11 | + Glob string `yaml:"glob,omitempty"` |
| 12 | + Files string `yaml:"files,omitempty"` |
| 13 | + Skip []string `yaml:"skip,omitempty"` |
| 14 | + Only []string `yaml:"only,omitempty"` |
| 15 | + Interactive bool `yaml:"interactive,omitempty"` |
| 16 | + StageFixed bool `yaml:"stage_fixed,omitempty"` |
| 17 | + Priority int `yaml:"priority,omitempty"` |
| 18 | +} |
| 19 | + |
| 20 | +// WithRun sets the shell command lefthook executes for this command. |
| 21 | +func (c *Command) WithRun(run string) *Command { |
| 22 | + c.Run = run |
| 23 | + |
| 24 | + return c |
| 25 | +} |
| 26 | + |
| 27 | +// WithTags attaches tags used for selective hook execution (lefthook --tags ...). |
| 28 | +func (c *Command) WithTags(tags ...string) *Command { |
| 29 | + c.Tags = tags |
| 30 | + |
| 31 | + return c |
| 32 | +} |
| 33 | + |
| 34 | +// WithGlob restricts the command to files matching the given glob. |
| 35 | +func (c *Command) WithGlob(glob string) *Command { |
| 36 | + c.Glob = glob |
| 37 | + |
| 38 | + return c |
| 39 | +} |
| 40 | + |
| 41 | +// WithFiles overrides the default file source (e.g. "git diff --name-only ..."). |
| 42 | +func (c *Command) WithFiles(files string) *Command { |
| 43 | + c.Files = files |
| 44 | + |
| 45 | + return c |
| 46 | +} |
| 47 | + |
| 48 | +// WithSkip lists git states or refs where the command should be skipped (e.g. "merge", "rebase"). |
| 49 | +func (c *Command) WithSkip(skip ...string) *Command { |
| 50 | + c.Skip = skip |
| 51 | + |
| 52 | + return c |
| 53 | +} |
| 54 | + |
| 55 | +// WithOnly is the inverse of WithSkip: command runs only in the listed states. |
| 56 | +func (c *Command) WithOnly(only ...string) *Command { |
| 57 | + c.Only = only |
| 58 | + |
| 59 | + return c |
| 60 | +} |
| 61 | + |
| 62 | +// WithInteractive marks the command as needing a TTY (stdin/stdout passthrough). |
| 63 | +func (c *Command) WithInteractive() *Command { |
| 64 | + c.Interactive = true |
| 65 | + |
| 66 | + return c |
| 67 | +} |
| 68 | + |
| 69 | +// WithStageFixed re-stages files modified by the command (useful for formatters). |
| 70 | +func (c *Command) WithStageFixed() *Command { |
| 71 | + c.StageFixed = true |
| 72 | + |
| 73 | + return c |
| 74 | +} |
| 75 | + |
| 76 | +// WithPriority sets the command's run order within its hook (lower runs first). |
| 77 | +func (c *Command) WithPriority(priority int) *Command { |
| 78 | + c.Priority = priority |
| 79 | + |
| 80 | + return c |
| 81 | +} |
0 commit comments