github.com/cmalfait/terraform@v0.11.12-beta1/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 // The boolean second return value can be called `wasPath` - it indicates if a 16 // path was detected and a file loaded. 17 func Read(poc string) (string, bool, error) { 18 if len(poc) == 0 { 19 return poc, false, nil 20 } 21 22 path := poc 23 if path[0] == '~' { 24 var err error 25 path, err = homedir.Expand(path) 26 if err != nil { 27 return path, true, err 28 } 29 } 30 31 if _, err := os.Stat(path); err == nil { 32 contents, err := ioutil.ReadFile(path) 33 if err != nil { 34 return string(contents), true, err 35 } 36 return string(contents), true, nil 37 } 38 39 return poc, false, nil 40 }