diff --git a/history/feed.go b/history/feed.go index 4906816..edd0dda 100644 --- a/history/feed.go +++ b/history/feed.go @@ -3,11 +3,12 @@ package history import ( "errors" "fmt" - "github.com/bouncepaw/mycorrhiza/internal/cfg" "net/url" "strings" "time" + "github.com/bouncepaw/mycorrhiza/internal/cfg" + "github.com/gorilla/feeds" ) diff --git a/history/history.go b/history/history.go index b889e80..0e77786 100644 --- a/history/history.go +++ b/history/history.go @@ -4,7 +4,7 @@ package history import ( "bytes" "fmt" - "log" + "log/slog" "os/exec" "path/filepath" "regexp" @@ -21,12 +21,14 @@ var renameMsgPattern = regexp.MustCompile(`^Rename ‘(.*)’ to ‘.*’`) var gitEnv = []string{"GIT_COMMITTER_NAME=wikimind", "GIT_COMMITTER_EMAIL=wikimind@mycorrhiza"} // Start finds git and initializes git credentials. -func Start() { +func Start() error { path, err := exec.LookPath("git") if err != nil { - log.Fatal("Could not find the git executable. Check your $PATH.") + slog.Error("Could not find the Git executable. Check your $PATH.") + return err } gitpath = path + return nil } // InitGitRepo checks a Git repository and initializes it if necessary. @@ -44,7 +46,7 @@ func InitGitRepo() { } } if !isGitRepo { - log.Println("Initializing Git repo at", files.HyphaeDir()) + slog.Info("Initializing Git repo", "path", files.HyphaeDir()) gitsh("init") gitsh("config", "core.quotePath", "false") } @@ -60,7 +62,7 @@ func gitsh(args ...string) (out bytes.Buffer, err error) { b, err := cmd.CombinedOutput() if err != nil { - log.Println("gitsh:", err) + slog.Info("Git command failed", "err", err, "output", string(b)) } return *bytes.NewBuffer(b), err } @@ -77,7 +79,9 @@ func silentGitsh(args ...string) (out bytes.Buffer, err error) { // Rename renames from `from` to `to` using `git mv`. func Rename(from, to string) error { - log.Println(util.ShorterPath(from), util.ShorterPath(to)) + slog.Info("Renaming file with git mv", + "from", util.ShorterPath(from), + "to", util.ShorterPath(to)) _, err := gitsh("mv", "--force", from, to) return err } diff --git a/history/histweb/histview.go b/history/histweb/histview.go index 196b998..7c7b3a4 100644 --- a/history/histweb/histview.go +++ b/history/histweb/histview.go @@ -4,19 +4,21 @@ package histweb import ( "embed" "fmt" - "github.com/bouncepaw/mycorrhiza/history" - "github.com/bouncepaw/mycorrhiza/internal/cfg" - "github.com/bouncepaw/mycorrhiza/internal/files" - hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae" - "github.com/bouncepaw/mycorrhiza/util" - viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil" - "github.com/gorilla/mux" "html/template" - "log" + "log/slog" "net/http" "path/filepath" "strconv" "strings" + + "github.com/bouncepaw/mycorrhiza/history" + "github.com/bouncepaw/mycorrhiza/internal/cfg" + "github.com/bouncepaw/mycorrhiza/internal/files" + "github.com/bouncepaw/mycorrhiza/internal/hyphae" + "github.com/bouncepaw/mycorrhiza/util" + "github.com/bouncepaw/mycorrhiza/web/viewutil" + + "github.com/gorilla/mux" ) func InitHandlers(rtr *mux.Router) { @@ -30,9 +32,9 @@ func InitHandlers(rtr *mux.Router) { rtr.HandleFunc("/recent-changes-atom", handlerRecentChangesAtom) rtr.HandleFunc("/recent-changes-json", handlerRecentChangesJSON) - chainPrimitiveDiff = viewutil2.CopyEnRuWith(fs, "view_primitive_diff.html", ruTranslation) - chainRecentChanges = viewutil2.CopyEnRuWith(fs, "view_recent_changes.html", ruTranslation) - chainHistory = viewutil2.CopyEnRuWith(fs, "view_history.html", ruTranslation) + chainPrimitiveDiff = viewutil.CopyEnRuWith(fs, "view_primitive_diff.html", ruTranslation) + chainRecentChanges = viewutil.CopyEnRuWith(fs, "view_recent_changes.html", ruTranslation) + chainHistory = viewutil.CopyEnRuWith(fs, "view_history.html", ruTranslation) } func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) { @@ -45,12 +47,12 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) { } var ( mycoFilePath string - h = hyphae2.ByName(util.CanonicalName(slug)) + h = hyphae.ByName(util.CanonicalName(slug)) ) switch h := h.(type) { - case hyphae2.ExistingHypha: + case hyphae.ExistingHypha: mycoFilePath = h.TextFilePath() - case *hyphae2.EmptyHypha: + case *hyphae.EmptyHypha: mycoFilePath = filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco") } text, err := history.PrimitiveDiffAtRevision(mycoFilePath, revHash) @@ -58,7 +60,7 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - primitiveDiff(viewutil2.MetaFrom(w, rq), h, revHash, text) + primitiveDiff(viewutil.MetaFrom(w, rq), h, revHash, text) } // handlerRecentChanges displays the /recent-changes/ page. @@ -68,7 +70,7 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) { if editCount > 100 { return } - recentChanges(viewutil2.MetaFrom(w, rq), editCount, history.RecentChanges(editCount)) + recentChanges(viewutil.MetaFrom(w, rq), editCount, history.RecentChanges(editCount)) } // handlerHistory lists all revisions of a hypha. @@ -81,9 +83,11 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) { if err == nil { list = history.WithRevisions(hyphaName, revs) } - log.Println("Found", len(revs), "revisions for", hyphaName) - historyView(viewutil2.MetaFrom(w, rq), hyphaName, list) + // TODO: extra log, not needed? + slog.Info("Found revisions", "hyphaName", hyphaName, "n", len(revs), "err", err) + + historyView(viewutil.MetaFrom(w, rq), hyphaName, list) } // genericHandlerOfFeeds is a helper function for the web feed handlers. @@ -135,20 +139,20 @@ var ( {{define "n recent changes"}}{{.}} свеж{{if eq . 1}}ая правка{{else if le . 4}}их правок{{else}}их правок{{end}}{{end}} {{define "recent empty"}}Правки не найдены.{{end}} ` - chainPrimitiveDiff, chainRecentChanges, chainHistory viewutil2.Chain + chainPrimitiveDiff, chainRecentChanges, chainHistory viewutil.Chain ) type recentChangesData struct { - *viewutil2.BaseData + *viewutil.BaseData EditCount int Changes []history.Revision UserHypha string Stops []int } -func recentChanges(meta viewutil2.Meta, editCount int, changes []history.Revision) { - viewutil2.ExecutePage(meta, chainRecentChanges, recentChangesData{ - BaseData: &viewutil2.BaseData{}, +func recentChanges(meta viewutil.Meta, editCount int, changes []history.Revision) { + viewutil.ExecutePage(meta, chainRecentChanges, recentChangesData{ + BaseData: &viewutil.BaseData{}, EditCount: editCount, Changes: changes, UserHypha: cfg.UserHypha, @@ -157,13 +161,13 @@ func recentChanges(meta viewutil2.Meta, editCount int, changes []history.Revisio } type primitiveDiffData struct { - *viewutil2.BaseData + *viewutil.BaseData HyphaName string Hash string Text template.HTML } -func primitiveDiff(meta viewutil2.Meta, h hyphae2.Hypha, hash, text string) { +func primitiveDiff(meta viewutil.Meta, h hyphae.Hypha, hash, text string) { hunks := history.SplitPrimitiveDiff(text) if len(hunks) > 0 { var buf strings.Builder @@ -198,8 +202,8 @@ func primitiveDiff(meta viewutil2.Meta, h hyphae2.Hypha, hash, text string) { text = fmt.Sprintf( `
%s`, text)
}
- viewutil2.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{
- BaseData: &viewutil2.BaseData{},
+ viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{
+ BaseData: &viewutil.BaseData{},
HyphaName: h.CanonicalName(),
Hash: hash,
Text: template.HTML(text),
@@ -207,14 +211,14 @@ func primitiveDiff(meta viewutil2.Meta, h hyphae2.Hypha, hash, text string) {
}
type historyData struct {
- *viewutil2.BaseData
+ *viewutil.BaseData
HyphaName string
Contents string
}
-func historyView(meta viewutil2.Meta, hyphaName, contents string) {
- viewutil2.ExecutePage(meta, chainHistory, historyData{
- BaseData: &viewutil2.BaseData{
+func historyView(meta viewutil.Meta, hyphaName, contents string) {
+ viewutil.ExecutePage(meta, chainHistory, historyData{
+ BaseData: &viewutil.BaseData{
Addr: "/history/" + util.CanonicalName(hyphaName),
},
HyphaName: hyphaName,
diff --git a/history/operations.go b/history/operations.go
index 0f9eb80..71cfbd5 100644
--- a/history/operations.go
+++ b/history/operations.go
@@ -4,11 +4,11 @@ package history
// Things related to writing history.
import (
"fmt"
- "github.com/bouncepaw/mycorrhiza/internal/user"
"os"
"path/filepath"
"sync"
+ "github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/util"
)
diff --git a/history/revision.go b/history/revision.go
index 7f04309..9f5becb 100644
--- a/history/revision.go
+++ b/history/revision.go
@@ -2,7 +2,8 @@ package history
import (
"fmt"
- "log"
+ "log/slog"
+ "os"
"regexp"
"strconv"
"strings"
@@ -11,9 +12,11 @@ import (
"github.com/bouncepaw/mycorrhiza/internal/files"
)
-// Revision represents a revision, duh. Hash is usually short. Username is extracted from email.
+// Revision represents a revision of a hypha.
type Revision struct {
- Hash string
+ // Hash is usually short.
+ Hash string
+ // Username is extracted from email.
Username string
Time time.Time
Message string
@@ -71,7 +74,9 @@ func (stream *recentChangesStream) next(n int) []Revision {
res, err := gitLog(args...)
if err != nil {
- log.Fatal(err)
+ // TODO: return error
+ slog.Error("Failed to git log", "err", err)
+ os.Exit(1)
}
if len(res) != 0 {
stream.currHash = res[len(res)-1].Hash
@@ -103,14 +108,14 @@ func (stream recentChangesStream) iterator() func() (Revision, bool) {
func RecentChanges(n int) []Revision {
stream := newRecentChangesStream()
revs := stream.next(n)
- log.Printf("Found %d recent changes", len(revs))
+ slog.Info("Found recent changes", "n", len(revs))
return revs
}
// Revisions returns slice of revisions for the given hypha name, ordered most recent first.
func Revisions(hyphaName string) ([]Revision, error) {
revs, err := gitLog("--", hyphaName+".*")
- log.Printf("Found %d revisions for ‘%s’\n", len(revs), hyphaName)
+ slog.Info("Found revisions", "hyphaName", hyphaName, "n", len(revs), "err", err)
return revs, err
}
diff --git a/main.go b/main.go
index b70189c..a2b6f50 100644
--- a/main.go
+++ b/main.go
@@ -5,13 +5,9 @@
package main
import (
- "github.com/bouncepaw/mycorrhiza/internal/categories"
- "log"
- "os"
- "syscall"
-
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/backlinks"
+ "github.com/bouncepaw/mycorrhiza/internal/categories"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
@@ -23,6 +19,8 @@ import (
"github.com/bouncepaw/mycorrhiza/web"
"github.com/bouncepaw/mycorrhiza/web/static"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
+ "log"
+ "os"
)
func main() {
@@ -48,7 +46,9 @@ func main() {
backlinks.IndexBacklinks()
go backlinks.RunBacklinksConveyor()
user.InitUserDatabase()
- history.Start()
+ if err := history.Start(); err != nil {
+ os.Exit(1)
+ }
history.InitGitRepo()
migration.MigrateRocketsMaybe()
migration.MigrateHeadingsMaybe()
@@ -65,6 +65,6 @@ func main() {
err := serveHTTP(web.Handler())
if err != nil {
- syscall.Exit(1)
+ os.Exit(1)
}
}