Skip to content

Commit

Permalink
feat(controllers/template): indent JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Sep 12, 2023
1 parent 192bb9c commit ef5fb6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pkg/controllers/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ type Controller struct {
// By default it uses a JSON encoding which can be overriden via WithResourceEncoding.
func New(opts ...containers.Option[Controller]) *Controller {
controller := &Controller{
encoding: &encoding.JSONEncoding[core.Resource]{},
encoding: &encoding.JSONEncoding[core.Resource]{
Prefix: "",
Indent: " ",
},
nsTmpl: template.Must(template.New("ns").
Funcs(funcs).
Parse(defaultNamespaceTmpl),
Expand Down
9 changes: 7 additions & 2 deletions pkg/encoding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ type JSON[T any] struct {
*json.Decoder
}

type JSONEncoding[T any] struct{}
type JSONEncoding[T any] struct {
Prefix string
Indent string
}

func (j JSONEncoding[T]) Extension() string {
return "json"
}

func (j *JSONEncoding[T]) NewEncoder(r io.Writer) TypedEncoder[T] {
return NewJSONEncoder[T](r)
enc := NewJSONEncoder[T](r)
enc.SetIndent(j.Prefix, j.Indent)
return enc
}

func (j *JSONEncoding[T]) NewDecoder(w io.Reader) TypedDecoder[T] {
Expand Down

0 comments on commit ef5fb6e

Please sign in to comment.