1
This repository has been archived on 2025-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
Files
go-commitlint/internal/commitlint/commitlint.go
Voomra af99bebf91 add: import code
портирован код из старого репозитория https://di9.ru/git/Voomra/Conventional-Commits
2025-07-30 18:44:50 +03:00

64 lines
1.4 KiB
Go

package commitlint
import (
"bufio"
"commitlint/internal/commitlint/configuration"
"commitlint/internal/commitlint/validator"
"fmt"
"os"
)
func EntryPoint(pCommitMessagePath *string, pConfigPath *string) error {
pConfig, err := configuration.CreateConfig(pConfigPath)
if err != nil {
return err
}
commitMessage, err := ReadCommitMessage(*pCommitMessagePath)
if err != nil {
return err
}
pResult := validator.Validate(commitMessage, *pConfig)
if pResult != nil {
var errMessage string
switch pResult.Result {
case validator.IncorrectPattern:
errMessage = "Incorrect commit message format"
case validator.UnknownType:
errMessage = fmt.Sprintf("Unknown commit type '%s'", pResult.UnknownType)
case validator.UnknownContext:
errMessage = fmt.Sprintf("Unknown commit context '%s'", pResult.UnknownContext)
case validator.OverlongLine:
errMessage = fmt.Sprintf("Line number %d exceeds the allowed length", pResult.Line)
case validator.BlankLine:
errMessage = "Use blank line before BODY"
}
_, err := fmt.Fprintln(os.Stderr, errMessage)
return err
}
return nil
}
func ReadCommitMessage(path string) ([]string, error) {
var lines []string
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer func(file *os.File) {
_ = file.Close()
}(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, err
}