Commit 5a7edb5

Eric Bower  ·  2024-03-19 16:11:44 -0400 EDT
parent 78eb71e
refactor: use slog
3 files changed,  +33, -35
M go.mod
M go.sum
M go.mod
+1, -2
......@@ -6,12 +6,11 @@ require (
66 github.com/alecthomas/chroma v0.10.0
77 github.com/dustin/go-humanize v1.0.0
88 github.com/gogs/git-module v1.6.0
9- go.uber.org/zap v1.25.0
109 )
1110
1211 require (
1312 github.com/dlclark/regexp2 v1.7.0 // indirect
1413 github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
15- go.uber.org/multierr v1.10.0 // indirect
14+ github.com/stretchr/testify v1.8.1 // indirect
1615 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
1716 )
M go.sum
+6, -6
......@@ -1,6 +1,5 @@
11 github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
22 github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
3-github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
43 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
65 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
......@@ -16,14 +15,14 @@ github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75/go.mod h1:76rf
1615 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1716 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1817 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
19+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
1920 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
2021 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
22+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2124 github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
22-go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
23-go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
24-go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
25-go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
26-go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
25+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2726 golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2827 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
2928 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
......@@ -31,3 +30,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
3130 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3231 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3332 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
33+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+26, -27
......@@ -14,6 +14,7 @@ import (
1414 "strings"
1515 "sync"
1616 "unicode/utf8"
17+ "log/slog"
1718
1819 "github.com/alecthomas/chroma"
1920 formatterHtml "github.com/alecthomas/chroma/formatters/html"
......@@ -21,7 +22,6 @@ import (
2122 "github.com/alecthomas/chroma/styles"
2223 "github.com/dustin/go-humanize"
2324 git "github.com/gogs/git-module"
24- "go.uber.org/zap"
2525 )
2626
2727 //go:embed html/*.tmpl static/*
......@@ -60,7 +60,7 @@ type Config struct {
6060 // pretty name for the repo
6161 RepoName string
6262 // logger
63- Logger *zap.SugaredLogger
63+ Logger *slog.Logger
6464 // chroma style
6565 Theme *chroma.Style
6666 }
......@@ -320,7 +320,7 @@ func (c *Config) writeHtml(writeData *WriteData) {
320320 bail(err)
321321
322322 fp := filepath.Join(dir, writeData.Filename)
323- c.Logger.Infof("writing (%s)", fp)
323+ c.Logger.Info("writing", "filepath", fp)
324324
325325 w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
326326 bail(err)
......@@ -342,7 +342,7 @@ func (c *Config) copyStatic(dir string) error {
342342 w, err := efs.ReadFile(infp)
343343 bail(err)
344344 fp := filepath.Join(c.Outdir, e.Name())
345- c.Logger.Infof("writing (%s)", fp)
345+ c.Logger.Info("writing", "filepath", fp)
346346 os.WriteFile(fp, w, 0755)
347347 }
348348
......@@ -350,7 +350,7 @@ func (c *Config) copyStatic(dir string) error {
350350 }
351351
352352 func (c *Config) writeRootSummary(data *PageData, readme template.HTML) {
353- c.Logger.Infof("writing root html (%s)", c.RepoPath)
353+ c.Logger.Info("writing root html", "repoPath", c.RepoPath)
354354 c.writeHtml(&WriteData{
355355 Filename: "index.html",
356356 Template: "html/summary.page.tmpl",
......@@ -362,7 +362,7 @@ func (c *Config) writeRootSummary(data *PageData, readme template.HTML) {
362362 }
363363
364364 func (c *Config) writeTree(data *PageData, tree *TreeRoot) {
365- c.Logger.Infof("writing tree (%s)", tree.Path)
365+ c.Logger.Info("writing tree", "treePath", tree.Path)
366366 c.writeHtml(&WriteData{
367367 Filename: "index.html",
368368 Subdir: tree.Path,
......@@ -375,7 +375,7 @@ func (c *Config) writeTree(data *PageData, tree *TreeRoot) {
375375 }
376376
377377 func (c *Config) writeLog(data *PageData, logs []*CommitData) {
378- c.Logger.Infof("writing log file (%s)", data.RevData.Name())
378+ c.Logger.Info("writing log file", "revision", data.RevData.Name())
379379 c.writeHtml(&WriteData{
380380 Filename: "index.html",
381381 Subdir: getLogBaseDir(data.RevData),
......@@ -389,7 +389,7 @@ func (c *Config) writeLog(data *PageData, logs []*CommitData) {
389389 }
390390
391391 func (c *Config) writeRefs(data *PageData, refs []*RefInfo) {
392- c.Logger.Infof("writing refs (%s)", c.RepoPath)
392+ c.Logger.Info("writing refs", "repoPath", c.RepoPath)
393393 c.writeHtml(&WriteData{
394394 Filename: "refs.html",
395395 Template: "html/refs.page.tmpl",
......@@ -444,7 +444,7 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit *
444444 c.Mutex.RUnlock()
445445
446446 if hasCommit {
447- c.Logger.Infof("(%s) commit file already generated, skipping", getShortID(commitID))
447+ c.Logger.Info("commit file already generated, skipping", "commitID", getShortID(commitID))
448448 return
449449 } else {
450450 c.Mutex.Lock()
......@@ -576,7 +576,7 @@ func getShortID(id string) string {
576576 }
577577
578578 func (c *Config) writeRepo() *BranchOutput {
579- c.Logger.Infof("Writing repo (%s)", c.RepoPath)
579+ c.Logger.Info("writing repo", "repoPath", c.RepoPath)
580580 repo, err := git.Open(c.RepoPath)
581581 bail(err)
582582
......@@ -656,7 +656,7 @@ func (c *Config) writeRepo() *BranchOutput {
656656 })
657657
658658 for _, revData := range revs {
659- c.Logger.Infof("Writing revision (%s)", revData.Name())
659+ c.Logger.Info("writing revision", "revision", revData.Name())
660660 data := &PageData{
661661 Repo: c,
662662 RevData: revData,
......@@ -858,6 +858,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) {
858858 return true
859859 }
860860
861+ if !treeEntries[i].IsDir && treeEntries[j].IsDir {
862+ return false
863+ }
864+
861865 return nameI < nameJ
862866 })
863867
......@@ -883,10 +887,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) {
883887 }
884888
885889 func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []*RefInfo) *BranchOutput {
886- c.Logger.Infof(
887- "compiling (%s) revision (%s)",
888- c.RepoName,
889- pageData.RevData.Name(),
890+ c.Logger.Info(
891+ "compiling revision",
892+ "repoName", c.RepoName,
893+ "revision", pageData.RevData.Name(),
890894 )
891895
892896 output := &BranchOutput{}
......@@ -998,10 +1002,10 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
9981002
9991003 wg.Wait()
10001004
1001- c.Logger.Infof(
1002- "compilation complete (%s) branch (%s)",
1003- c.RepoName,
1004- pageData.RevData.Name(),
1005+ c.Logger.Info(
1006+ "compilation complete branch",
1007+ "repoName", c.RepoName,
1008+ "revision", pageData.RevData.Name(),
10051009 )
10061010
10071011 output.Readme = readme
......@@ -1029,12 +1033,7 @@ func main() {
10291033
10301034 theme := styles.Get(*themeFlag)
10311035
1032- lg, err := zap.NewProduction()
1033- if err != nil {
1034- bail(err)
1035- }
1036-
1037- logger := lg.Sugar()
1036+ logger := slog.Default()
10381037
10391038 label := repoName(repoPath)
10401039 if *labelFlag != "" {
......@@ -1060,7 +1059,7 @@ func main() {
10601059 MaxCommits: *maxCommitsFlag,
10611060 HideTreeLastCommit: *hideTreeLastCommitFlag,
10621061 }
1063- config.Logger.Infof("%+v", config)
1062+ config.Logger.Info("config", "config", config)
10641063
10651064 if len(revs) == 0 {
10661065 bail(fmt.Errorf("you must provide --revs"))
......@@ -1070,5 +1069,5 @@ func main() {
10701069 config.copyStatic("static")
10711070
10721071 url := filepath.Join("/", "index.html")
1073- config.Logger.Info(url)
1072+ config.Logger.Info("root url", "url", url)
10741073 }