Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inspect

import (
"bytes"
"context"
"errors"
"fmt"
"net/url"
Expand All @@ -14,9 +15,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

var (
tanzuNamespace string
)
var tanzuNamespace string

func NewCmdInspect() *cobra.Command {
c := &cobra.Command{
Expand All @@ -37,6 +36,8 @@ Examples:
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
defer cancel()

tanzuCluster := args[0]
tanzuServer := viper.GetString("server")
Expand Down Expand Up @@ -85,7 +86,7 @@ Examples:
tanzuNamespace = conf.Contexts[contextName].Namespace
}

cluster, err := c.Cluster(tanzuNamespace, tanzuCluster)
cluster, err := c.Cluster(ctx, tanzuNamespace, tanzuCluster)
if err != nil {
return err
}
Expand Down
33 changes: 17 additions & 16 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package list

import (
"context"
"errors"
"fmt"
"net/url"
Expand All @@ -16,9 +17,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

var (
tanzuNamespace string
)
var tanzuNamespace string

func NewCmdList() *cobra.Command {
c := &cobra.Command{
Expand Down Expand Up @@ -49,6 +48,8 @@ Examples:
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
defer cancel()

tanzuServer := viper.GetString("server")
tanzuUsername := viper.GetString("username")
Expand Down Expand Up @@ -110,13 +111,13 @@ Examples:
tanzuPassword = string(bytePassword)
fmt.Printf("\n")
}
return listNamespaces(c, tanzuUsername, tanzuPassword)
return listNamespaces(ctx, c, tanzuUsername, tanzuPassword)
case "clusters", "clu", "tkc":
return listClusters(c, tanzuNamespace)
return listClusters(ctx, c, tanzuNamespace)
case "releases", "rel", "tkr":
return listReleases(c)
return listReleases(ctx, c)
case "addons", "tka":
return listAddons(c)
return listAddons(ctx, c)
default:
return fmt.Errorf("%s is not a valid resource", a)
}
Expand All @@ -126,8 +127,8 @@ Examples:
return c
}

func listClusters(c *client.RestClient, ns string) error {
objs, err := c.Clusters(ns)
func listClusters(ctx context.Context, c *client.RestClient, ns string) error {
objs, err := c.Clusters(ctx, ns)
if err != nil {
return err
}
Expand All @@ -139,8 +140,8 @@ func listClusters(c *client.RestClient, ns string) error {
return nil
}

func listReleases(c *client.RestClient) error {
objs, err := c.ReleasesTable()
func listReleases(ctx context.Context, c *client.RestClient) error {
objs, err := c.ReleasesTable(ctx)
if err != nil {
return err
}
Expand All @@ -152,12 +153,12 @@ func listReleases(c *client.RestClient) error {
return nil
}

func listNamespaces(c *client.RestClient, username, password string) error {
err := c.Login(username, password)
func listNamespaces(ctx context.Context, c *client.RestClient, username, password string) error {
err := c.Login(ctx, username, password)
if err != nil {
return err
}
nsList, err := c.Namespaces()
nsList, err := c.Namespaces(ctx)
if err != nil {
return err
}
Expand All @@ -167,8 +168,8 @@ func listNamespaces(c *client.RestClient, username, password string) error {
return nil
}

func listAddons(c *client.RestClient) error {
objs, err := c.AddonsTable()
func listAddons(ctx context.Context, c *client.RestClient) error {
objs, err := c.AddonsTable(ctx)
if err != nil {
return err
}
Expand Down
18 changes: 14 additions & 4 deletions cmd/login/login.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package login

import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/url"
"syscall"
Expand All @@ -23,7 +25,7 @@ var (
func NewCmdLogin() *cobra.Command {
c := &cobra.Command{
Use: "login CLUSTER",
//Args: cobra.MaximumNArgs(1),
// Args: cobra.MaximumNArgs(1),
Args: cobra.MinimumNArgs(0),
Short: "Authenticate user with Tanzu namespaces and clusters",
Long: `Authenticate user with Tanzu namespaces and clusters
Expand Down Expand Up @@ -52,6 +54,7 @@ Examples:
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}

// Read from stdin if password isn't set anywhere
if len(viper.GetString("password")) == 0 {
fmt.Printf("Password:")
Expand All @@ -68,6 +71,8 @@ Examples:
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
defer cancel()

tanzuServer := viper.GetString("server")
tanzuUsername := viper.GetString("username")
Expand All @@ -86,12 +91,12 @@ Examples:
return err
}
c.SetInsecure(insecureSkipVerify)
err = c.Login(tanzuUsername, tanzuPassword)
err = c.Login(ctx, tanzuUsername, tanzuPassword)
if err != nil {
return err
}

ns, err := c.Namespaces()
ns, err := c.Namespaces(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -138,8 +143,12 @@ Examples:
// Range over args and perform login on each of them
for _, arg := range args {
tanzuCluster := arg
res, err := c.LoginCluster(tanzuCluster, tanzuNamespace)
res, err := c.LoginCluster(ctx, tanzuCluster, tanzuNamespace)
if err != nil {
if errors.Is(err, client.ErrClusterNotFound) {
fmt.Printf("Cluster %s does not exist", tanzuCluster)
continue
}
return err
}
caCertData, err := base64.StdEncoding.DecodeString(res.GuestClusterCa)
Expand Down Expand Up @@ -178,6 +187,7 @@ Examples:
return err
}

fmt.Printf("Successfully logged into cluster %s", tanzuCluster)
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"time"

"github.com/middlewaregruppen/tcli/cmd/inspect"
"github.com/middlewaregruppen/tcli/cmd/list"
Expand All @@ -26,6 +27,7 @@ var (
insecureSkipVerify bool
verbosity string
kubeconfig string
timeout time.Duration
)

func init() {
Expand Down Expand Up @@ -65,7 +67,7 @@ func NewDefaultCommand() *cobra.Command {

// Check if kubeconfig exists, create if it doesn't
if _, err := os.Stat(kubeconfig); errors.Is(err, os.ErrNotExist) {
_, err = os.OpenFile(kubeconfig, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
_, err = os.OpenFile(kubeconfig, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o666)
if err != nil {
return err
}
Expand All @@ -84,6 +86,7 @@ func NewDefaultCommand() *cobra.Command {
logrus.Fatal(err)
}
// Setup flags
c.PersistentFlags().DurationVar(&timeout, "timeout", 30*time.Second, "How long to wait for an operation before giving up")
c.PersistentFlags().StringVarP(&verbosity, "verbosity", "v", "info", "number for the log level verbosity (debug, info, warn, error, fatal, panic)")
c.PersistentFlags().StringVarP(&tanzuServer, "server", "s", "", "Address of the server to authenticate against.")
c.PersistentFlags().StringVarP(&tanzuUsername, "username", "u", "", "Username to authenticate.")
Expand Down
Loading