From 03bfb41ecfe05572900421c77a79969c83ed4e99 Mon Sep 17 00:00:00 2001
From: Boming Zhang <bomingzh@sjtu.edu.cn>
Date: Fri, 1 Nov 2024 07:43:33 -0400
Subject: [PATCH] feat(healthcheck): cleaner return tips

---
 examples/clangtidy/sillycode |  2 +-
 pkg/healthcheck/meta.go      | 66 ++++++++++++++++++++----------------
 pkg/healthcheck/utils.go     | 29 ----------------
 pkg/healthcheck/verify.go    |  4 +--
 4 files changed, 39 insertions(+), 62 deletions(-)
 delete mode 100644 pkg/healthcheck/utils.go

diff --git a/examples/clangtidy/sillycode b/examples/clangtidy/sillycode
index a3e3c53..13fe889 160000
--- a/examples/clangtidy/sillycode
+++ b/examples/clangtidy/sillycode
@@ -1 +1 @@
-Subproject commit a3e3c533c8f986168a6b1a8621ce4034fd4eda04
+Subproject commit 13fe8890f87cd68125c48c19e1f9d4a21bc21383
diff --git a/pkg/healthcheck/meta.go b/pkg/healthcheck/meta.go
index a78da6b..6e3696a 100644
--- a/pkg/healthcheck/meta.go
+++ b/pkg/healthcheck/meta.go
@@ -4,53 +4,59 @@ import (
 	"fmt"
 	"log/slog"
 	"os"
+	"regexp"
+	"strings"
 )
 
 // 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.
 func getMetas(rootDir string, fileList []string) ([]string, string, error) {
-	addExt(fileList, "\\.*")
-	regexList, err := getRegex(fileList)
-	var unmatchedList []string
-
-	if err != nil {
-		return nil, "", err
+	var regexList []*regexp.Regexp
+	for _, file := range fileList {
+		pattern := "(?i)" + file
+		if !strings.Contains(pattern, "\\.") {
+			pattern += "(\\.[^\\.]*)?"
+		}
+		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)
 	if err != nil {
 		return nil, "", fmt.Errorf("error reading directory: %w", err)
 	}
 
-	matched := false
-	umatchedRes := ""
+	matched := make([]bool, len(fileList))
 
-	// 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 {
-			if file.IsDir() {
-				continue
-			}
+	for _, file := range files {
+		if file.IsDir() {
+			continue
+		}
+		fileName := file.Name()
 
-			if regex.MatchString(file.Name()) {
-				matched = true
-				break
+		for i, regex := range regexList {
+			if regex.MatchString(fileName) {
+				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])
-			str := fmt.Sprint("\tno ", fileList[i], " file found")
-			switch fileList[i] {
-			case "readme\\.*":
-				str += ", please refer to https://www.makeareadme.com/ for more information"
-			case "changelog\\.*":
-				str += ", please refer to https://keepachangelog.com/en/1.1.0/ for more information"
-			default:
-				str += ""
+			str := fmt.Sprintf("%d. No %s file found", i+1, fileList[i])
+			if strings.Index(strings.ToLower(fileList[i]), "readme") == 0 {
+				str += ", please refer to https://www.makeareadme.com/ for more information."
+			} else if strings.Index(strings.ToLower(fileList[i]), "changelog") == 0 {
+				str += ", please refer to https://keepachangelog.com/en/1.1.0/ for more information."
 			}
 			str += "\n"
-
 			umatchedRes += str
 		}
 	}
@@ -67,7 +73,7 @@ func MetaCheck(rootDir string, fileList []string) error {
 		return fmt.Errorf("error getting metas: %w", err)
 	}
 	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
 }
diff --git a/pkg/healthcheck/utils.go b/pkg/healthcheck/utils.go
deleted file mode 100644
index 8939f22..0000000
--- a/pkg/healthcheck/utils.go
+++ /dev/null
@@ -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
-}
diff --git a/pkg/healthcheck/verify.go b/pkg/healthcheck/verify.go
index a26e73f..d9bb87f 100644
--- a/pkg/healthcheck/verify.go
+++ b/pkg/healthcheck/verify.go
@@ -32,7 +32,7 @@ func getChecksum(filePath string) (string, error) {
 func checkFileChecksum(filePath, expectedChecksum string) (bool, error) {
 	actualChecksum, err := getChecksum(filePath)
 	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 {
 		return true, nil
@@ -48,7 +48,7 @@ func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList stri
 	checkSums := strings.Split(checkFileSumList, ",")
 	// Check if the number of files matches the number of 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
 	alteredFiles := []string{}