~chrisppy/go-barefeed

ref: 63fc114fd6c8a8779e44c7042a5c1a6f543a76fb go-barefeed/barefeed.go -rw-r--r-- 841 bytes
63fc114fChris Palmer updates from schema 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Package barefeed is an implementation of the barefeed message format for Go.
//
// https://git.sr.ht/~chrisppy/go-barefeed
package barefeed

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

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

// FromFile will read barefeed from a file
func FromFile(path string) (*Message, error) {
	b, err := os.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{}
	if _, err := buf.ReadFrom(reader); err != nil {
		return nil, err
	}
	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
}