github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/file/file.go (about)

     1  /*
     2  Copyright 2016 Google Inc. All Rights Reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  
    14  // Package file provides utility file operations.
    15  package file
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"io/ioutil"
    21  	"os"
    22  )
    23  
    24  // ReadFile is like ioutil.ReadFile.
    25  func ReadFile(name string) ([]byte, os.FileInfo, error) {
    26  	fi, err := os.Stat(name)
    27  	if err != nil {
    28  		return nil, nil, err
    29  	}
    30  
    31  	data, err := ioutil.ReadFile(name)
    32  	return data, fi, err
    33  }
    34  
    35  // WriteFile is like ioutil.WriteFile
    36  func WriteFile(name string, data []byte) error {
    37  	return ioutil.WriteFile(name, data, 0644)
    38  }
    39  
    40  // OpenReadFile is like os.Open.
    41  func OpenReadFile(name string) io.ReadCloser {
    42  	f, err := os.Open(name)
    43  	if err != nil {
    44  		fmt.Fprintf(os.Stderr, "Could not open %s\n", name)
    45  		os.Exit(1)
    46  	}
    47  	return f
    48  }