Skip to content

Commit 5ecb9ae

Browse files
committed
chore: extract sops_external logic
1 parent d596f38 commit 5ecb9ae

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

sops/data.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io/ioutil"
66
"path"
7+
"strings"
78

89
"github.com/hashicorp/terraform-plugin-framework/types"
910
)
@@ -59,3 +60,22 @@ func getFileData(sourceFile types.String, inputType types.String) (data map[stri
5960
}
6061
return data, raw, nil
6162
}
63+
64+
func getExternalData(source types.String, inputType types.String) (data map[string]string, raw string, err error) {
65+
content, err := ioutil.ReadAll(strings.NewReader(source.ValueString()))
66+
if err != nil {
67+
return nil, "", newSummaryError("Error reading source", err)
68+
}
69+
70+
format := inputType.ValueString()
71+
if err := validateInputType(format); err != nil {
72+
return nil, "", newSummaryError("Invalid input type", err)
73+
}
74+
75+
data, raw, err = readData(content, format)
76+
if err != nil {
77+
return nil, "", newSummaryError("Error reading data", err)
78+
}
79+
80+
return data, raw, nil
81+
}

sops/data_sops_external.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package sops
22

33
import (
44
"context"
5-
"io/ioutil"
6-
"strings"
75

86
"github.com/hashicorp/terraform-plugin-framework/datasource"
97
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -70,22 +68,13 @@ func (d *externalDataSource) Read(ctx context.Context, req datasource.ReadReques
7068
return
7169
}
7270

73-
source := config.Source.ValueString()
74-
content, err := ioutil.ReadAll(strings.NewReader(source))
71+
data, raw, err := getExternalData(config.Source, config.InputType)
7572
if err != nil {
76-
resp.Diagnostics.AddError("Error reading source", err.Error())
77-
return
78-
}
79-
80-
format := config.InputType.ValueString()
81-
if err := validateInputType(format); err != nil {
82-
resp.Diagnostics.AddError("Invalid input type", err.Error())
83-
return
84-
}
85-
86-
data, raw, err := readData(content, format)
87-
if err != nil {
88-
resp.Diagnostics.AddError("Error reading data", err.Error())
73+
if detailedErr, ok := err.(summaryError); ok {
74+
resp.Diagnostics.AddError(detailedErr.Summary, detailedErr.Err.Error())
75+
} else {
76+
resp.Diagnostics.AddError("Failed to decrypt file", err.Error())
77+
}
8978
return
9079
}
9180

sops/ephemeral_sops_external.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package sops
22

33
import (
44
"context"
5-
"io/ioutil"
6-
"strings"
75

86
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
97
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema"
@@ -65,22 +63,13 @@ func (d *externalEphemeralResource) Open(ctx context.Context, req ephemeral.Open
6563
return
6664
}
6765

68-
source := config.Source.ValueString()
69-
content, err := ioutil.ReadAll(strings.NewReader(source))
66+
data, raw, err := getExternalData(config.Source, config.InputType)
7067
if err != nil {
71-
resp.Diagnostics.AddError("Error reading source", err.Error())
72-
return
73-
}
74-
75-
format := config.InputType.ValueString()
76-
if err := validateInputType(format); err != nil {
77-
resp.Diagnostics.AddError("Invalid input type", err.Error())
78-
return
79-
}
80-
81-
data, raw, err := readData(content, format)
82-
if err != nil {
83-
resp.Diagnostics.AddError("Error reading data", err.Error())
68+
if detailedErr, ok := err.(summaryError); ok {
69+
resp.Diagnostics.AddError(detailedErr.Summary, detailedErr.Err.Error())
70+
} else {
71+
resp.Diagnostics.AddError("Failed to decrypt file", err.Error())
72+
}
8473
return
8574
}
8675

0 commit comments

Comments
 (0)