github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/pathorcontents/read.go (about)

     1  // Helpers for dealing with file paths and their contents
     2  package pathorcontents
     3  
     4  import (
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/mitchellh/go-homedir"
     9  )
    10  
    11  // If the argument is a path, Read loads it and returns the contents,
    12  // otherwise the argument is assumed to be the desired contents and is simply
    13  // returned.
    14  //
    15  // Deprecated: This will be removed in v2 without replacement. If you need
    16  // its functionality, you can copy it or reference the v1 package.
    17  //
    18  // The boolean second return value can be called `wasPath` - it indicates if a
    19  // path was detected and a file loaded.
    20  func Read(poc string) (string, bool, error) {
    21  	if len(poc) == 0 {
    22  		return poc, false, nil
    23  	}
    24  
    25  	path := poc
    26  	if path[0] == '~' {
    27  		var err error
    28  		path, err = homedir.Expand(path)
    29  		if err != nil {
    30  			return path, true, err
    31  		}
    32  	}
    33  
    34  	if _, err := os.Stat(path); err == nil {
    35  		contents, err := ioutil.ReadFile(path)
    36  		if err != nil {
    37  			return string(contents), true, err
    38  		}
    39  		return string(contents), true, nil
    40  	}
    41  
    42  	return poc, false, nil
    43  }