~chrisppy/go-barefeed

700e181767a3c79a6b83cb3a10f425d509a53026 — Chris Palmer 9 months ago
Initial Commit
11 files changed, 225 insertions(+), 0 deletions(-)

A .build.yml
A .gitignore
A LICENSE
A README.md
A barefeed.go
A feed.go
A go.mod
A go.sum
A item.go
A message.go
A time.go
A  => .build.yml +41 -0
@@ 1,41 @@
image: alpine/edge
packages:
- go
sources:
- https://git.sr.ht/~chrisppy/go-barefeed
tasks:
- format: |
    cd go-barefeed
    go fmt -x ./...
    go vet ./...
- lint: |
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    go get -u golang.org/x/lint/golint
    go get -u honnef.co/go/tools/cmd/staticcheck
    go get -u gitlab.com/opennota/check/cmd/aligncheck
    go get -u gitlab.com/opennota/check/cmd/structcheck
    go get -u gitlab.com/opennota/check/cmd/varcheck
    go get -u github.com/kisielk/errcheck
    cd go-barefeed
    golint -set_exit_status ./...
    staticcheck ./...
    aligncheck ./...
    structcheck ./...
    varcheck ./...
    errcheck ./...
- security: |
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    go get -u github.com/securego/gosec/cmd/gosec
    cd go-barefeed
    gosec -exclude=G107,G204 ./...
- dependencies: |
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    go get -u github.com/psampaz/go-mod-outdated
    cd go-barefeed
    go list -u -m -json all | go-mod-outdated -direct -ci
- test: |
    cd go-barefeed
    go test ./...

A  => .gitignore +3 -0
@@ 1,3 @@
*.test
*.prof
*.log

A  => LICENSE +21 -0
@@ 1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Chris Palmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README.md +13 -0
@@ 1,13 @@
# go-barefeed [![GoDoc](https://godoc.org/git.sr.ht/~chrisppy/go-barefeed?status.svg)](https://godoc.org/git.sr.ht/~chrisppy/go-barefeed) [![builds.sr.ht status](https://builds.sr.ht/~chrisppy/go-barefeed.svg)](https://builds.sr.ht/~chrisppy/go-barefeed?)

An implementation of the [barefeed](http://git.sr.ht/~chrisppy/barefeed) message
format for Go.

## Contributing

[Send patches](https://git-send-email.io) to
[~chrisppy/beagles-devel@lists.sr.ht](https://lists.sr.ht/~chrisppy/beagles-devel)

## License

MIT

A  => barefeed.go +68 -0
@@ 1,68 @@
// Package barefeed is an implementation of the barefeed message format for Go.
//
// https://git.sr.ht/~chrisppy/go-barefeed
package barefeed

import (
	"bytes"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"

	"git.sr.ht/~sircmpwn/go-bare"
)

// Message is the core type for the barefeed message format. It will handle any
// new version that may be released
type Message interface {
	bare.Union
}

// IsUnion is needed for unioning to the Message type
func (t MessageV1) IsUnion() {}

func init() {
	bare.RegisterUnion((*Message)(nil)).
		Member(*new(MessageV1), 0)
}

// FromFile will read barefeed from a file
func FromFile(path string) (*Message, error) {
	b, err := ioutil.ReadFile(filepath.Clean(path))
	if err != nil {
		return nil, err
	}
	return FromBytes(b)
}

// FromReader will read barefeed from a Reader
func FromReader(reader io.Reader) (*Message, error) {
	buf := &bytes.Buffer{}
	buf.ReadFrom(reader)
	return FromBytes(buf.Bytes())
}

// FromBytes will read barefeed from bytes
func FromBytes(b []byte) (*Message, error) {
	var m Message
	if err := bare.Unmarshal(b, &m); err != nil {
		return nil, err
	}
	return &m, nil
}

// Bytes will return the barefeed as a slice of bytes
func Bytes(t *Message) ([]byte, error) {
	return bare.Marshal(t)
}

// WriteFile will write barefeed to a file at the designated path
func WriteFile(t *Message, path string) error {
	b, err := Bytes(t)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filepath.Clean(path), b, 0644)
}

A  => feed.go +10 -0
@@ 1,10 @@
package barefeed

// Feed contains all info needed for a RSS Feed
type Feed struct {
	Feed        string `bare:"feed"`
	Title       string `bare:"title"`
	Description string `bare:"description"`
	Link        string `bare:"link"`
	Items       []Item `bare:"items"`
}

A  => go.mod +5 -0
@@ 1,5 @@
module git.sr.ht/~chrisppy/go-barefeed

go 1.15

require git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287

A  => go.sum +15 -0
@@ 1,15 @@
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287 h1:mfySyh3Gq6RTlvHFGhrWKtmvTKBFKQQDpRc2Em0cDow=
git.sr.ht/~sircmpwn/go-bare v0.0.0-20201210182351-86af428a8287/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

A  => item.go +14 -0
@@ 1,14 @@
package barefeed

// Item contains all info needed for a RSS Item
type Item struct {
	Link     string `bare:"link"`
	Title    string `bare:"title"`
	Content  string `bare:"content"`
	Location string `bare:"location"`
	Mimetype string `bare:"mimetype"`
	Length   string `bare:"length"`
	Read     bool   `bare:"read"`
	Favorite bool   `bare:"favorite"`
	Date     Time   `bare:"date"`
}

A  => message.go +8 -0
@@ 1,8 @@
package barefeed

// MessageV1 is the first implementation of the barefeed message format
type MessageV1 struct {
	Created   Time   `bare:"created"`
	Generator string `bare:"generator"`
	Feeds     []Feed `bare:"feeds"`
}

A  => time.go +27 -0
@@ 1,27 @@
package barefeed

import (
	"fmt"
	"time"

	"git.sr.ht/~sircmpwn/go-bare"
)

// Time type conforming to RFC3339
type Time time.Time

// Unmarshal the time type
func (t *Time) Unmarshal(r *bare.Reader) error {
	st, err := r.ReadString()
	if err != nil {
		return fmt.Errorf("Time.Unmarshal: read string: %e", err)
	}

	tm, err := time.Parse(time.RFC3339, st)
	if err != nil {
		return fmt.Errorf("Time.Unmarshal: parse time: %e", err)
	}

	*t = Time(tm)
	return nil
}