From ca590175d96fe4a8d0aa98ddd4354741234240b5 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sun, 9 Jun 2024 19:10:42 -0400 Subject: [PATCH] feat: remove ErrorLog field --- cmd/healthcheck/main.go | 10 +++++++++- internal/parsers/healthcheck/parser.go | 2 -- pkg/healthcheck/commit.go | 10 +++++----- pkg/healthcheck/forbidden.go | 3 ++- pkg/healthcheck/meta.go | 3 ++- pkg/healthcheck/nonascii.go | 3 ++- pkg/healthcheck/reposize.go | 6 +++--- pkg/healthcheck/utils.go | 3 +-- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cmd/healthcheck/main.go b/cmd/healthcheck/main.go index a575046..2542a62 100644 --- a/cmd/healthcheck/main.go +++ b/cmd/healthcheck/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "log/slog" "os" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/healthcheck" @@ -28,6 +29,13 @@ func (m *multiStringValue) String() string { return fmt.Sprintf("%v", *m) } +func setupSlog() { + opts := &slog.HandlerOptions{} + handler := slog.NewTextHandler(os.Stderr, opts) + logger := slog.New(handler) + slog.SetDefault(logger) +} + // Generally, err is used for runtime errors, and checkRes is used for the result of the checks. func main() { var info []healthcheck.CheckStage @@ -45,7 +53,7 @@ func main() { parseMultiValueFlag(&metaFile, "meta", "") parseMultiValueFlag(&releaseTags, "releaseTags", "") flag.Parse() - + setupSlog() tmp = healthcheck.RepoSize() info = append(info, tmp) diff --git a/internal/parsers/healthcheck/parser.go b/internal/parsers/healthcheck/parser.go index 046ff18..5655c3d 100644 --- a/internal/parsers/healthcheck/parser.go +++ b/internal/parsers/healthcheck/parser.go @@ -1,11 +1,9 @@ package healthcheck import ( - // "encoding/json" "fmt" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" - // "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/healthcheck" "github.com/criyle/go-judge/envexec" ) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 7c94fb2..1532cc1 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -1,7 +1,7 @@ package healthcheck import ( - "fmt" + "log/slog" "strings" "unicode" @@ -27,7 +27,7 @@ func NonAsciiMsg(root string) (jsonOut CheckStage) { if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = fmt.Errorf("Error openning git repo%w", err) + slog.Error("openning git repo", "err", err) return jsonOut } @@ -35,14 +35,14 @@ func NonAsciiMsg(root string) (jsonOut CheckStage) { if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = fmt.Errorf("Error getting reference%w", err) + slog.Error("getting reference", "err", err) return jsonOut } commits, err := repo.Log(&git.LogOptions{From: ref.Hash()}) if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = fmt.Errorf("Error getting commits%w", err) + slog.Error("getting commits", "err", err) return jsonOut } @@ -54,7 +54,7 @@ func NonAsciiMsg(root string) (jsonOut CheckStage) { if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = fmt.Errorf("Error converting log to string%w", err) + slog.Error("converting log to string", "err", err) return jsonOut } diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index af98747..9333094 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -2,6 +2,7 @@ package healthcheck import ( "fmt" + "log/slog" "os" "path/filepath" "regexp" @@ -67,7 +68,7 @@ func ForbiddenCheck(rootDir string, regexList []string, repo string, droneBranch if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = err + slog.Error("get forbiddens", "error", err) return jsonOut } diff --git a/pkg/healthcheck/meta.go b/pkg/healthcheck/meta.go index e4bcfce..3cad159 100644 --- a/pkg/healthcheck/meta.go +++ b/pkg/healthcheck/meta.go @@ -2,6 +2,7 @@ package healthcheck import ( "fmt" + "log/slog" "os" ) @@ -70,7 +71,7 @@ func MetaCheck(rootDir string, fileList []string) (jsonOut CheckStage) { if err != nil { jsonOut.ExitCode = 1 - jsonOut.ErrorLog = err + slog.Error("get metas", "err", err) return jsonOut } diff --git a/pkg/healthcheck/nonascii.go b/pkg/healthcheck/nonascii.go index eb4d6b7..f2fe523 100644 --- a/pkg/healthcheck/nonascii.go +++ b/pkg/healthcheck/nonascii.go @@ -3,6 +3,7 @@ package healthcheck import ( "bufio" "fmt" + "log/slog" "os" "path/filepath" "strings" @@ -71,7 +72,7 @@ func NonAsciiFiles(root string) (jsonOut CheckStage) { if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = err + slog.Error("get non-ascii", "err", err) return jsonOut } diff --git a/pkg/healthcheck/reposize.go b/pkg/healthcheck/reposize.go index 3c362b2..7103be4 100644 --- a/pkg/healthcheck/reposize.go +++ b/pkg/healthcheck/reposize.go @@ -1,7 +1,7 @@ package healthcheck import ( - "fmt" + "log/slog" "os/exec" "strconv" "strings" @@ -24,7 +24,7 @@ func RepoSize() (jsonOut CheckStage) { if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = fmt.Errorf("Error running git command:%w", err) + slog.Error("running git command:", "err", err) return jsonOut } @@ -38,7 +38,7 @@ func RepoSize() (jsonOut CheckStage) { if err != nil { jsonOut.StdOut += "Failed" jsonOut.ExitCode = 1 - jsonOut.ErrorLog = fmt.Errorf("Error running git command:%w", err) + slog.Error("running git command:", "err", err) return jsonOut } sum += size diff --git a/pkg/healthcheck/utils.go b/pkg/healthcheck/utils.go index 21bb256..449953c 100644 --- a/pkg/healthcheck/utils.go +++ b/pkg/healthcheck/utils.go @@ -8,11 +8,10 @@ import ( // For ExitCode, see https://focs.ji.sjtu.edu.cn/git/TAs/resources/src/branch/drone/dronelib.checks // 1 for unrecoverable error and 0 for succeses type CheckStage struct { - Name string `json:"name of check"` + Name string `json:"name"` StdOut string `json:"stdout"` ExitCode int `json:"exit code"` StdErr string `json:"stderr"` - ErrorLog error `json:"errorLog"` } // addExt appends the specified extension to each file name in the given fileList.