aboutsummaryrefslogblamecommitdiff
path: root/io.go
blob: ab8611fa919fb1fb5e095a0d2fbcb781740ce6b9 (plain) (tree)
1
2
3
4
5
6
7
8

            
        


             

 








                                                      
                                                                                               


                                       
         
                       

 



                                                               
         





                                       
         
                      
 
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
}