From 7dc19d115c5f3b012f96284adc8cadb9b703d6db Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 18 May 2024 20:23:27 +0200 Subject: remove lint errors --- cmd/dev/main.go | 35 +++++++++++++++----------- internal/builder/builder.go | 7 +++++- internal/builder/posts.go | 8 ++++-- internal/builder/template.go | 55 +++++++++++++++++++++++++++++++++-------- internal/config/config.go | 9 ------- internal/config/cspgenerator.go | 5 +++- 6 files changed, 81 insertions(+), 38 deletions(-) diff --git a/cmd/dev/main.go b/cmd/dev/main.go index 1a6ccea..aa3102f 100644 --- a/cmd/dev/main.go +++ b/cmd/dev/main.go @@ -36,7 +36,11 @@ type DevConfig struct { ServerURL config.URL `conf:"default:http://localhost:3001"` } -func RunCommandPiped(ctx context.Context, command string, args ...string) (cmd *exec.Cmd, err error) { +func RunCommandPiped( + ctx context.Context, + command string, + args ...string, +) (cmd *exec.Cmd, err error) { slog.Debug(fmt.Sprintf("running command %s %s", command, strings.Join(args, " "))) cmd = exec.CommandContext(ctx, command, args...) cmd.Env = append(os.Environ(), "DEBUG=") @@ -128,7 +132,7 @@ func server(ctx context.Context, devConfig DevConfig) error { done := make(chan struct{}) err = cmd.Start() if err != nil { - return errors.WithMessage(err, fmt.Sprintf("error starting server binary")) + return errors.WithMessage(err, "error starting server binary") } go func() { @@ -145,7 +149,10 @@ func server(ctx context.Context, devConfig DevConfig) error { select { case <-ctx.Done(): slog.Debug("server context done") - cmd.Process.Signal(os.Interrupt) + err := cmd.Process.Signal(os.Interrupt) + if err != nil { + return err + } <-done case err := <-cmdErr: return err @@ -219,15 +226,12 @@ func main() { done <- true }() go func() { - for { - select { - case ready := <-serverChan: - if ready { - slog.Debug("sending reload message") - eventsource.SendEventMessage("reload", "", "") - } else { - slog.Debug("server not ready") - } + for ready := range serverChan { + if ready { + slog.Debug("sending reload message") + eventsource.SendEventMessage("reload", "", "") + } else { + slog.Debug("server not ready") } } }() @@ -274,7 +278,10 @@ loop: case <-ctx.Done(): slog.Debug("main context cancelled") slog.Debug("calling server shutdown") - srv.Shutdown(devCtx) + err := srv.Shutdown(devCtx) + if err != nil { + slog.Debug("shutdown error", "error", err) + } exitCode = 1 break loop case event := <-fw.Events: @@ -291,10 +298,8 @@ loop: slog.Error(fmt.Sprintf("server reported error: %v", err)) if errors.As(err, &exerr) { slog.Debug("server exit error") - exitCode = exerr.ExitCode() } else { slog.Debug("server other error") - exitCode = 1 } break } diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 90e957c..cb3b670 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -114,7 +114,12 @@ func build(outDir string, config config.Config) error { } slog.Debug("rendering tags feed", "tag", tag) - output, err = renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), config, matchingPosts, tag) + output, err = renderFeed( + fmt.Sprintf("%s - %s", config.Title, tag), + config, + matchingPosts, + tag, + ) if err != nil { return errors.WithMessage(err, "could not render tag feed page") } diff --git a/internal/builder/posts.go b/internal/builder/posts.go index 223531b..a4526e4 100644 --- a/internal/builder/posts.go +++ b/internal/builder/posts.go @@ -52,13 +52,17 @@ var markdown = goldmark.New( func getPost(filename string) (*PostMatter, []byte, error) { matter := PostMatter{} content, err := os.Open(filename) - defer content.Close() if err != nil { return nil, nil, errors.WithMessagef(err, "could not open post %s", filename) } + defer content.Close() rest, err := frontmatter.MustParse(content, &matter) if err != nil { - return nil, nil, errors.WithMessagef(err, "could not parse front matter of post %s", filename) + return nil, nil, errors.WithMessagef( + err, + "could not parse front matter of post %s", + filename, + ) } return &matter, rest, nil diff --git a/internal/builder/template.go b/internal/builder/template.go index 74d0418..5bd990e 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -93,7 +93,12 @@ func (root QuerySelection) setImgURL(pageURL string, pageTitle string) QuerySele return root } -func layout(filename string, config config.Config, pageTitle string, pageURL string) (*goquery.Document, error) { +func layout( + filename string, + config config.Config, + pageTitle string, + pageURL string, +) (*goquery.Document, error) { html, err := loadTemplate(filename) if err != nil { return nil, err @@ -112,6 +117,9 @@ func layout(filename string, config config.Config, pageTitle string, pageURL str } defer countFile.Close() countHTML, err = NewDocumentNoScript(countFile) + if err != nil { + return + } if config.InjectLiveReload { liveReloadFile, err := os.OpenFile("templates/dev.html", os.O_RDONLY, 0) if err != nil { @@ -119,6 +127,9 @@ func layout(filename string, config config.Config, pageTitle string, pageURL str } defer liveReloadFile.Close() liveReloadHTML, err = goquery.NewDocumentFromReader(liveReloadFile) + if err != nil { + return + } } }) if err != nil { @@ -235,6 +246,9 @@ func renderHomepage(config config.Config, posts []Post, url string) (io.Reader, doc.Find(".title").AddClass("p-name u-url") html, err := renderMarkdown(index) + if err != nil { + return nil, err + } doc.Find("#content").SetHtml(html) feed := doc.Find(".h-feed") @@ -277,18 +291,29 @@ func render404(config config.Config, url string) (io.Reader, error) { return renderHTML(doc), nil } -func renderFeed(title string, config config.Config, posts []Post, specific string) (io.Reader, error) { +func renderFeed( + title string, + config config.Config, + posts []Post, + specific string, +) (io.Reader, error) { reader, err := loadTemplate("templates/feed.xml") if err != nil { return nil, err } defer reader.Seek(0, io.SeekStart) doc, err := xmlquery.Parse(reader) + if err != nil { + return nil, err + } feed := doc.SelectElement("feed") feed.SelectElement("title").FirstChild.Data = title feed.SelectElement("link").SetAttr("href", config.BaseURL.String()) feed.SelectElement("id").FirstChild.Data = atom.MakeTagURI(config, specific) datetime, err := posts[0].Date.UTC().MarshalText() + if err != nil { + return nil, err + } feed.SelectElement("updated").FirstChild.Data = string(datetime) tpl := feed.SelectElement("entry") xmlquery.RemoveFromTree(tpl) @@ -313,13 +338,16 @@ func renderFeed(title string, config config.Config, posts []Post, specific strin if err != nil { return nil, err } - entry, err := xmlquery.ParseWithOptions(strings.NewReader(string(text)), xmlquery.ParserOptions{ - Decoder: &xmlquery.DecoderOptions{ - Strict: false, - AutoClose: xml.HTMLAutoClose, - Entity: xml.HTMLEntity, + entry, err := xmlquery.ParseWithOptions( + strings.NewReader(string(text)), + xmlquery.ParserOptions{ + Decoder: &xmlquery.DecoderOptions{ + Strict: false, + AutoClose: xml.HTMLAutoClose, + Entity: xml.HTMLEntity, + }, }, - }) + ) if err != nil { return nil, err } @@ -341,6 +369,9 @@ func renderFeedStyles() (io.Reader, error) { "xhtml": "http://www.w3.org/1999/xhtml", } doc, err := xmlquery.Parse(reader) + if err != nil { + return nil, err + } expr, err := xpath.CompileWithNS("//xhtml:style", nsMap) if err != nil { return nil, err @@ -358,8 +389,12 @@ func renderHTML(doc *goquery.Document) io.Reader { // TODO: return errors to main thread go func() { - w.Write([]byte("\n")) - err := htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)}) + _, err := w.Write([]byte("\n")) + if err != nil { + slog.Error("error writing doctype") + w.CloseWithError(err) + } + err = htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)}) if err != nil { slog.Error("error rendering html", "error", err) w.CloseWithError(err) diff --git a/internal/config/config.go b/internal/config/config.go index 578390e..47422e7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,7 +4,6 @@ import ( "io/fs" "log/slog" "net/url" - "os" "github.com/BurntSushi/toml" "github.com/pkg/errors" @@ -46,14 +45,6 @@ type Config struct { Menus map[string][]MenuItem } -func getEnvFallback(key, fallback string) string { - if value, ok := os.LookupEnv(key); ok { - return value - } else { - return fallback - } -} - func GetConfig() (*Config, error) { config := Config{} slog.Debug("reading config.toml") diff --git a/internal/config/cspgenerator.go b/internal/config/cspgenerator.go index 0985b9e..5ad3ef0 100644 --- a/internal/config/cspgenerator.go +++ b/internal/config/cspgenerator.go @@ -53,10 +53,13 @@ import ( if err != nil { return err } - tags.Set(&structtag.Tag{ + err = tags.Set(&structtag.Tag{ Key: "toml", Name: cspTag.Name, }) + if err != nil { + return err + } _, err = fmt.Fprintf(file, "\t%-23s %-28s `%s`\n", field.Name, t, tags.String()) if err != nil { -- cgit 1.4.1