commands

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InitConfigFileName = validator.ConfigName + ".yml"
	InitConfigFileMode = 0o600
)
View Source
const InitConfigData = `` /* 549-byte string literal not displayed */

Variables

View Source
var (
	ErrFileExists = errors.New("file already exists")
	ErrOpen       = errors.New("cannot open file")
	ErrWrite      = errors.New("cannot write content")
)
View Source
var InitCMD = &cli.Command{
	Name:  "init",
	Usage: "Initialize a config file",
	Action: func(ctx context.Context, cmd *cli.Command) error {
		f, err := os.OpenFile(InitConfigFileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, InitConfigFileMode)
		if err != nil {
			if errors.Is(err, os.ErrExist) {
				return fmt.Errorf("%w: %s", ErrFileExists, InitConfigFileName)
			}

			return fmt.Errorf("%w: %w", ErrOpen, err)
		}

		defer func() {
			if closeErr := f.Close(); closeErr != nil && err == nil {
				_, _ = fmt.Fprintf(cmd.ErrWriter, "%s: %s", ErrWrite, closeErr)
			}
		}()

		_, err = f.WriteString(InitConfigData)
		if err != nil {
			return fmt.Errorf("%w: %w", ErrWrite, err)
		}

		return nil
	},
}
View Source
var RunCMD = &cli.Command{
	Name:      "run",
	Usage:     "Lint commit scopes against changed files",
	UsageText: "commitlint-scope run --from <sha> --to <sha>",
	Description: `Validate that scopes declared in commit messages correspond to actually changed files

The command inspects a range of commits (from exclusive, to inclusive) and reports any scope that does not match the files modified in that commit.

Examples:
  commitlint-scope run --from main --to feature-branch
  commitlint-scope run --from HEAD~5 --to HEAD
`,
	Suggest: true,
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:        "from",
			Aliases:     []string{"f"},
			Usage:       "Start of the commit range (exclusive)",
			Required:    true,
			Destination: &flagRunFrom,
		},
		&cli.StringFlag{
			Name:        "to",
			Aliases:     []string{"t"},
			Usage:       "End of the commit range (inclusive)",
			Required:    true,
			Destination: &flagRunTo,
		},
		&cli.BoolFlag{
			Name:        "verbose",
			Aliases:     []string{"v"},
			Usage:       "Verbose output",
			Required:    false,
			Destination: &flagRunVerbose,
		},
		&cli.BoolFlag{
			Name:        "no-color",
			Usage:       "Disable color output",
			Required:    false,
			Destination: &flagRunNoColor,
		},
		&cli.BoolFlag{
			Name:        "json",
			Usage:       "Show output in JSON format",
			Required:    false,
			Destination: &flagRunJSON,
		},
	},

	Action: func(ctx context.Context, cmd *cli.Command) error {
		color.NoColor = flagRunNoColor

		configureLogger(os.Stderr, flagRunVerbose)

		cfg, err := validator.LoadConfig()
		if err != nil {
			return fmt.Errorf("loading config: %w", err)
		}

		vld, err := validator.NewValidator(cfg, validator.Options{
			Logger:         slog.Default(),
			SHALength:      7,
			Git:            nil,
			OutsiderFinder: nil,
			ScopeParser:    nil,
		})
		if err != nil {
			return fmt.Errorf("creating validator: %w", err)
		}

		violations, err := vld.Validate(ctx, flagRunFrom, flagRunTo)
		if err != nil {
			return fmt.Errorf("validation failed: %w", err)
		}

		if len(violations) == 0 {
			return nil
		}

		if flagRunJSON {
			err := jsonOutput(cmd.Writer, violations)
			if err != nil {
				return err
			}
		} else {
			textOutput(cmd.Writer, violations)
		}

		return errs.NewViolationsFound(len(violations))
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL