diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2016-10-01 21:56:29 +0200 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2016-10-01 21:56:29 +0200 |
commit | d01892150eed9d58210eb40b7c005d5fa8e93238 (patch) | |
tree | f9d37f3d5b4f0d9afd01755801826713f47d83c3 /io.go | |
parent | faaf7d8859895767b5e64d32c14d561d6fdb5a14 (diff) |
rework program flow
This commit is a complete rebuild of pkictl. Before everything was all
over the place and adding new commands was kind of a hassle.
Now each command has its own file and can be adjusted on a command
basis. Options are still used by the same name, but can now use
different descriptions.
Diffstat (limited to 'io.go')
-rw-r--r-- | io.go | 62 |
1 files changed, 28 insertions, 34 deletions
@@ -1,45 +1,39 @@ package main -// handle all io and de/encoding of data - import ( - "encoding/pem" - "errors" - "io/ioutil" -) - -var ( - ErrBlockNotFound = errors.New("block not found") + "fmt" + "io" + "os" ) -// load a pem section from a file -func readSectionFromFile(path, btype string) ([]byte, error) { - raw, err := readFile(path) - if err != nil { - return raw, err +// Open a path for writing +func openOutput(path string) (io.WriteCloser, error) { + var ( + err error + out io.WriteCloser + ) + if path == "stdout" { + out = os.Stdout + } else { + out, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0700) + if err != nil { + return nil, err + } } - - return decodeSection(raw, btype) + return out, nil } -// 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 +// Open a path for reading the content +func openInput(path string) (io.ReadCloser, error) { + if path == "" { + return nil, fmt.Errorf("empty path is invalid") } - 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 - } + var err error + var in io.ReadCloser + if path == "stdin" { + in = os.Stdin + } else { + in, err = os.Open(path) } - return EmptyByteArray, ErrBlockNotFound + return in, err } |