11package template
22
33import (
4- "bytes"
5- "context"
64 _ "embed"
75 "encoding/json"
86 "fmt"
9- "io"
107 "os"
118
129 "github.com/goccy/go-yaml"
@@ -30,7 +27,7 @@ type inputModel struct {
3027}
3128
3229//go:embed template.json
33- var template [] byte
30+ var template string
3431
3532func NewCmd (p * print.Printer ) * cobra.Command {
3633 cmd := & cobra.Command {
@@ -49,33 +46,25 @@ func NewCmd(p *print.Printer) *cobra.Command {
4946 ),
5047 ),
5148 RunE : func (cmd * cobra.Command , _ []string ) error {
52- ctx := context .Background ()
5349 model , err := parseInput (p , cmd )
5450 if err != nil {
5551 return err
5652 }
5753
58- var reader io.Reader
5954 if model .Format == nil || * model .Format == "json" {
60- reader = bytes . NewReader (template )
55+ p . Outputln (template )
6156 } else if * model .Format == "yaml" {
6257 var target alb.CreateLoadBalancerPayload
63- if err := json .Unmarshal (template , & target ); err != nil {
58+ if err := json .Unmarshal ([] byte ( template ) , & target ); err != nil {
6459 return fmt .Errorf ("cannot unmarshal template: %w" , err )
6560 }
66- data , err := yaml .Marshal ( & target )
67- if err != nil {
61+ encoder := yaml .NewEncoder ( os . Stdout , yaml . IndentSequence ( true ), yaml . UseJSONMarshaler () )
62+ if encoder . Encode ( target ); err != nil {
6863 return fmt .Errorf ("cannot marshal template to yaml: %w" , err )
6964 }
70- reader = bytes .NewReader (data )
7165 } else {
7266 return fmt .Errorf ("invalid format %q defined. Must be 'json' or 'yaml'" , * model .Format )
7367 }
74- if _ , err := io .Copy (os .Stdout , reader ); err != nil {
75- return fmt .Errorf ("cannot write output: %w" , err )
76- }
77-
78- _ , _ = ctx , model
7968
8069 return nil
8170 },
0 commit comments