helper: deprecate pathorcontents

pathorcontents was solely used by the gcs backend. I moved the function
into the backend package so it could still be used by other backends for
good measure.
This commit is contained in:
Kristin Laemmert 2020-10-07 12:55:43 -04:00
parent 04be220f5f
commit 6f4f6fbc72
4 changed files with 43 additions and 73 deletions

View File

@ -7,6 +7,8 @@ package backend
import ( import (
"context" "context"
"errors" "errors"
"io/ioutil"
"os"
"time" "time"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
@ -20,6 +22,7 @@ import (
"github.com/hashicorp/terraform/states/statemgr" "github.com/hashicorp/terraform/states/statemgr"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags" "github.com/hashicorp/terraform/tfdiags"
"github.com/mitchellh/go-homedir"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
) )
@ -287,3 +290,31 @@ const (
func (r OperationResult) ExitStatus() int { func (r OperationResult) ExitStatus() int {
return int(r) return int(r)
} }
// 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
}

View File

@ -1,4 +1,4 @@
package pathorcontents package backend
import ( import (
"io" "io"
@ -11,8 +11,7 @@ import (
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
) )
func TestRead_Path(t *testing.T) { func TestReadPathOrContents_Path(t *testing.T) {
isPath := true
f, cleanup := testTempFile(t) f, cleanup := testTempFile(t)
defer cleanup() defer cleanup()
@ -21,21 +20,17 @@ func TestRead_Path(t *testing.T) {
} }
f.Close() f.Close()
contents, wasPath, err := Read(f.Name()) contents, err := ReadPathOrContents(f.Name())
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if wasPath != isPath {
t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
}
if contents != "foobar" { if contents != "foobar" {
t.Fatalf("expected contents %s, got %s", "foobar", contents) t.Fatalf("expected contents %s, got %s", "foobar", contents)
} }
} }
func TestRead_TildePath(t *testing.T) { func TestReadPathOrContents_TildePath(t *testing.T) {
isPath := true
home, err := homedir.Dir() home, err := homedir.Dir()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -50,14 +45,11 @@ func TestRead_TildePath(t *testing.T) {
r := strings.NewReplacer(home, "~") r := strings.NewReplacer(home, "~")
homePath := r.Replace(f.Name()) homePath := r.Replace(f.Name())
contents, wasPath, err := Read(homePath) contents, err := ReadPathOrContents(homePath)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if wasPath != isPath {
t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
}
if contents != "foobar" { if contents != "foobar" {
t.Fatalf("expected contents %s, got %s", "foobar", contents) t.Fatalf("expected contents %s, got %s", "foobar", contents)
} }
@ -72,7 +64,6 @@ func TestRead_PathNoPermission(t *testing.T) {
t.Skip("This test is invalid when running as root, since root can read every file") t.Skip("This test is invalid when running as root, since root can read every file")
} }
isPath := true
f, cleanup := testTempFile(t) f, cleanup := testTempFile(t)
defer cleanup() defer cleanup()
@ -85,48 +76,37 @@ func TestRead_PathNoPermission(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
contents, wasPath, err := Read(f.Name()) contents, err := ReadPathOrContents(f.Name())
if err == nil { if err == nil {
t.Fatal("Expected error, got none!") t.Fatal("Expected error, got none!")
} }
if wasPath != isPath {
t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
}
if contents != "" { if contents != "" {
t.Fatalf("expected contents %s, got %s", "", contents) t.Fatalf("expected contents %s, got %s", "", contents)
} }
} }
func TestRead_Contents(t *testing.T) { func TestReadPathOrContents_Contents(t *testing.T) {
isPath := false
input := "hello" input := "hello"
contents, wasPath, err := Read(input) contents, err := ReadPathOrContents(input)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if wasPath != isPath {
t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
}
if contents != input { if contents != input {
t.Fatalf("expected contents %s, got %s", input, contents) t.Fatalf("expected contents %s, got %s", input, contents)
} }
} }
func TestRead_TildeContents(t *testing.T) { func TestReadPathOrContents_TildeContents(t *testing.T) {
isPath := false
input := "~/hello/notafile" input := "~/hello/notafile"
contents, wasPath, err := Read(input) contents, err := ReadPathOrContents(input)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if wasPath != isPath {
t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath)
}
if contents != input { if contents != input {
t.Fatalf("expected contents %s, got %s", input, contents) t.Fatalf("expected contents %s, got %s", input, contents)
} }

View File

@ -11,7 +11,6 @@ import (
"cloud.google.com/go/storage" "cloud.google.com/go/storage"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/httpclient" "github.com/hashicorp/terraform/httpclient"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@ -148,7 +147,7 @@ func (b *Backend) configure(ctx context.Context) error {
var account accountFile var account accountFile
// to mirror how the provider works, we accept the file path or the contents // to mirror how the provider works, we accept the file path or the contents
contents, _, err := pathorcontents.Read(creds) contents, err := backend.ReadPathOrContents(creds)
if err != nil { if err != nil {
return fmt.Errorf("Error loading credentials: %s", err) return fmt.Errorf("Error loading credentials: %s", err)
} }
@ -183,7 +182,7 @@ func (b *Backend) configure(ctx context.Context) error {
} }
if key != "" { if key != "" {
kc, _, err := pathorcontents.Read(key) kc, err := backend.ReadPathOrContents(key)
if err != nil { if err != nil {
return fmt.Errorf("Error loading encryption key: %s", err) return fmt.Errorf("Error loading encryption key: %s", err)
} }

View File

@ -1,40 +0,0 @@
// Helpers for dealing with file paths and their contents
package pathorcontents
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.
//
// The boolean second return value can be called `wasPath` - it indicates if a
// path was detected and a file loaded.
func Read(poc string) (string, bool, error) {
if len(poc) == 0 {
return poc, false, nil
}
path := poc
if path[0] == '~' {
var err error
path, err = homedir.Expand(path)
if err != nil {
return path, true, err
}
}
if _, err := os.Stat(path); err == nil {
contents, err := ioutil.ReadFile(path)
if err != nil {
return string(contents), true, err
}
return string(contents), true, nil
}
return poc, false, nil
}