github.com/blend/go-sdk@v1.20220411.3/sh/to_file.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package sh
     9  
    10  import "os"
    11  
    12  // MustToFile opens or creates a file and panics on error.
    13  // This is meant to be used as an output writer for a command.
    14  func MustToFile(path string) *os.File {
    15  	file, err := ToFile(path)
    16  	if err != nil {
    17  		panic(err)
    18  	}
    19  	return file
    20  }
    21  
    22  // ToFile opens or creates a file.
    23  // This is meant to be used as an output writer for a command.
    24  func ToFile(path string) (*os.File, error) {
    25  	if _, err := os.Stat(path); err == nil {
    26  		return os.Open(path)
    27  	}
    28  	return os.Create(path)
    29  }