go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/cliutil/file_or_stdin.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package cliutil
     9  
    10  import (
    11  	"io"
    12  	"os"
    13  	"strings"
    14  )
    15  
    16  // FileOrStdin reads a file or from standard input if the file
    17  // path is the literal string "-".
    18  func FileOrStdin(path string) ([]byte, error) {
    19  	if strings.TrimSpace(path) == "-" {
    20  		return io.ReadAll(os.Stdin)
    21  	}
    22  	return os.ReadFile(path)
    23  }