From f24d0f7933e6df09fb33bd65e2af85e6c2a23031 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 18 Sep 2016 14:14:27 +0200 Subject: Split File --- file.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 file.go (limited to 'file.go') diff --git a/file.go b/file.go new file mode 100644 index 0000000..e321695 --- /dev/null +++ b/file.go @@ -0,0 +1,59 @@ +package main + +import ( + "bufio" + "bytes" + "errors" + "io/ioutil" + "os" + "strings" +) + +type B64 interface { + Marshal() ([]byte, error) + Unmarshal([]byte) error +} + +type File struct { + Comment string + B64 []byte + Body []byte +} + +const ( + commentHdr = "untrusted comment: " + verifyWith = "verify with " + pubKey = "%s public key" + secKey = "%s secret key" + sigFrom = "signature from %s" +) + +func ParseFile(fname string) (File, error) { + fd, err := os.Open(fname) + if err != nil { + return File{}, err + } + defer fd.Close() + buf := bufio.NewReader(fd) + comment, err := buf.ReadString('\n') + if err != nil { + return File{}, err + } + if !strings.HasPrefix(comment, commentHdr) { + return File{}, errors.New("expected untrusted header") + } + comment = comment[len(commentHdr):] + b64, err := buf.ReadBytes('\n') + if err != nil { + return File{}, err + } + body, err := ioutil.ReadAll(buf) + if err != nil { + return File{}, err + } + return File{ + Comment: strings.TrimRight(comment, "\r\n"), + B64: bytes.TrimRight(b64, "\r\n"), + Body: body, + }, nil +} -- cgit v1.2.3