From 5a7edb53f5cec518977938cd66fa2a3da48d2be4 Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Tue, 19 Mar 2024 16:11:44 -0400 Subject: [PATCH] refactor: use slog --- go.mod | 3 +-- go.sum | 12 ++++++------ main.go | 53 ++++++++++++++++++++++++++--------------------------- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index 6642859..80c028d 100755 --- a/go.mod +++ b/go.mod @@ -6,12 +6,11 @@ require ( github.com/alecthomas/chroma v0.10.0 github.com/dustin/go-humanize v1.0.0 github.com/gogs/git-module v1.6.0 - go.uber.org/zap v1.25.0 ) require ( github.com/dlclark/regexp2 v1.7.0 // indirect github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect - go.uber.org/multierr v1.10.0 // indirect + github.com/stretchr/testify v1.8.1 // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect ) diff --git a/go.sum b/go.sum index 043ea0b..e56874f 100755 --- a/go.sum +++ b/go.sum @@ -1,6 +1,5 @@ github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= 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= @@ -16,14 +15,14 @@ github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75/go.mod h1:76rf 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= -go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= -go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= -go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= 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 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 006aa4c..f76b5da 100755 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "strings" "sync" "unicode/utf8" + "log/slog" "github.com/alecthomas/chroma" formatterHtml "github.com/alecthomas/chroma/formatters/html" @@ -21,7 +22,6 @@ import ( "github.com/alecthomas/chroma/styles" "github.com/dustin/go-humanize" git "github.com/gogs/git-module" - "go.uber.org/zap" ) //go:embed html/*.tmpl static/* @@ -60,7 +60,7 @@ type Config struct { // pretty name for the repo RepoName string // logger - Logger *zap.SugaredLogger + Logger *slog.Logger // chroma style Theme *chroma.Style } @@ -320,7 +320,7 @@ func (c *Config) writeHtml(writeData *WriteData) { bail(err) fp := filepath.Join(dir, writeData.Filename) - c.Logger.Infof("writing (%s)", fp) + c.Logger.Info("writing", "filepath", fp) w, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) bail(err) @@ -342,7 +342,7 @@ func (c *Config) copyStatic(dir string) error { w, err := efs.ReadFile(infp) bail(err) fp := filepath.Join(c.Outdir, e.Name()) - c.Logger.Infof("writing (%s)", fp) + c.Logger.Info("writing", "filepath", fp) os.WriteFile(fp, w, 0755) } @@ -350,7 +350,7 @@ func (c *Config) copyStatic(dir string) error { } func (c *Config) writeRootSummary(data *PageData, readme template.HTML) { - c.Logger.Infof("writing root html (%s)", c.RepoPath) + c.Logger.Info("writing root html", "repoPath", c.RepoPath) c.writeHtml(&WriteData{ Filename: "index.html", Template: "html/summary.page.tmpl", @@ -362,7 +362,7 @@ func (c *Config) writeRootSummary(data *PageData, readme template.HTML) { } func (c *Config) writeTree(data *PageData, tree *TreeRoot) { - c.Logger.Infof("writing tree (%s)", tree.Path) + c.Logger.Info("writing tree", "treePath", tree.Path) c.writeHtml(&WriteData{ Filename: "index.html", Subdir: tree.Path, @@ -375,7 +375,7 @@ func (c *Config) writeTree(data *PageData, tree *TreeRoot) { } func (c *Config) writeLog(data *PageData, logs []*CommitData) { - c.Logger.Infof("writing log file (%s)", data.RevData.Name()) + c.Logger.Info("writing log file", "revision", data.RevData.Name()) c.writeHtml(&WriteData{ Filename: "index.html", Subdir: getLogBaseDir(data.RevData), @@ -389,7 +389,7 @@ func (c *Config) writeLog(data *PageData, logs []*CommitData) { } func (c *Config) writeRefs(data *PageData, refs []*RefInfo) { - c.Logger.Infof("writing refs (%s)", c.RepoPath) + c.Logger.Info("writing refs", "repoPath", c.RepoPath) c.writeHtml(&WriteData{ Filename: "refs.html", Template: "html/refs.page.tmpl", @@ -444,7 +444,7 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit * c.Mutex.RUnlock() if hasCommit { - c.Logger.Infof("(%s) commit file already generated, skipping", getShortID(commitID)) + c.Logger.Info("commit file already generated, skipping", "commitID", getShortID(commitID)) return } else { c.Mutex.Lock() @@ -576,7 +576,7 @@ func getShortID(id string) string { } func (c *Config) writeRepo() *BranchOutput { - c.Logger.Infof("Writing repo (%s)", c.RepoPath) + c.Logger.Info("writing repo", "repoPath", c.RepoPath) repo, err := git.Open(c.RepoPath) bail(err) @@ -656,7 +656,7 @@ func (c *Config) writeRepo() *BranchOutput { }) for _, revData := range revs { - c.Logger.Infof("Writing revision (%s)", revData.Name()) + c.Logger.Info("writing revision", "revision", revData.Name()) data := &PageData{ Repo: c, RevData: revData, @@ -858,6 +858,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) { return true } + if !treeEntries[i].IsDir && treeEntries[j].IsDir { + return false + } + return nameI < nameJ }) @@ -883,10 +887,10 @@ func (tw *TreeWalker) walk(tree *git.Tree, curpath string) { } func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []*RefInfo) *BranchOutput { - c.Logger.Infof( - "compiling (%s) revision (%s)", - c.RepoName, - pageData.RevData.Name(), + c.Logger.Info( + "compiling revision", + "repoName", c.RepoName, + "revision", pageData.RevData.Name(), ) output := &BranchOutput{} @@ -998,10 +1002,10 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs [] wg.Wait() - c.Logger.Infof( - "compilation complete (%s) branch (%s)", - c.RepoName, - pageData.RevData.Name(), + c.Logger.Info( + "compilation complete branch", + "repoName", c.RepoName, + "revision", pageData.RevData.Name(), ) output.Readme = readme @@ -1029,12 +1033,7 @@ func main() { theme := styles.Get(*themeFlag) - lg, err := zap.NewProduction() - if err != nil { - bail(err) - } - - logger := lg.Sugar() + logger := slog.Default() label := repoName(repoPath) if *labelFlag != "" { @@ -1060,7 +1059,7 @@ func main() { MaxCommits: *maxCommitsFlag, HideTreeLastCommit: *hideTreeLastCommitFlag, } - config.Logger.Infof("%+v", config) + config.Logger.Info("config", "config", config) if len(revs) == 0 { bail(fmt.Errorf("you must provide --revs")) @@ -1070,5 +1069,5 @@ func main() { config.copyStatic("static") url := filepath.Join("/", "index.html") - config.Logger.Info(url) + config.Logger.Info("root url", "url", url) }