Skip to content

Commit 1ba0a41

Browse files
committed
simplify code
1 parent 9783a19 commit 1ba0a41

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

modules/repository/init.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,13 @@ var (
4747
)
4848

4949
type optionFileList struct {
50-
all []string
51-
custom []string
52-
}
53-
54-
// mergeCustomFiles merges the custom files, de-duplicates and returns a sorted list of files
55-
func mergeCustomFiles(fl optionFileList) []string {
56-
files := fl.all
57-
for _, f := range fl.custom {
58-
// for most cases, the SliceContainsString is good enough because there are only a few custom files
59-
if !util.SliceContainsString(files, f) {
60-
files = append(files, f)
61-
}
62-
}
63-
sort.Strings(files)
64-
return files
50+
all []string // all files provided by bindata & custom-path. sorted.
51+
custom []string // custom files provided by custom-path. non-sorted.
6552
}
6653

6754
// mergeCustomLabels merges the custom label files. Always use the file's main name as the key to de-duplicate.
6855
func mergeCustomLabels(fl optionFileList) []string {
69-
exts := map[string]int{
70-
"": 0,
71-
".yml": 1,
72-
".yaml": 2,
73-
}
56+
exts := map[string]int{"": 0, ".yml": 1, ".yaml": 2} // "yaml" file has the highest priority to be used.
7457
sort.Slice(fl.all, func(i, j int) bool {
7558
return exts[filepath.Ext(fl.all[i])] < exts[filepath.Ext(fl.all[j])]
7659
})
@@ -98,32 +81,31 @@ func mergeCustomLabels(fl optionFileList) []string {
9881

9982
// LoadRepoConfig loads the repository config
10083
func LoadRepoConfig() error {
101-
// Load .gitignore and license files and readme templates.
102-
types := []string{"gitignore", "license", "readme", "label"}
103-
optionTypeFiles := make([]optionFileList, len(types))
104-
84+
types := []string{"gitignore", "license", "readme", "label"} // option file directories
85+
typeFiles := make([]optionFileList, len(types))
10586
for i, t := range types {
10687
var err error
107-
if optionTypeFiles[i].all, err = options.Dir(t); err != nil {
88+
if typeFiles[i].all, err = options.Dir(t); err != nil {
10889
return fmt.Errorf("failed to list %s files: %w", t, err)
10990
}
91+
sort.Strings(typeFiles[i].all)
11092
customPath := filepath.Join(setting.CustomPath, "options", t)
11193
if isDir, err := util.IsDir(customPath); err != nil {
11294
return fmt.Errorf("failed to check custom %s dir: %w", t, err)
11395
} else if isDir {
114-
optionTypeFiles[i].custom, err = util.StatDir(customPath)
96+
typeFiles[i].custom, err = util.StatDir(customPath)
11597
if err != nil {
11698
return fmt.Errorf("failed to list custom %s files: %w", t, err)
11799
}
118100
}
119101
}
120102

121-
Gitignores = mergeCustomFiles(optionTypeFiles[0])
122-
Licenses = mergeCustomFiles(optionTypeFiles[1])
123-
Readmes = mergeCustomFiles(optionTypeFiles[2])
124-
labelTemplatesFiles := mergeCustomLabels(optionTypeFiles[3])
103+
Gitignores = typeFiles[0].all
104+
Licenses = typeFiles[1].all
105+
Readmes = typeFiles[2].all
125106

126107
// Load label templates
108+
labelTemplatesFiles := mergeCustomLabels(typeFiles[3])
127109
for _, templateFile := range labelTemplatesFiles {
128110
labels, err := label.LoadFormatted(templateFile)
129111
if err != nil {

0 commit comments

Comments
 (0)