Skip to content

Commit

Permalink
fix: set-file was not reading file content
Browse files Browse the repository at this point in the history
  • Loading branch information
micovery committed Dec 11, 2024
1 parent 471d392 commit 0bfd616
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/flags/setfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type SetFile struct {
Data *values.Map
}

func NewSetFile(data *values.Map) SetString {
return SetString{Data: data}
func NewSetFile(data *values.Map) SetFile {
return SetFile{Data: data}
}

func (v *SetFile) Type() string {
Expand All @@ -48,6 +48,6 @@ func (v *SetFile) Set(entry string) error {
return errors.New(err)
}

v.Data.Set(key, fileText)
v.Data.Set(key, string(fileText))
return nil
}
21 changes: 21 additions & 0 deletions pkg/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"testing"
)

Expand All @@ -32,27 +33,39 @@ func TestRenderGeneric(t *testing.T) {
templateFile string
valuesFile string
includesFile string
setFileFlag string
wantErr error
}{
{
"using-files",
"input.yaml",
"",
"",
"",
nil,
},
{
"using-helpers",
"input.yaml",
"",
"_helpers.tmpl",
"",
nil,
},
{
"policies",
"apiproxy.yaml",
"values.yaml",
"",
"",
nil,
},
{
"set-file",
"input.yaml",
"",
"",
"data=./data.json",
nil,
},
}
Expand Down Expand Up @@ -87,6 +100,14 @@ func TestRenderGeneric(t *testing.T) {
require.NoError(t, err)
}

if tt.setFileFlag != "" {
key, filePath, _ := strings.Cut(tt.setFileFlag, "=")
tt.setFileFlag = fmt.Sprintf("%s=%s", key, path.Join(testDir, filePath))
f := flags.NewSetFile(cFlags.Values)
err := f.Set(tt.setFileFlag)
require.NoError(t, err)
}

err = RenderGenericTemplate(cFlags, false)

if tt.wantErr != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/render/testdata/render/set-file/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"name": "hello world"
}
]
15 changes: 15 additions & 0 deletions pkg/render/testdata/render/set-file/exp-input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: hello world
16 changes: 16 additions & 0 deletions pkg/render/testdata/render/set-file/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#{{ $data := index ($.Values.data | fromJson) 0 }}
name: {{ $data.name }}

0 comments on commit 0bfd616

Please sign in to comment.