log improvements; todo with question

master
Stephen Searles 2 years ago
parent 16e3583927
commit 43e119f12c
  1. 23
      caddyhugo.go
  2. 1
      content.go
  3. 2
      go.mod
  4. 3
      setup.go

@ -1,8 +1,8 @@
package caddyhugo package caddyhugo
import ( import (
"fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"path" "path"
"path/filepath" "path/filepath"
@ -12,6 +12,7 @@ import (
"git.stephensearles.com/stephen/acedoc" "git.stephensearles.com/stephen/acedoc"
"git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/comments"
"git.stephensearles.com/stephen/caddy-hugo2/media" "git.stephensearles.com/stephen/caddy-hugo2/media"
"go.uber.org/zap"
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
@ -42,17 +43,20 @@ func (m *CaddyHugo) CaddyModule() caddy.ModuleInfo {
} }
func (m *CaddyHugo) Provision(ctx caddy.Context) error { func (m *CaddyHugo) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
if m.Site.CommentsEnabled { if m.Site.CommentsEnabled {
m.Comments = comments.WithStorage(comments.NewDiskv(path.Join(m.Site.Root, "comments"))) m.Comments = comments.WithStorage(comments.NewDiskv(path.Join(m.Site.Root, "comments")))
} }
// TODO: not sure where m.Site is getting populated.
// m.Site.Root looks to be the working directory of caddy but it needs to be the directory of the site
root, err := filepath.Abs(m.Site.Root) root, err := filepath.Abs(m.Site.Root)
if err != nil { if err != nil {
return err return err
} }
log.Println("SETTING UP")
err = m.Setup(root) err = m.Setup(root)
log.Println("SET UP; err:", err)
if err != nil { if err != nil {
return err return err
} }
@ -79,6 +83,8 @@ func (s *SiteConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// CaddyHugo implements the plugin for a single site // CaddyHugo implements the plugin for a single site
type CaddyHugo struct { type CaddyHugo struct {
logger *zap.Logger
ServerType string ServerType string
Site SiteConfig Site SiteConfig
HugoSites *hugolib.HugoSites HugoSites *hugolib.HugoSites
@ -98,6 +104,17 @@ type CaddyHugo struct {
confirmingToClient map[uint64]struct{} confirmingToClient map[uint64]struct{}
} }
func (ch *CaddyHugo) log(msg string, args ...interface{}) {
all := make([]any, len(args)+1)
all[0] = msg
copy(all[1:], args)
ch.logger.Info(fmt.Sprint(all...))
}
func (ch *CaddyHugo) logf(msg string, args ...interface{}) {
ch.logger.Info(fmt.Sprintf(msg, args...))
}
// Build rebuilds the cached state of the site. TODO: determine if this republishes // Build rebuilds the cached state of the site. TODO: determine if this republishes
func (ch *CaddyHugo) Build() error { func (ch *CaddyHugo) Build() error {
return buildSite(ch.HugoSites) return buildSite(ch.HugoSites)

@ -98,6 +98,7 @@ func (ch *CaddyHugo) NewContent(name, ctype string) (string, error) {
if os.IsNotExist(err) { if os.IsNotExist(err) {
cmd := exec.Command("hugo", "new", filename) cmd := exec.Command("hugo", "new", filename)
cmd.Dir = ch.Dir cmd.Dir = ch.Dir
ch.logf(ch.Dir)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out)) return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out))

@ -1,6 +1,6 @@
module git.stephensearles.com/stephen/caddy-hugo2 module git.stephensearles.com/stephen/caddy-hugo2
go 1.16 go 1.18
require ( require (
git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d

@ -3,7 +3,6 @@ package caddyhugo
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"log"
"os" "os"
"path" "path"
@ -49,7 +48,7 @@ func (ch *CaddyHugo) commentsSetting() {
func (ch *CaddyHugo) Setup(dir string) error { func (ch *CaddyHugo) Setup(dir string) error {
var err error var err error
log.Println("setting up caddy-hugo in", dir) ch.log("setting up caddy-hugo in", dir)
ch.Dir = dir ch.Dir = dir
ch.docs = make(map[string]*editSession) ch.docs = make(map[string]*editSession)
ch.confirmingToClient = make(map[uint64]struct{}) ch.confirmingToClient = make(map[uint64]struct{})

Loading…
Cancel
Save