1515package records
1616
1717import (
18- "bytes"
19- "encoding/json"
2018 "fmt"
21- "io"
22- "text/tabwriter"
23- "text/template"
19+ "os"
2420
21+ "github.com/spf13/cobra"
2522 "github.com/tektoncd/results/pkg/cli/dev/flags"
2623 "github.com/tektoncd/results/pkg/cli/dev/format"
27-
28- "github.com/jonboulle/clockwork"
29- "github.com/spf13/cobra"
3024 pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
3125)
3226
33- const tmpl = `Name: {{.Record.Name}}
34- UID: {{.Record.Uid}}
35- Status:
36- Created: {{ formatAge .Record.CreateTime .Time }} Duration: {{formatDuration .Record.CreateTime .Record.UpdateTime}}
37- {{- if .Record.Data }}
38- Type: {{.Record.Data.Type}}
39- Data:
40- {{formatJSON .Record.Data.Value}}
41- {{ end -}}
42- `
43-
4427// GetRecordCommand returns a cobra sub command that will fetch a record by name
4528func GetRecordCommand (params * flags.Params ) * cobra.Command {
4629 opts := & flags.GetOptions {}
@@ -50,14 +33,14 @@ func GetRecordCommand(params *flags.Params) *cobra.Command {
5033 Short : "[To be deprecated] Get Record by <record-name>" ,
5134 Long : "Get Record by <record-name>. <record-name> is typically of format <namespace>/results/<parent-run-uuid>/records/<child-run-uuid>" ,
5235 RunE : func (cmd * cobra.Command , args []string ) error {
53- record , err := params .ResultsClient .GetRecord (cmd .Context (), & pb.GetRecordRequest {
36+ resp , err := params .ResultsClient .GetRecord (cmd .Context (), & pb.GetRecordRequest {
5437 Name : args [0 ],
5538 })
5639 if err != nil {
5740 fmt .Printf ("GetRecord: %v\n " , err )
5841 return err
5942 }
60- return formatRecord ( cmd . OutOrStdout (), record , params . Clock )
43+ return format . PrintProto ( os . Stdout , resp , opts . Format )
6144 },
6245 Args : cobra .ExactArgs (1 ),
6346 Annotations : map [string ]string {
@@ -82,31 +65,3 @@ func GetRecordCommand(params *flags.Params) *cobra.Command {
8265
8366 return cmd
8467}
85-
86- func formatRecord (out io.Writer , record * pb.Record , c clockwork.Clock ) error {
87- data := struct {
88- Record * pb.Record
89- Time clockwork.Clock
90- }{
91- Record : record ,
92- Time : c ,
93- }
94-
95- funcMap := template.FuncMap {
96- "formatAge" : format .Age ,
97- "formatDuration" : format .Duration ,
98- "formatJSON" : func (data []byte ) string {
99- if len (data ) == 0 {
100- return "No data"
101- }
102- var prettyJSON bytes.Buffer
103- if err := json .Indent (& prettyJSON , data , " " , " " ); err != nil {
104- return fmt .Sprintf ("Error formatting JSON: %v" , err )
105- }
106- return prettyJSON .String ()
107- },
108- }
109- w := tabwriter .NewWriter (out , 0 , 5 , 3 , ' ' , tabwriter .TabIndent )
110- t := template .Must (template .New ("record" ).Funcs (funcMap ).Parse (tmpl ))
111- return t .Execute (w , data )
112- }
0 commit comments