aboutsummaryrefslogtreecommitdiff
path: root/file.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-09-18 16:45:11 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-09-18 16:45:11 +0200
commit0baeb781e47e3334162c086dfbe198f79f435789 (patch)
tree12388deaf4936e09637ec701d06aeaf9edc495cc /file.go
parent83e37e3326921806642351c59761b16919c10e72 (diff)
io
Diffstat (limited to 'file.go')
-rw-r--r--file.go35
1 files changed, 22 insertions, 13 deletions
diff --git a/file.go b/file.go
index eaee236..795dda0 100644
--- a/file.go
+++ b/file.go
@@ -5,6 +5,7 @@ import (
"bytes"
"errors"
"fmt"
+ "io"
"io/ioutil"
"os"
"strings"
@@ -29,13 +30,8 @@ const (
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)
+func Parse(r io.Reader) (File, error) {
+ buf := bufio.NewReader(r)
comment, err := buf.ReadString('\n')
if err != nil {
return File{}, err
@@ -59,16 +55,29 @@ func ParseFile(fname string) (File, error) {
}, nil
}
-func (f File) EncodeFile(fname string) error {
- fd, err := os.Create(fname)
+func ParseFile(fname string) (File, error) {
+ fd, err := os.Open(fname)
if err != nil {
- return err
+ return File{}, err
}
defer fd.Close()
- fmt.Fprintln(fd, commentHdr, f.Comment)
- fmt.Fprintln(fd, string(f.B64))
+ return Parse(fd)
+}
+
+func (f File) Encode(w io.Writer) error {
+ fmt.Fprintf(w, "%v%v\n", commentHdr, f.Comment)
+ fmt.Fprintf(w, "%v\n", string(f.B64))
if f.Body != nil {
- fmt.Fprintln(fd, string(f.Body))
+ fmt.Fprintf(w, "%v", string(f.Body))
}
return nil
}
+
+func (f File) EncodeFile(fname string) error {
+ fd, err := os.Create(fname)
+ if err != nil {
+ return err
+ }
+ defer fd.Close()
+ return f.Encode(fd)
+}