aboutsummaryrefslogtreecommitdiff
path: root/io.go
blob: ab8611fa919fb1fb5e095a0d2fbcb781740ce6b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
	"fmt"
	"io"
	"os"
)

// 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, 0640)
		if err != nil {
			return nil, err
		}
	}
	return out, nil
}

// Open a path for reading the content
func openInput(path string) (io.ReadCloser, error) {
	if path == "" {
		return nil, fmt.Errorf("empty path is invalid")
	}
	var err error
	var in io.ReadCloser
	if path == "stdin" {
		in = os.Stdin
	} else {
		in, err = os.Open(path)
	}
	return in, err
}