JOJ3/pkg/healthcheck/reposize.go

57 lines
1.3 KiB
Go

package healthcheck
import (
"log/slog"
"os/exec"
"strconv"
"strings"
)
// RepoSize checks the size of the repository to determine if it is oversized.
// It executes the 'git count-objects -v' command to obtain the size information,
func RepoSize() (jsonOut CheckStage) {
jsonOut = CheckStage{
Name: "RepoSize",
StdOut: "Checking repository size: ",
ExitCode: 0,
StdErr: "",
}
// TODO: reimplement here when go-git is available
// https://github.com/go-git/go-git/blob/master/COMPATIBILITY.md
cmd := exec.Command("git", "count-objects", "-v")
output, err := cmd.CombinedOutput()
if err != nil {
jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1
slog.Error("running git command:", "err", err)
return jsonOut
}
lines := strings.Split(string(output), "\n")
var sum int
for _, line := range lines {
if strings.Contains(line, "size") {
fields := strings.Fields(line)
sizeStr := fields[1]
size, err := strconv.Atoi(sizeStr)
if err != nil {
jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1
slog.Error("running git command:", "err", err)
return jsonOut
}
sum += size
}
}
if sum <= 2048 {
jsonOut.StdOut += "OK"
return jsonOut
}
jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 100
jsonOut.StdErr = "repository larger than 2MB, please clean up or contact the teaching team."
return jsonOut
}