add: import code
портирован код из старого репозитория https://di9.ru/git/Voomra/Conventional-Commits
This commit is contained in:
63
internal/commitlint/commitlint.go
Normal file
63
internal/commitlint/commitlint.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user