@@ -2,9 +2,6 @@ package sops
22
33import (
44 "context"
5- "fmt"
6- "io/ioutil"
7- "path"
85
96 "github.com/hashicorp/terraform-plugin-framework/datasource"
107 "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -71,48 +68,19 @@ func (d *fileDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
7168 return
7269 }
7370
74- sourceFile := config .SourceFile .ValueString ()
75- content , err := ioutil .ReadFile (sourceFile )
71+ data , raw , err := getFileData (config .SourceFile , config .InputType )
7672 if err != nil {
77- resp .Diagnostics .AddError ("Error reading file" , err .Error ())
78- return
79- }
80-
81- var format string
82- if ! config .InputType .IsNull () {
83- format = config .InputType .ValueString ()
84- } else {
85- switch ext := path .Ext (sourceFile ); ext {
86- case ".json" :
87- format = "json"
88- case ".yaml" , ".yml" :
89- format = "yaml"
90- case ".env" :
91- format = "dotenv"
92- case ".ini" :
93- format = "ini"
94- default :
95- resp .Diagnostics .AddError (
96- "Unknown file type" ,
97- fmt .Sprintf ("Don't know how to decode file with extension %s, set input_type as appropriate" , ext ),
98- )
99- return
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 ())
10077 }
101- }
102-
103- if err := validateInputType (format ); err != nil {
104- resp .Diagnostics .AddError ("Invalid input type" , err .Error ())
105- return
106- }
107-
108- data , raw , err := readData (content , format )
109- if err != nil {
110- resp .Diagnostics .AddError ("Error reading data" , err .Error ())
11178 return
11279 }
11380
11481 m , mapDiags := types .MapValueFrom (ctx , types .StringType , data )
11582 resp .Diagnostics .Append (mapDiags ... )
83+
11684 if resp .Diagnostics .HasError () {
11785 return
11886 }
0 commit comments