feat(healthcheck): cleaner return tips
This commit is contained in:
parent
e65fd1e675
commit
03bfb41ecf
|
@ -1 +1 @@
|
||||||
Subproject commit a3e3c533c8f986168a6b1a8621ce4034fd4eda04
|
Subproject commit 13fe8890f87cd68125c48c19e1f9d4a21bc21383
|
|
@ -4,53 +4,59 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getMetas retrieves a list of metadata files that are expected to exist in the specified root directory.
|
// getMetas retrieves a list of metadata files that are expected to exist in the specified root directory.
|
||||||
// It checks for the existence of each file in the fileList and provides instructions if any file is missing.
|
// It checks for the existence of each file in the fileList and provides instructions if any file is missing.
|
||||||
func getMetas(rootDir string, fileList []string) ([]string, string, error) {
|
func getMetas(rootDir string, fileList []string) ([]string, string, error) {
|
||||||
addExt(fileList, "\\.*")
|
var regexList []*regexp.Regexp
|
||||||
regexList, err := getRegex(fileList)
|
for _, file := range fileList {
|
||||||
var unmatchedList []string
|
pattern := "(?i)" + file
|
||||||
|
if !strings.Contains(pattern, "\\.") {
|
||||||
if err != nil {
|
pattern += "(\\.[^\\.]*)?"
|
||||||
return nil, "", err
|
}
|
||||||
|
regex, err := regexp.Compile(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("error compiling regex:%w", err)
|
||||||
|
}
|
||||||
|
regexList = append(regexList, regex)
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := os.ReadDir(rootDir)
|
files, err := os.ReadDir(rootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", fmt.Errorf("error reading directory: %w", err)
|
return nil, "", fmt.Errorf("error reading directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
matched := false
|
matched := make([]bool, len(fileList))
|
||||||
umatchedRes := ""
|
|
||||||
|
|
||||||
// TODO: it seems that there is no good find substitution now
|
|
||||||
// modify current code if exist a better solution
|
|
||||||
for i, regex := range regexList {
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
fileName := file.Name()
|
||||||
|
|
||||||
if regex.MatchString(file.Name()) {
|
for i, regex := range regexList {
|
||||||
matched = true
|
if regex.MatchString(fileName) {
|
||||||
break
|
matched[i] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !matched {
|
}
|
||||||
|
|
||||||
|
// Process unmatched patterns
|
||||||
|
var unmatchedList []string
|
||||||
|
var umatchedRes string
|
||||||
|
|
||||||
|
for i, wasFound := range matched {
|
||||||
|
if !wasFound {
|
||||||
unmatchedList = append(unmatchedList, fileList[i])
|
unmatchedList = append(unmatchedList, fileList[i])
|
||||||
str := fmt.Sprint("\tno ", fileList[i], " file found")
|
str := fmt.Sprintf("%d. No %s file found", i+1, fileList[i])
|
||||||
switch fileList[i] {
|
if strings.Index(strings.ToLower(fileList[i]), "readme") == 0 {
|
||||||
case "readme\\.*":
|
str += ", please refer to https://www.makeareadme.com/ for more information."
|
||||||
str += ", please refer to https://www.makeareadme.com/ for more information"
|
} else if strings.Index(strings.ToLower(fileList[i]), "changelog") == 0 {
|
||||||
case "changelog\\.*":
|
str += ", please refer to https://keepachangelog.com/en/1.1.0/ for more information."
|
||||||
str += ", please refer to https://keepachangelog.com/en/1.1.0/ for more information"
|
|
||||||
default:
|
|
||||||
str += ""
|
|
||||||
}
|
}
|
||||||
str += "\n"
|
str += "\n"
|
||||||
|
|
||||||
umatchedRes += str
|
umatchedRes += str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +73,7 @@ func MetaCheck(rootDir string, fileList []string) error {
|
||||||
return fmt.Errorf("error getting metas: %w", err)
|
return fmt.Errorf("error getting metas: %w", err)
|
||||||
}
|
}
|
||||||
if len(unmatchedList) != 0 {
|
if len(unmatchedList) != 0 {
|
||||||
return fmt.Errorf("%d important project files missing\n"+umatchedRes, len(unmatchedList))
|
return fmt.Errorf("%d important project file(s) missing:\n"+umatchedRes, len(unmatchedList))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
package healthcheck
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// addExt appends the specified extension to each file name in the given fileList.
|
|
||||||
// It modifies the original fileList in place.
|
|
||||||
func addExt(fileList []string, ext string) {
|
|
||||||
for i, file := range fileList {
|
|
||||||
fileList[i] = file + ext
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getRegex compiles each regex pattern in the fileList into a []*regexp.Regexp slice.
|
|
||||||
// It returns a slice containing compiled regular expressions.
|
|
||||||
func getRegex(fileList []string) ([]*regexp.Regexp, error) {
|
|
||||||
var regexList []*regexp.Regexp
|
|
||||||
for _, pattern := range fileList {
|
|
||||||
regex, err := regexp.Compile("(?i)" + pattern)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error compiling regex:%w", err)
|
|
||||||
}
|
|
||||||
regexList = append(regexList, regex)
|
|
||||||
}
|
|
||||||
|
|
||||||
return regexList, nil
|
|
||||||
}
|
|
|
@ -32,7 +32,7 @@ func getChecksum(filePath string) (string, error) {
|
||||||
func checkFileChecksum(filePath, expectedChecksum string) (bool, error) {
|
func checkFileChecksum(filePath, expectedChecksum string) (bool, error) {
|
||||||
actualChecksum, err := getChecksum(filePath)
|
actualChecksum, err := getChecksum(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("Error reading file %s: %v", filePath, err)
|
return false, fmt.Errorf("error reading file %s: %v", filePath, err)
|
||||||
}
|
}
|
||||||
if actualChecksum != expectedChecksum {
|
if actualChecksum != expectedChecksum {
|
||||||
return true, nil
|
return true, nil
|
||||||
|
@ -48,7 +48,7 @@ func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList stri
|
||||||
checkSums := strings.Split(checkFileSumList, ",")
|
checkSums := strings.Split(checkFileSumList, ",")
|
||||||
// Check if the number of files matches the number of checksums
|
// Check if the number of files matches the number of checksums
|
||||||
if len(fileNames) != len(checkSums) {
|
if len(fileNames) != len(checkSums) {
|
||||||
return fmt.Errorf("Error: The number of files and checksums do not match.")
|
return fmt.Errorf("error: The number of files and checksums do not match.")
|
||||||
}
|
}
|
||||||
// Check each file's checksum
|
// Check each file's checksum
|
||||||
alteredFiles := []string{}
|
alteredFiles := []string{}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user