Skip to content

Commit

Permalink
Refactor directory structure for the enhanced CLI
Browse files Browse the repository at this point in the history
Signed-off-by: divyansh42 <[email protected]>
  • Loading branch information
divyansh42 committed Mar 7, 2025
1 parent f4a98d7 commit d619b00
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 83 deletions.
2 changes: 1 addition & 1 deletion cmd/cli-docs/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"github.com/tektoncd/results/pkg/cli/cmd"
"log"

"github.com/spf13/cobra/doc"
"github.com/tektoncd/results/pkg/cli/cmd"
)

func main() {
Expand Down
3 changes: 1 addition & 2 deletions cmd/tkn-results/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"context"
"os"

"github.com/tektoncd/results/pkg/cli/cmd"
"os"
)

func main() {
Expand Down
128 changes: 64 additions & 64 deletions pkg/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
_ "embed"
"flag"
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/client"
"github.com/tektoncd/results/pkg/cli/dev/cmd"
"github.com/tektoncd/results/pkg/cli/dev/cmd/logs"
"github.com/tektoncd/results/pkg/cli/dev/cmd/records"
"github.com/tektoncd/results/pkg/cli/dev/config"
"github.com/tektoncd/results/pkg/cli/dev/flags"
"github.com/tektoncd/results/pkg/cli/dev/portforward"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tektoncd/results/pkg/cli/client"
"github.com/tektoncd/results/pkg/cli/cmd/logs"
"github.com/tektoncd/results/pkg/cli/cmd/records"
"github.com/tektoncd/results/pkg/cli/config"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/portforward"

// TODO: Dynamically discover other protos to allow custom record printing.
_ "github.com/tektoncd/results/proto/pipeline/v1/pipeline_go_proto"
)
Expand All @@ -28,56 +28,13 @@ var (
func Root() *cobra.Command {
params := &flags.Params{}
var portForwardCloseChan chan struct{}
cmd := &cobra.Command{
c := &cobra.Command{
Use: "tkn-results",
Short: "tkn CLI plugin for Tekton Results API",
Long: help,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
var overrideAPIAdr string

// Prepare to port-forward if addr config is not set
if cfg := config.GetConfig(); cfg.Portforward && cfg.Address == "" {
portForward, err := portforward.NewPortForward()
if err != nil {
return err
}
// Pick a usable port on localhost for port-forwarding
port, err := portforward.PickFreePort()
if err != nil {
return err
}
overrideAPIAdr = fmt.Sprintf("localhost:%d", port)
portForwardCloseChan = make(chan struct{})
if err = portForward.ForwardPortBackground(portForwardCloseChan, port); err != nil {
return err
}
}

apiClient, err := client.DefaultResultsClient(cmd.Context(), overrideAPIAdr)

if err != nil {
return err
}

params.ResultsClient = apiClient

logClient, err := client.DefaultLogsClient(cmd.Context(), overrideAPIAdr)

if err != nil {
return err
}

params.LogsClient = logClient

pluginLogsClient, err := client.DefaultPluginLogsClient(cmd.Context(), overrideAPIAdr)

if err != nil {
return err
}

params.PluginLogsClient = pluginLogsClient

return nil
PersistentPreRunE: func(c *cobra.Command, _ []string) error {
// this will only run when older commands is being used
return persistentPreRunHandler(c, params, &portForwardCloseChan)
},
PersistentPostRun: func(_ *cobra.Command, _ []string) {
if portForwardCloseChan != nil {
Expand All @@ -89,23 +46,66 @@ func Root() *cobra.Command {
},
}

cmd.PersistentFlags().StringP("addr", "a", "", "Result API server address. If not specified, tkn-result would port-forward to service/tekton-results-api-service automatically")
cmd.PersistentFlags().StringP("authtoken", "t", "", "authorization bearer token to use for authenticated requests")
cmd.PersistentFlags().String("sa", "", "ServiceAccount to use instead of token for authorization and authentication")
cmd.PersistentFlags().String("sa-ns", "", "ServiceAccount Namespace, if not given, it will be taken from current context")
cmd.PersistentFlags().Bool("portforward", true, "enable auto portforwarding to tekton-results-api-service, when addr is set and portforward is true, tkn-results will portforward tekton-results-api-service automatically")
cmd.PersistentFlags().Bool("insecure", false, "determines whether to run insecure GRPC tls request")
cmd.PersistentFlags().Bool("v1alpha2", false, "use v1alpha2 API for get log command")
c.PersistentFlags().StringP("addr", "a", "", "Result API server address. If not specified, tkn-result would port-forward to service/tekton-results-api-service automatically")
c.PersistentFlags().StringP("authtoken", "t", "", "authorization bearer token to use for authenticated requests")
c.PersistentFlags().String("sa", "", "ServiceAccount to use instead of token for authorization and authentication")
c.PersistentFlags().String("sa-ns", "", "ServiceAccount Namespace, if not given, it will be taken from current context")
c.PersistentFlags().Bool("portforward", true, "enable auto portforwarding to tekton-results-api-service, when addr is set and portforward is true, tkn-results will portforward tekton-results-api-service automatically")
c.PersistentFlags().Bool("insecure", false, "determines whether to run insecure GRPC tls request")
c.PersistentFlags().Bool("v1alpha2", false, "use v1alpha2 API for get log command")

cmd.AddCommand(ListCommand(params), records.Command(params), logs.Command(params))
c.AddCommand(cmd.ListCommand(params), records.Command(params), logs.Command(params))

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

err := viper.BindPFlags(cmd.PersistentFlags())
err := viper.BindPFlags(c.PersistentFlags())
if err != nil {
return nil
}
cobra.OnInitialize(config.Init)

return cmd
return c
}

func persistentPreRunHandler(c *cobra.Command, params *flags.Params, portForwardCloseChan *chan struct{}) error {
var overrideAPIAdr string

// Prepare to port-forward if addr config is not set
if cfg := config.GetConfig(); cfg.Portforward && cfg.Address == "" {
portForward, err := portforward.NewPortForward()
if err != nil {
return err
}
// Pick a usable port on localhost for port-forwarding
port, err := portforward.PickFreePort()
if err != nil {
return err
}
overrideAPIAdr = fmt.Sprintf("localhost:%d", port)
*portForwardCloseChan = make(chan struct{})
if err = portForward.ForwardPortBackground(*portForwardCloseChan, port); err != nil {
return err
}
}

// Initialize API clients
apiClient, err := client.DefaultResultsClient(c.Context(), overrideAPIAdr)
if err != nil {
return err
}
params.ResultsClient = apiClient

logClient, err := client.DefaultLogsClient(c.Context(), overrideAPIAdr)
if err != nil {
return err
}
params.LogsClient = logClient

pluginLogsClient, err := client.DefaultPluginLogsClient(c.Context(), overrideAPIAdr)
if err != nil {
return err
}
params.PluginLogsClient = pluginLogsClient

return nil
}
2 changes: 1 addition & 1 deletion pkg/cli/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/format"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/client/client.go → pkg/cli/dev/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/config"
"io"
"os"
"time"

"github.com/tektoncd/results/pkg/cli/config"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
pb3 "github.com/tektoncd/results/proto/v1alpha3/results_go_proto"
"golang.org/x/oauth2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"crypto/ed25519"
"crypto/x509"
"encoding/pem"
"github.com/tektoncd/results/pkg/cli/dev/config"
"math/big"
"os"
"testing"

"github.com/tektoncd/results/pkg/cli/config"
v1 "k8s.io/api/authentication/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/list.go → pkg/cli/dev/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/flags"
"github.com/tektoncd/results/pkg/cli/dev/format"
"os"

"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/cmd/logs/get.go → pkg/cli/dev/cmd/logs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package logs

import (
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/config"
"github.com/tektoncd/results/pkg/cli/dev/flags"
"github.com/tektoncd/results/pkg/cli/dev/format"
"os"
"strings"

Expand All @@ -26,9 +29,6 @@ import (
"github.com/tektoncd/results/pkg/api/server/v1alpha2/log"
"github.com/tektoncd/results/pkg/api/server/v1alpha2/record"
"github.com/tektoncd/results/pkg/api/server/v1alpha2/result"
"github.com/tektoncd/results/pkg/cli/config"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
pb3 "github.com/tektoncd/results/proto/v1alpha3/results_go_proto"
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/logs/list.go → pkg/cli/dev/cmd/logs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package logs

import (
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/flags"
"github.com/tektoncd/results/pkg/cli/dev/format"
"os"

"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cmd/logs/logs.go → pkg/cli/dev/cmd/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package logs

import (
"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/dev/flags"
)

// Command returns a cobra command for `logs` sub commands
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/records/get.go → pkg/cli/dev/cmd/records/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package records

import (
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/flags"
"github.com/tektoncd/results/pkg/cli/dev/format"
"os"

"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package records

import (
"fmt"
"github.com/tektoncd/results/pkg/cli/dev/flags"
"github.com/tektoncd/results/pkg/cli/dev/format"
"os"

"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/format"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package records

import (
"github.com/spf13/cobra"
"github.com/tektoncd/results/pkg/cli/flags"
"github.com/tektoncd/results/pkg/cli/dev/flags"
)

// Command returns a cobra command for `records` sub commands
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d619b00

Please sign in to comment.