From 1660561ffd7a78a160e88e9a0ebed5b44444ff91 Mon Sep 17 00:00:00 2001 From: Paul Lhussiez Date: Thu, 22 Aug 2019 10:44:19 +0200 Subject: [PATCH] Add proper Drone config, README and fix issues (ineff/errcheck) --- .drone.yml | 74 +++++++++++++++++++++++++++++++--------- README.md | 18 +++++----- cmd/generate.go | 10 +++++- cmd/new.go | 2 +- cmd/root.go | 2 +- cmd/serve.go | 2 +- filesystem/filesystem.go | 8 +++-- views/views.go | 6 +++- 8 files changed, 89 insertions(+), 33 deletions(-) diff --git a/.drone.yml b/.drone.yml index df6b7a1..9102e96 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,19 +1,59 @@ -workspace: - base: /go - path: src/github.com/Depado/smallblog/ +--- +kind: pipeline +name: default -pipeline: - prerequisites: - image: golang:1.10 - commands: - - go get -u github.com/golang/dep/cmd/dep - - dep ensure +steps: +- name: fetch + image: docker:git + commands: + - git fetch --tags - test: - image: golang:1.10 - commands: - - bash coverage.sh - - codecov: - image: robertstettner/drone-codecov - secrets: [ codecov_token ] \ No newline at end of file +- name: test + image: golang:latest + volumes: + - name: deps + path: /go + commands: + - go test -race -coverprofile=coverage.txt -covermode=atomic + environment: + GO111MODULE: "on" + +- name: linter + image: golang:latest + volumes: + - name: deps + path: /go + commands: + - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.17.1 + - ./bin/golangci-lint run + environment: + GO111MODULE: "on" + +- name: coverage + image: plugins/codecov + settings: + token: + from_secret: codecov_token + files: + - coverage.txt + +- name: telegram + image: appleboy/drone-telegram + settings: + to: 790376882 + format: markdown + token: + from_secret: telegram_token + message: > + *{{repo.name}}* + [Build {{build.number}}]({{build.link}}) by {{commit.author}} {{#success build.status}}succeeded{{else}}failed{{/success}} in {{buildtime build.started}} + `{{truncate commit.sha 8}}`: "{{commit.message}}" + when: + status: + - success + - failure + +volumes: +- name: deps + host: + path: /var/lib/cache/godeps/ \ No newline at end of file diff --git a/README.md b/README.md index 09837a2..1cbf274 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ # Smallblog -![Go Version](https://img.shields.io/badge/go-1.9-brightgreen.svg) -![Go Version](https://img.shields.io/badge/go-1.10-brightgreen.svg) +![Go Version](https://img.shields.io/badge/go-latest-brightgreen) [![Go Report Card](https://goreportcard.com/badge/github.com/Depado/smallblog)](https://goreportcard.com/report/github.com/Depado/smallblog) -[![Build Status](https://drone.depado.eu/api/badges/Depado/smallblog/status.svg)](https://drone.depado.eu/Depado/smallblog) +[![Build Status](https://drone.depa.do/api/badges/Depado/smallblog/status.svg)](https://drone.depa.do/Depado/smallblog) [![codecov](https://codecov.io/gh/Depado/smallblog/branch/master/graph/badge.svg)](https://codecov.io/gh/Depado/smallblog) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Depado/smallblog/blob/master/LICENSE) @@ -26,19 +25,20 @@ that's not as fun. ## Features -- Filesystem monitoring : Drop a new file or modify a file, bam, your blog is -updated +- Filesystem monitoring: Create, modify or delete a file and your blog is + updated - Automatic syntax highlighting using [bfchroma](https://github.com/Depado/bfchroma) -(which uses [chroma](https://github.com/alecthomas/chroma) under the hood) + (which uses [chroma](https://github.com/alecthomas/chroma) under the hood) +- Supports [admonitions](https://python-markdown.github.io/extensions/admonition/) + using [bfadmonition](https://github.com/Depado/bfadmonition) - No CGO dependencies - Tag system - Simple and customizable template and CSS for easy reading - Comments using [gitalk](https://github.com/gitalk/gitalk) - Supports global assets for all your articles -- Can generate a static site : +- Can generate a static site: - Tags won't work - Raw markdown won't work - - Can't test without a proper web server like Caddy or Nginx ## Tutorial @@ -213,7 +213,7 @@ post's title if it is omitted. ## Example Article -`pages/example-article` +`pages/example-article.md` ``` title: "Example article !" diff --git a/cmd/generate.go b/cmd/generate.go index 4fcdfd1..58a17e8 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -29,13 +29,21 @@ var generate = &cobra.Command{ logrus.WithError(err).Fatal("Couldn't parse directory") } t, err := template.ParseGlob("templates/*.tmpl") + if err != nil { + logrus.WithError(err).Fatal("Unable to parse templates") + } for _, v := range models.MPages { var fd *os.File v.Slug = v.Slug + ".html" if fd, err = os.Create(filepath.Join(pages, v.Slug)); err != nil { logrus.WithError(err).Fatal("Couldn't create file") } - t.ExecuteTemplate(fd, "post.tmpl", gin.H{"post": v, "gitalk": models.GetGitalk(), "local": true}) + if err = t.ExecuteTemplate( + fd, "post.tmpl", + gin.H{"post": v, "gitalk": models.GetGitalk(), "local": true}, + ); err != nil { + logrus.WithError(err).Fatal("Unable to execute template") + } } var fd *os.File if fd, err = os.Create(filepath.Join(output, "index.html")); err != nil { diff --git a/cmd/new.go b/cmd/new.go index 4a05a7f..3d25437 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -88,5 +88,5 @@ func init() { newCmd.Flags().String("author.github", "", "github username of the author (overrides global conf)") newCmd.Flags().String("author.site", "", "website of the author (overrides global conf)") newCmd.Flags().String("author.avatar", "", "URL to the author's avatar (overrides global conf)") - viper.BindPFlags(newCmd.Flags()) + viper.BindPFlags(newCmd.Flags()) // nolint: errcheck } diff --git a/cmd/root.go b/cmd/root.go index 1b451d9..cbd5792 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,7 +46,7 @@ func init() { rootCmd.PersistentFlags().Bool("blog.share", false, "add a Twitter share button on articles") // Flag binding - viper.BindPFlags(rootCmd.PersistentFlags()) + viper.BindPFlags(rootCmd.PersistentFlags()) // nolint: errcheck } func initialize() { diff --git a/cmd/serve.go b/cmd/serve.go index 3edd195..f923c04 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -32,5 +32,5 @@ func init() { serve.Flags().String("gitalk.repo", "", "repository where the comments will be stored") serve.Flags().String("gitalk.owner", "", "repository owner") serve.Flags().StringArray("gitalk.admins", []string{}, "gitalk admins") - viper.BindPFlags(serve.Flags()) + viper.BindPFlags(serve.Flags()) // nolint: errcheck } diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go index ffd3c64..c594b9d 100644 --- a/filesystem/filesystem.go +++ b/filesystem/filesystem.go @@ -5,6 +5,7 @@ import ( "path/filepath" "github.com/rjeczalik/notify" + "github.com/sirupsen/logrus" "github.com/Depado/smallblog/models" ) @@ -30,11 +31,14 @@ func Watch(dir string) { case notify.Write, notify.InMovedTo: if p, ok := m[filepath.Base(ei.Path())]; ok { // Existing file, needs to be parsed. - p.UpdateFromFile(ei.Path()) + if err = p.UpdateFromFile(ei.Path()); err != nil { + logrus.WithError(err).WithField("file", ei.Path()).Error("Unable to update post") + } } else { // New file. Needs to be parsed and inserted. - p := new(models.Page) + var p *models.Page if p, err = models.NewPageFromFile(ei.Path()); err != nil { + logrus.WithError(err).WithField("file", ei.Path()).Error("Unable to update post") continue } m[filepath.Base(ei.Path())] = p diff --git a/views/views.go b/views/views.go index 4b92c76..ecc3275 100644 --- a/views/views.go +++ b/views/views.go @@ -7,6 +7,7 @@ import ( "sort" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/Depado/smallblog/models" @@ -69,7 +70,10 @@ func Post(c *gin.Context) { func RawPost(c *gin.Context) { slug := c.Param("slug") if val, ok := models.MPages[slug]; ok { - c.Writer.Write([]byte(val.Raw)) + if _, err := c.Writer.Write([]byte(val.Raw)); err != nil { + logrus.WithError(err).Error("Unable to write raw markdown") + c.String(http.StatusInternalServerError, "500 internal server error") + } } else { c.String(http.StatusNotFound, "404 not found") }