Skip to content

Commit

Permalink
fix: ensure resource output template has a newline (#161)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza authored Jul 8, 2024
1 parent 1685dbf commit c8ff86b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions internal/command/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"log/slog"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
Expand Down Expand Up @@ -95,6 +96,10 @@ be returned as json.
case "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(outputs)
default:
// ensure there is a new line at the end if one is not already present
if !strings.HasSuffix(formatValue, "\n") {
formatValue += "\n"
}
prepared, err := template.New("").Funcs(sprig.FuncMap()).Parse(formatValue)
if err != nil {
return fmt.Errorf("failed to parse format template: %w", err)
Expand Down
11 changes: 10 additions & 1 deletion internal/command/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -111,18 +112,26 @@ volume.default#example.vol
assert.NoError(t, err)
var out map[string]interface{}
assert.NoError(t, json.Unmarshal([]byte(stdout), &out))
assert.True(t, strings.HasSuffix(stdout, "\n"))
})

t.Run("format yaml", func(t *testing.T) {
stdout, _, err := executeAndResetCommand(context.Background(), rootCmd, []string{"resources", "get-outputs", "volume.default#example.vol", "--format", "yaml"})
assert.NoError(t, err)
var out map[string]interface{}
assert.NoError(t, yaml.Unmarshal([]byte(stdout), &out))
assert.True(t, strings.HasSuffix(stdout, "\n"))
})

t.Run("format template", func(t *testing.T) {
stdout, _, err := executeAndResetCommand(context.Background(), rootCmd, []string{"resources", "get-outputs", "volume.default#example.vol", "--format", `{{ . | len }}`})
assert.NoError(t, err)
assert.Equal(t, "2", stdout)
assert.Equal(t, "2\n", stdout)
})

t.Run("format template with newline", func(t *testing.T) {
stdout, _, err := executeAndResetCommand(context.Background(), rootCmd, []string{"resources", "get-outputs", "volume.default#example.vol", "--format", "{{ . | len }}\n"})
assert.NoError(t, err)
assert.Equal(t, "2\n", stdout)
})
}

0 comments on commit c8ff86b

Please sign in to comment.