mirror of https://github.com/hashicorp/terraform
Package backend previously had a ReadPathOrContents helper that takes a string that could represent either a filename or some literal content, and then reads the given file if it's a filename or just returns the literal content otherwise. We originally used variations of this helper to shim a number of different situations that had originally accepted filenames but later changed to accept literal content, but over time we've eliminated all of those and so the only remaining caller is the gcs backend which uses it to accept GCP credentials either as literal content or as a file containing the credentials. Therefore here we move that helper function to be just an unexported part of the gcs backend package. This is part of an ongoing effort to minimize what each remote state backend depends on (either directly or indirectly) so that we can make gradual progress towards these remote state backends being self-contained enough to move into other codebases. In the short term though, it just puts this helper function closer to its caller so it's easier to maintain them both together if needed.pull/34827/head
parent
a2a982206c
commit
914618b061
@ -0,0 +1,39 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package gcs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
)
|
||||
|
||||
// If the argument is a path, Read loads it and returns the contents,
|
||||
// otherwise the argument is assumed to be the desired contents and is simply
|
||||
// returned.
|
||||
func readPathOrContents(poc string) (string, error) {
|
||||
if len(poc) == 0 {
|
||||
return poc, nil
|
||||
}
|
||||
|
||||
path := poc
|
||||
if path[0] == '~' {
|
||||
var err error
|
||||
path, err = homedir.Expand(path)
|
||||
if err != nil {
|
||||
return path, err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
contents, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return string(contents), err
|
||||
}
|
||||
return string(contents), nil
|
||||
}
|
||||
|
||||
return poc, nil
|
||||
}
|
||||
Loading…
Reference in new issue