add: import code
портирован код из старого репозитория https://di9.ru/git/Voomra/Conventional-Commits
This commit is contained in:
105
internal/commitlint/configuration/config.go
Normal file
105
internal/commitlint/configuration/config.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type UserConfig struct {
|
||||
Types *[]string `json:"types"`
|
||||
Contexts *[]string `json:"contexts"`
|
||||
Excludes *[]string `json:"excludes"`
|
||||
MaxLengthLine *int `json:"maxLengthLine"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Types []string
|
||||
Contexts []string
|
||||
Excludes []*regexp.Regexp
|
||||
MaxLengthLine int
|
||||
}
|
||||
|
||||
func CreateConfig(pPath *string) (*Config, error) {
|
||||
config := Config{
|
||||
Types: []string{"fix", "feat"},
|
||||
Contexts: []string{},
|
||||
Excludes: []*regexp.Regexp{},
|
||||
MaxLengthLine: 72,
|
||||
}
|
||||
|
||||
if pPath != nil && isFileExists(*pPath) {
|
||||
userConfig, err := readUserConfig(*pPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mergeConfigs(&config, userConfig)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func isFileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func readUserConfig(path string) (*UserConfig, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(fileJson *os.File) {
|
||||
err := fileJson.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(file)
|
||||
|
||||
bytes, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var userConfig UserConfig
|
||||
err = json.Unmarshal(bytes, &userConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userConfig, nil
|
||||
}
|
||||
|
||||
func mergeConfigs(config *Config, userConfig *UserConfig) {
|
||||
if userConfig.Types != nil {
|
||||
for _, item := range *(*userConfig).Types {
|
||||
config.Types = append(config.Types, item)
|
||||
}
|
||||
}
|
||||
|
||||
if userConfig.Contexts != nil {
|
||||
for _, item := range *(userConfig.Contexts) {
|
||||
config.Contexts = append(config.Contexts, item)
|
||||
}
|
||||
}
|
||||
|
||||
if userConfig.Excludes != nil {
|
||||
for _, item := range *(userConfig.Excludes) {
|
||||
pattern, err := regexp.Compile("(?i)" + item)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
config.Excludes = append(config.Excludes, pattern)
|
||||
}
|
||||
}
|
||||
|
||||
if userConfig.MaxLengthLine != nil {
|
||||
config.MaxLengthLine = *(userConfig.MaxLengthLine)
|
||||
}
|
||||
}
|
||||
69
internal/commitlint/configuration/config_test.go
Normal file
69
internal/commitlint/configuration/config_test.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadUserConfig(t *testing.T) {
|
||||
createTmpJson := func() string {
|
||||
tmpFile, err := os.CreateTemp("", "config.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func(tmpFile *os.File) {
|
||||
_ = tmpFile.Close()
|
||||
}(tmpFile)
|
||||
|
||||
_, err = tmpFile.WriteString(`{"types": ["type1", "type2"], "contexts": ["ctx1", "ctx2"], "excludes": ["^wip$"], "maxLengthLine": 14}`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return tmpFile.Name()
|
||||
}
|
||||
|
||||
expectedUserConfig := UserConfig{}
|
||||
expectedUserConfig.Types = &([]string{"type1", "type2"})
|
||||
expectedUserConfig.Contexts = &([]string{"ctx1", "ctx2"})
|
||||
expectedUserConfig.Excludes = &([]string{"^wip$"})
|
||||
expectedUserConfig.MaxLengthLine = new(int)
|
||||
*(expectedUserConfig.MaxLengthLine) = 14
|
||||
|
||||
userConfig, err := readUserConfig(createTmpJson())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, expectedUserConfig, *userConfig)
|
||||
}
|
||||
|
||||
func TestMergeConfigs(t *testing.T) {
|
||||
config := Config{
|
||||
Types: []string{"fix", "feat"},
|
||||
Contexts: []string{},
|
||||
Excludes: []*regexp.Regexp{},
|
||||
MaxLengthLine: 72,
|
||||
}
|
||||
|
||||
userConfig := UserConfig{}
|
||||
userConfig.Types = &([]string{"type1", "type2"})
|
||||
userConfig.Contexts = &([]string{"ctx1", "ctx2"})
|
||||
userConfig.Excludes = &([]string{"^wip$"})
|
||||
userConfig.MaxLengthLine = new(int)
|
||||
*(userConfig.MaxLengthLine) = 14
|
||||
|
||||
expectedConfig := Config{
|
||||
Types: []string{"fix", "feat", "type1", "type2"},
|
||||
Contexts: []string{"ctx1", "ctx2"},
|
||||
Excludes: []*regexp.Regexp{},
|
||||
MaxLengthLine: 14,
|
||||
}
|
||||
expectedConfig.Excludes = append(expectedConfig.Excludes, regexp.MustCompile("^wip$"))
|
||||
|
||||
mergeConfigs(&config, &userConfig)
|
||||
|
||||
assert.Equal(t, expectedConfig, config)
|
||||
}
|
||||
Reference in New Issue
Block a user