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 is missing. Please immediately revert your changes!\n", filepath.Join(rootDir, relPath)) } match, err := filesMatch(path, file2) if err != nil { return fmt.Errorf("error matching files: %w", err) } if !match { return fmt.Errorf("File %s is altered. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.\n", filepath.Join(rootDir, relPath)) } } return nil }) return err }