74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package healthcheck
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// fileExists checks if a file exists at the specified path.
|
|
func fileExists(filePath string) bool {
|
|
_, err := os.Stat(filePath)
|
|
return !os.IsNotExist(err)
|
|
}
|
|
|
|
// filesMatch checks if two files are identical.
|
|
func filesMatch(file1, file2 string) (bool, error) {
|
|
f1, err := os.Open(file1)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer f1.Close()
|
|
|
|
f2, err := os.Open(file2)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer f2.Close()
|
|
|
|
scanner1 := bufio.NewScanner(f1)
|
|
scanner2 := bufio.NewScanner(f2)
|
|
|
|
for scanner1.Scan() && scanner2.Scan() {
|
|
line1 := scanner1.Text()
|
|
line2 := scanner2.Text()
|
|
|
|
if line1 != line2 {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
if scanner1.Scan() || scanner2.Scan() {
|
|
// One file has more lines than the other
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// VerifyDirectory checks if the contents of two directories are identical.
|
|
func VerifyDirectory(rootDir, compareDir string) error {
|
|
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return fmt.Errorf("error walking directory: %w", err)
|
|
}
|
|
if !info.IsDir() {
|
|
relPath, _ := filepath.Rel(rootDir, path)
|
|
file2 := filepath.Join(compareDir, relPath)
|
|
if !fileExists(file2) {
|
|
return fmt.Errorf("File %s in %s is missing in %s. Please immediately revert your changes!\n", relPath, rootDir, compareDir)
|
|
}
|
|
match, err := filesMatch(path, file2)
|
|
if err != nil {
|
|
return fmt.Errorf("error matching files: %w", err)
|
|
}
|
|
if !match {
|
|
return fmt.Errorf("File %s in %s is not identical to %s. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.\n", relPath, rootDir, compareDir)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
return err
|
|
}
|