aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-01-19 12:13:13 +0100
committerGibheer <gibheer@gmail.com>2015-01-19 12:13:13 +0100
commitf74ed0fda27fd4839281b41c8fc008e97985504f (patch)
tree06f869b0080d138c7f4b7ae1dd840e4d58bffd12
parent1ff5d678a2df34ea601ce6a416b83ed767219c49 (diff)
split stream opening into two functions
This way it is easier to open files for reading and writing.
-rw-r--r--main.go24
1 files changed, 17 insertions, 7 deletions
diff --git a/main.go b/main.go
index 73ea1b2..7a6bc5e 100644
--- a/main.go
+++ b/main.go
@@ -47,16 +47,26 @@ func open_output_stream(path string) (io.WriteCloser, error) {
switch path {
case "STDOUT": return os.Stdout, nil
case "STDERR": return os.Stderr, nil
- default:
- var err error
- output_stream, err := os.OpenFile(path, os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0600)
- if err != nil {
- return nil, err
- }
- return output_stream, nil
+ default: return open_stream(path, os.O_WRONLY | os.O_CREATE | os.O_TRUNC)
}
}
+func open_input_stream(path string) (io.ReadCloser, error) {
+ switch path {
+ case "STDIN": return os.Stdin, nil
+ default: return open_stream(path, os.O_RDONLY)
+ }
+}
+
+func open_stream(path string, flags int) (io.ReadWriteCloser, error) {
+ var err error
+ output_stream, err := os.OpenFile(path, flags, 0600)
+ if err != nil {
+ return nil, err
+ }
+ return output_stream, nil
+}
+
// print the module help
func print_modules() {
fmt.Printf(`Usage: %s command args