diff options
author | Martin Fischer <martin@push-f.com> | 2025-04-06 21:37:32 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2025-04-06 21:40:36 +0200 |
commit | 455778d2db961c36c54550ed3fc02a23aeaac34b (patch) | |
tree | f4153bf0c65b956ac5e72f2e4949e014fa4a7056 | |
parent | 5ad1feb9925cb4c4b25913aa88f864a062c58ffa (diff) |
refactor: use slog from standard library
-rw-r--r-- | build.go | 55 | ||||
-rw-r--r-- | default.nix | 2 | ||||
-rw-r--r-- | go.mod | 7 | ||||
-rw-r--r-- | go.sum | 15 |
4 files changed, 40 insertions, 39 deletions
@@ -7,6 +7,7 @@ import ( "flag" "fmt" "html/template" + "log/slog" "maps" "os" "os/exec" @@ -16,7 +17,6 @@ import ( "strings" "github.com/BurntSushi/toml" - log "github.com/sirupsen/logrus" ) const caniuseRepoURL = "https://github.com/jplatte/caniuse.rs" @@ -36,7 +36,10 @@ var style []byte //go:embed find-lib-feats.sh var findLibFeatsShellCommand string +var log *slog.Logger + func main() { + log = slog.New(slog.NewTextHandler(os.Stderr, nil)) caniuseRepo := "caniuse.rs" rustRepo := "rust" outDir := "out" @@ -65,7 +68,8 @@ func main() { cmd.Stderr = &stderr libFeaturesText, err := cmd.Output() if err != nil { - log.WithField("stderr", stderr.String()).Fatalf("failed to run shell command to find library features: %s", err) + log.Error("failed to run shell command to find library features", Error(err), "stderr", stderr.String()) + os.Exit(1) } libFeatureFlags := strings.Split(string(libFeaturesText), "\n") @@ -73,11 +77,13 @@ func main() { versionsPath := fmt.Sprintf(caniuseRepo + "/data/versions.toml") _, err = toml.DecodeFile(versionsPath, &versionInfos) if err != nil { - log.Fatalf("error parsing %s: %s", versionsPath, err) + log.Error("failed to parse versions.toml", Error(err), "path", versionsPath) + os.Exit(1) } if len(versionInfos) == 0 { - log.Fatal("found no versions in versions.toml") + log.Error("found no versions in versions.toml") + os.Exit(1) } versions := make([]Version, 0) @@ -113,7 +119,8 @@ func main() { } if featureCount == 0 { - log.Fatal("found no features") + log.Error("found no features") + os.Exit(1) } tmpl := template.New("template.html.tmpl").Funcs(template.FuncMap{ @@ -129,17 +136,20 @@ func main() { tmpl, err = tmpl.Parse(templateText) if err != nil { - log.Fatalf("error parsing template: %s", err) + log.Error("error parsing template", Error(err)) + os.Exit(1) } err = os.MkdirAll(outDir, 0o755) if err != nil { - log.Fatalf("error creating directory: %s", err) + log.Error("error creating output directory", Error(err)) + os.Exit(1) } outputFile, err := os.Create(outDir + "/index.html") if err != nil { - log.Fatalf("error creating index.html: %s", err) + log.Error("error creating index.html", Error(err)) + os.Exit(1) } defer outputFile.Close() @@ -150,29 +160,34 @@ func main() { }, ) if err != nil { - log.Fatalf("error executing template: %s", err) + log.Error("error executing template", Error(err)) + os.Exit(1) } outputFile, err = os.Create(outDir + "/data.json") if err != nil { - log.Fatalf("error creating data.json: %s", err) + log.Error("error creating data.json", Error(err)) + os.Exit(1) } defer outputFile.Close() encoder := json.NewEncoder(outputFile) err = encoder.Encode(versions) if err != nil { - log.Fatalf("error encoding JSON: %s", err) + log.Error("error encoding JSON", Error(err)) + os.Exit(1) } err = os.WriteFile(outDir+"/script.js", script, 0o644) if err != nil { - log.Fatalf("error creating script.js: %s", err) + log.Error("error creating script.js", Error(err)) + os.Exit(1) } err = os.WriteFile(outDir+"/style.css", style, 0o644) if err != nil { - log.Fatalf("error creating style.css: %s", err) + log.Error("error creating style.css", Error(err)) + os.Exit(1) } } @@ -207,16 +222,19 @@ func getFeatures(caniuseRepo string, version string) map[string]Feature { if os.IsNotExist(err) { return nil } - log.Fatal(err) + log.Error("failed to read directory", "path", dirPath, Error(err)) + os.Exit(1) } features := make(map[string]Feature) for _, entry := range entries { var feature Feature - _, err := toml.DecodeFile(dirPath+"/"+entry.Name(), &feature) + path := dirPath + "/" + entry.Name() + _, err := toml.DecodeFile(path, &feature) if err != nil { - log.Fatalf("Error parsing %s: %s\n", dirPath+entry.Name(), err) + log.Error("error decoding TOML file", "path", path, Error(err)) + os.Exit(1) } if feature.TrackingIssueId != nil { feature.URL = fmt.Sprintf("https://github.com/rust-lang/rust/issues/%d", *feature.TrackingIssueId) @@ -276,7 +294,8 @@ func runCommand(name string, args ...string) { var stderr bytes.Buffer cmd.Stderr = &stderr if err := cmd.Run(); err != nil { - log.WithField("stderr", stderr.String()).Fatalf("command '%s %v' failed: %v", name, args, err) + log.Error("failed to run command", "command", cmd.String(), Error(err), "stderr", stderr.String()) + os.Exit(1) } } @@ -284,3 +303,5 @@ func runCommand(name string, args ...string) { func nilAsEmptyArray[T any](slice []T) []T { return append([]T{}, slice...) } + +func Error(value error) slog.Attr { return slog.String("error", value.Error()) } diff --git a/default.nix b/default.nix index 01d0210..cd08afa 100644 --- a/default.nix +++ b/default.nix @@ -4,7 +4,7 @@ pkgs.buildGoModule rec { pname = "rust-features"; version = "git"; src = ./.; - vendorHash = "sha256-WaDp7wKYHeV8hAAwPGmp1Dv8Hkwzl+Mzr8yzuUP2TgQ="; + vendorHash = "sha256-CVycV7wxo7nOHm7qjZKfJrIkNcIApUNzN1mSIIwQN0g="; nativeBuildInputs = [pkgs.makeWrapper]; postInstall = '' @@ -2,9 +2,4 @@ module push-f.com/rust-features go 1.23.5 -require ( - github.com/BurntSushi/toml v1.5.0 - github.com/sirupsen/logrus v1.9.3 -) - -require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +require github.com/BurntSushi/toml v1.5.0 @@ -1,17 +1,2 @@ github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |