Skip to content

Commit 27de9a9

Browse files
committed
[CLI] Add Get and List CMD for Log
1 parent c4bbe74 commit 27de9a9

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

tools/tkn-results/cmd/logs/get_log.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package logs
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
9+
"github.com/tektoncd/results/tools/tkn-results/internal/flags"
10+
"github.com/tektoncd/results/tools/tkn-results/internal/format"
11+
)
12+
13+
func GetRecordCommand(params *flags.Params) *cobra.Command {
14+
opts := &flags.GetOptions{}
15+
16+
cmd := &cobra.Command{
17+
Use: `get [flags] <record>
18+
19+
<record parent>: Record parent name to query. This is typically "<namespace>/results/<result name>", but may vary depending on the API Server. "-" may be used as <result name> to query all Results for a given parent.`,
20+
Short: "Get Record",
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
resp, err := params.LogsClient.GetLog(cmd.Context(), &pb.GetLogRequest{
23+
Name: args[0],
24+
})
25+
if err != nil {
26+
fmt.Printf("GetLog: %v\n", err)
27+
return err
28+
}
29+
data, err := resp.Recv()
30+
if err != nil {
31+
fmt.Printf("Get Log Client Resp: %v\n", err)
32+
return err
33+
}
34+
return format.PrintProto(os.Stdout, data, opts.Format)
35+
},
36+
Args: cobra.ExactArgs(1),
37+
}
38+
39+
flags.AddGetFlags(opts, cmd)
40+
41+
return cmd
42+
}

tools/tkn-results/cmd/logs/list.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2023 The Tekton Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package logs
16+
17+
import (
18+
"fmt"
19+
"os"
20+
21+
"github.com/spf13/cobra"
22+
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
23+
"github.com/tektoncd/results/tools/tkn-results/internal/flags"
24+
"github.com/tektoncd/results/tools/tkn-results/internal/format"
25+
)
26+
27+
func ListCommand(params *flags.Params) *cobra.Command {
28+
opts := &flags.ListOptions{}
29+
30+
cmd := &cobra.Command{
31+
Use: `list [flags] <result parent>
32+
33+
<result parent>: Result parent name to query. This is typically "<namespace>/results/<result name>", but may vary depending on the API Server. "-" may be used as <result name> to query all Results for a given parent.`,
34+
Short: "List Records",
35+
RunE: func(cmd *cobra.Command, args []string) error {
36+
resp, err := params.LogsClient.ListLogs(cmd.Context(), &pb.ListRecordsRequest{
37+
Parent: args[0],
38+
Filter: opts.Filter,
39+
PageSize: opts.Limit,
40+
PageToken: opts.PageToken,
41+
})
42+
if err != nil {
43+
fmt.Printf("ListRecords: %v\n", err)
44+
return err
45+
}
46+
return format.PrintProto(os.Stdout, resp, opts.Format)
47+
},
48+
Args: cobra.ExactArgs(1),
49+
}
50+
51+
flags.AddListFlags(opts, cmd)
52+
53+
return cmd
54+
}

tools/tkn-results/cmd/logs/logs.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 The Tekton Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Copyright 2023 The Tekton Authors
16+
//
17+
// Licensed under the Apache License, Version 2.0 (the "License");
18+
// you may not use this file except in compliance with the License.
19+
// You may obtain a copy of the License at
20+
//
21+
// http://www.apache.org/licenses/LICENSE-2.0
22+
//
23+
// Unless required by applicable law or agreed to in writing, software
24+
// distributed under the License is distributed on an "AS IS" BASIS,
25+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26+
// See the License for the specific language governing permissions and
27+
// limitations under the License.
28+
29+
package logs
30+
31+
import (
32+
"github.com/spf13/cobra"
33+
"github.com/tektoncd/results/tools/tkn-results/internal/flags"
34+
)
35+
36+
func Command(params *flags.Params) *cobra.Command {
37+
cmd := &cobra.Command{
38+
Use: "logs",
39+
Short: "Command sub-group for querying logs",
40+
}
41+
42+
cmd.AddCommand(ListCommand(params), GetRecordCommand(params))
43+
44+
return cmd
45+
}

tools/tkn-results/cmd/records/get_record.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 The Tekton Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package records
216

317
import (

tools/tkn-results/cmd/root.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88
"github.com/spf13/pflag"
99
"github.com/spf13/viper"
10+
"github.com/tektoncd/results/tools/tkn-results/cmd/logs"
1011
"github.com/tektoncd/results/tools/tkn-results/cmd/records"
1112
"github.com/tektoncd/results/tools/tkn-results/internal/client"
1213
"github.com/tektoncd/results/tools/tkn-results/internal/flags"
@@ -51,7 +52,7 @@ func Root() *cobra.Command {
5152
cmd.PersistentFlags().StringP("addr", "a", "", "Result API server address")
5253
cmd.PersistentFlags().StringP("authtoken", "t", "", "authorization bearer token to use for authenticated requests")
5354

54-
cmd.AddCommand(ListCommand(params), records.Command(params))
55+
cmd.AddCommand(ListCommand(params), records.Command(params), logs.Command(params))
5556

5657
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
5758
viper.BindPFlags(cmd.PersistentFlags())

tools/tkn-results/internal/flags/flags.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
)
77

88
type Params struct {
9-
Client pb.ResultsClient
9+
ResultsClient pb.ResultsClient
10+
LogsClient pb.LogsClient
1011
}
1112

1213
// Options used on commands that list thing
@@ -24,3 +25,13 @@ func AddListFlags(options *ListOptions, cmd *cobra.Command) {
2425
cmd.Flags().StringVarP(&options.PageToken, "page", "p", "", "pagination token to use for next page")
2526
cmd.Flags().StringVarP(&options.Format, "output", "o", "tab", "output format. Valid values: tab|textproto|json")
2627
}
28+
29+
// Options used on commands that list thing
30+
type GetOptions struct {
31+
Format string
32+
}
33+
34+
// This is a helper function that adds common flags for commands that list things
35+
func AddGetFlags(options *GetOptions, cmd *cobra.Command) {
36+
cmd.Flags().StringVarP(&options.Format, "output", "o", "json", "output format. Valid values: textproto|json")
37+
}

0 commit comments

Comments
 (0)