Commit 5a7edb5
Eric Bower
·
2024-03-19 16:11:44 -0400 EDT
parent 78eb71e
refactor: use slog
M
go.mod
+1,
-2
| ... | ... | @@ -6,12 +6,11 @@ require ( | |
| 6 | 6 | github.com/alecthomas/chroma v0.10.0 | |
| 7 | 7 | github.com/dustin/go-humanize v1.0.0 | |
| 8 | 8 | github.com/gogs/git-module v1.6.0 | |
| 9 | - | go.uber.org/zap v1.25.0 | |
| 10 | 9 | ) | |
| 11 | 10 | ||
| 12 | 11 | require ( | |
| 13 | 12 | github.com/dlclark/regexp2 v1.7.0 // indirect | |
| 14 | 13 | 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 | |
| 16 | 15 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect | |
| 17 | 16 | ) |
M
go.sum
+6,
-6
| ... | ... | @@ -1,6 +1,5 @@ | |
| 1 | 1 | github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= | |
| 2 | 2 | github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= | |
| 3 | - | github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= | |
| 4 | 3 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | |
| 5 | 4 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | |
| 6 | 5 | 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 | |
| 16 | 15 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | |
| 17 | 16 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | |
| 18 | 17 | 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= | |
| 19 | 20 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | |
| 20 | 21 | 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= | |
| 21 | 24 | 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= | |
| 27 | 26 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | |
| 28 | 27 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= | |
| 29 | 28 | 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 | |
| 31 | 30 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | |
| 32 | 31 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | |
| 33 | 32 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | |
| 33 | + | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
M
main.go
+26,
-27
| ... | ... | @@ -14,6 +14,7 @@ import ( | |
| 14 | 14 | "strings" | |
| 15 | 15 | "sync" | |
| 16 | 16 | "unicode/utf8" | |
| 17 | + | "log/slog" | |
| 17 | 18 | ||
| 18 | 19 | "github.com/alecthomas/chroma" | |
| 19 | 20 | formatterHtml "github.com/alecthomas/chroma/formatters/html" |
| ... | ... | @@ -21,7 +22,6 @@ import ( | |
| 21 | 22 | "github.com/alecthomas/chroma/styles" | |
| 22 | 23 | "github.com/dustin/go-humanize" | |
| 23 | 24 | git "github.com/gogs/git-module" | |
| 24 | - | "go.uber.org/zap" | |
| 25 | 25 | ) | |
| 26 | 26 | ||
| 27 | 27 | //go:embed html/*.tmpl static/* |
| ... | ... | @@ -60,7 +60,7 @@ type Config struct { | |
| 60 | 60 | // pretty name for the repo | |
| 61 | 61 | RepoName string | |
| 62 | 62 | // logger | |
| 63 | - | Logger *zap.SugaredLogger | |
| 63 | + | Logger *slog.Logger | |
| 64 | 64 | // chroma style | |
| 65 | 65 | Theme *chroma.Style | |
| 66 | 66 | } |
| ... | ... | @@ -320,7 +320,7 @@ func (c *Config) writeHtml(writeData *WriteData) { | |
| 320 | 320 | bail(err) | |
| 321 | 321 | ||
| 322 | 322 | fp := filepath.Join(dir, writeData.Filename) | |
| 323 | - | c.Logger.Infof("writing (%s)", fp) | |
| 323 | + | c.Logger.Info("writing", "filepath", fp) | |
| 324 | 324 | ||
| 325 | 325 | w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) | |
| 326 | 326 | bail(err) |
| ... | ... | @@ -342,7 +342,7 @@ func (c *Config) copyStatic(dir string) error { | |
| 342 | 342 | w, err := efs.ReadFile(infp) | |
| 343 | 343 | bail(err) | |
| 344 | 344 | fp := filepath.Join(c.Outdir, e.Name()) | |
| 345 | - | c.Logger.Infof("writing (%s)", fp) | |
| 345 | + | c.Logger.Info("writing", "filepath", fp) | |
| 346 | 346 | os.WriteFile(fp, w, 0755) | |
| 347 | 347 | } | |
| 348 | 348 |
| ... | ... | @@ -350,7 +350,7 @@ func (c *Config) copyStatic(dir string) error { | |
| 350 | 350 | } | |
| 351 | 351 | ||
| 352 | 352 | 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) | |
| 354 | 354 | c.writeHtml(&WriteData{ | |
| 355 | 355 | Filename: "index.html", | |
| 356 | 356 | Template: "html/summary.page.tmpl", |
| ... | ... | @@ -362,7 +362,7 @@ func (c *Config) writeRootSummary(data *PageData, readme template.HTML) { | |
| 362 | 362 | } | |
| 363 | 363 | ||
| 364 | 364 | 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) | |
| 366 | 366 | c.writeHtml(&WriteData{ | |
| 367 | 367 | Filename: "index.html", | |
| 368 | 368 | Subdir: tree.Path, |
| ... | ... | @@ -375,7 +375,7 @@ func (c *Config) writeTree(data *PageData, tree *TreeRoot) { | |
| 375 | 375 | } | |
| 376 | 376 | ||
| 377 | 377 | 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()) | |
| 379 | 379 | c.writeHtml(&WriteData{ | |
| 380 | 380 | Filename: "index.html", | |
| 381 | 381 | Subdir: getLogBaseDir(data.RevData), |
| ... | ... | @@ -389,7 +389,7 @@ func (c *Config) writeLog(data *PageData, logs []*CommitData) { | |
| 389 | 389 | } | |
| 390 | 390 | ||
| 391 | 391 | 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) | |
| 393 | 393 | c.writeHtml(&WriteData{ | |
| 394 | 394 | Filename: "refs.html", | |
| 395 | 395 | Template: "html/refs.page.tmpl", |
| ... | ... | @@ -444,7 +444,7 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit * | |
| 444 | 444 | c.Mutex.RUnlock() | |
| 445 | 445 | ||
| 446 | 446 | 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)) | |
| 448 | 448 | return | |
| 449 | 449 | } else { | |
| 450 | 450 | c.Mutex.Lock() |
| ... | ... | @@ -576,7 +576,7 @@ func getShortID(id string) string { | |
| 576 | 576 | } | |
| 577 | 577 | ||
| 578 | 578 | func (c *Config) writeRepo() *BranchOutput { | |
| 579 | - | c.Logger.Infof("Writing repo (%s)", c.RepoPath) | |
| 579 | + | c.Logger.Info("writing repo", "repoPath", c.RepoPath) | |
| 580 | 580 | repo, err := git.Open(c.RepoPath) | |
| 581 | 581 | bail(err) | |
| 582 | 582 |
| ... | ... | @@ -656,7 +656,7 @@ func (c *Config) writeRepo() *BranchOutput { | |
| 656 | 656 | }) | |
| 657 | 657 | ||
| 658 | 658 | for _, revData := range revs { | |
| 659 | - | c.Logger.Infof("Writing revision (%s)", revData.Name()) | |
| 659 | + | c.Logger.Info("writing revision", "revision", revData.Name()) | |
| 660 | 660 | data := &PageData{ | |
| 661 | 661 | Repo: c, | |
| 662 | 662 | RevData: revData, |
| ... | ... | @@ -858,6 +858,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) { | |
| 858 | 858 | return true | |
| 859 | 859 | } | |
| 860 | 860 | ||
| 861 | + | if !treeEntries[i].IsDir && treeEntries[j].IsDir { | |
| 862 | + | return false | |
| 863 | + | } | |
| 864 | + | ||
| 861 | 865 | return nameI < nameJ | |
| 862 | 866 | }) | |
| 863 | 867 |
| ... | ... | @@ -883,10 +887,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) { | |
| 883 | 887 | } | |
| 884 | 888 | ||
| 885 | 889 | 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(), | |
| 890 | 894 | ) | |
| 891 | 895 | ||
| 892 | 896 | output := &BranchOutput{} |
| ... | ... | @@ -998,10 +1002,10 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs [] | |
| 998 | 1002 | ||
| 999 | 1003 | wg.Wait() | |
| 1000 | 1004 | ||
| 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(), | |
| 1005 | 1009 | ) | |
| 1006 | 1010 | ||
| 1007 | 1011 | output.Readme = readme |
| ... | ... | @@ -1029,12 +1033,7 @@ func main() { | |
| 1029 | 1033 | ||
| 1030 | 1034 | theme := styles.Get(*themeFlag) | |
| 1031 | 1035 | ||
| 1032 | - | lg, err := zap.NewProduction() | |
| 1033 | - | if err != nil { | |
| 1034 | - | bail(err) | |
| 1035 | - | } | |
| 1036 | - | ||
| 1037 | - | logger := lg.Sugar() | |
| 1036 | + | logger := slog.Default() | |
| 1038 | 1037 | ||
| 1039 | 1038 | label := repoName(repoPath) | |
| 1040 | 1039 | if *labelFlag != "" { |
| ... | ... | @@ -1060,7 +1059,7 @@ func main() { | |
| 1060 | 1059 | MaxCommits: *maxCommitsFlag, | |
| 1061 | 1060 | HideTreeLastCommit: *hideTreeLastCommitFlag, | |
| 1062 | 1061 | } | |
| 1063 | - | config.Logger.Infof("%+v", config) | |
| 1062 | + | config.Logger.Info("config", "config", config) | |
| 1064 | 1063 | ||
| 1065 | 1064 | if len(revs) == 0 { | |
| 1066 | 1065 | bail(fmt.Errorf("you must provide --revs")) |