aboutsummaryrefslogtreecommitdiff
path: root/io.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-15 01:34:25 +0100
committerGibheer <gibheer@gmail.com>2015-02-15 01:34:25 +0100
commit16eb14db9f9b228ef88bcf1beb09cf823256dac1 (patch)
tree414ed9ba9f3e5679a7b0ae7b120e752d3f8ee2f6 /io.go
parent2f9126dc6a2eab32316ec90e21688d31159f9e80 (diff)
redesign cli
This is a major rebuilding of the CLI. The library part is split out into pkilib and the cli handles only the communication with the user, I/O and the library. The API will still look the same, but the code should be much better to grasp. Instead of repeating everything, more will be grouped together and reused.
Diffstat (limited to 'io.go')
-rw-r--r--io.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/io.go b/io.go
new file mode 100644
index 0000000..382044e
--- /dev/null
+++ b/io.go
@@ -0,0 +1,41 @@
+package main
+
+// handle all io and de/encoding of data
+
+import (
+ "encoding/pem"
+ "errors"
+ "io/ioutil"
+)
+
+var (
+ ErrBlockNotFound = errors.New("block not found")
+)
+
+// load a pem section from a file
+func readSectionFromFile(path, btype string) ([]byte, error) {
+ raw, err := readFile(path)
+ if err != nil { return raw, err }
+
+ return decodeSection(raw, btype)
+}
+
+// read a file completely and report possible errors
+func readFile(path string) ([]byte, error) {
+ raw, err := ioutil.ReadFile(path)
+ if err != nil { return EmptyByteArray, err }
+ return raw, nil
+}
+
+// decode a pem encoded file and search for the specified section
+func decodeSection(data []byte, btype string) ([]byte, error) {
+ rest := data
+ for len(rest) > 0 {
+ var block *pem.Block
+ block, rest = pem.Decode(rest)
+ if block.Type == btype {
+ return block.Bytes, nil
+ }
+ }
+ return EmptyByteArray, ErrBlockNotFound
+}