Skip to content

Commit

Permalink
fix: remove need to pass cli.Config to subcommands
Browse files Browse the repository at this point in the history
earlier, `vcluster platform add cluster` had an unintialized config

Signed-off-by: Rohan CJ <rohantmp@gmail.com>
  • Loading branch information
rohantmp committed May 31, 2024
1 parent bf13d5e commit 5d8e2db
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 89 deletions.
4 changes: 1 addition & 3 deletions cmd/vclusterctl/cmd/platform/add/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

managementv1 "github.com/loft-sh/api/v4/pkg/apis/management/v1"
storagev1 "github.com/loft-sh/api/v4/pkg/apis/storage/v1"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/platform/clihelper"
Expand All @@ -32,7 +31,6 @@ import (
type ClusterCmd struct {
Log log.Logger
*flags.GlobalFlags
Cfg *config.CLI
Namespace string
ServiceAccount string
DisplayName string
Expand Down Expand Up @@ -91,7 +89,7 @@ func (cmd *ClusterCmd) Run(ctx context.Context, args []string) error {
// Get clusterName from command argument
clusterName := args[0]

platformClient, err := platform.NewClientFromConfig(ctx, cmd.Cfg)
platformClient, err := platform.NewClientFromConfig(ctx, cmd.LoadedConfig(cmd.Log))
if err != nil {
return fmt.Errorf("new client from path: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/vclusterctl/cmd/platform/backup/backup.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package backup

import (
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/spf13/cobra"
)

// NewAddCmd creates a new command
func NewBackupCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func NewBackupCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
addCmd := &cobra.Command{
Use: "backup",
Short: "Backup subcommands",
Expand All @@ -18,6 +17,6 @@ func NewBackupCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Comman
Args: cobra.NoArgs,
}

addCmd.AddCommand(newManagementCmd(globalFlags, cfg))
addCmd.AddCommand(newManagementCmd(globalFlags))
return addCmd
}
7 changes: 2 additions & 5 deletions cmd/vclusterctl/cmd/platform/backup/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/log"
"github.com/loft-sh/log/survey"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/platform/backup"
Expand All @@ -35,15 +34,13 @@ type ManagementCmd struct {
Namespace string
Filename string
Skip []string
cfg *config.CLI
}

// newManagementCmd creates a new command for backing up the management plane
func newManagementCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func newManagementCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &ManagementCmd{
GlobalFlags: globalFlags,
Log: log.GetInstance(),
cfg: cfg,
}

description := product.ReplaceWithHeader("backup management", `
Expand All @@ -61,7 +58,7 @@ vcluster platform backup management
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, _ []string) error {
// we need to set the project namespace prefix correctly here
_, err := platform.NewClientFromConfig(cobraCmd.Context(), cmd.cfg)
_, err := platform.NewClientFromConfig(cobraCmd.Context(), cmd.LoadedConfig(cmd.Log))
if err != nil {
return fmt.Errorf("create vCluster platform client: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/vclusterctl/cmd/platform/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package connect

import (
"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/spf13/cobra"
)

// NewConnectCmd creates a new cobra command
func NewConnectCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func NewConnectCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
description := product.ReplaceWithHeader("connect", `
Activates a kube context for the given cluster / space / vcluster / management.
Expand All @@ -21,6 +20,6 @@ Activates a kube context for the given cluster / space / vcluster / management.
}

connectCmd.AddCommand(newClusterCmd(globalFlags))
connectCmd.AddCommand(newManagementCmd(globalFlags, cfg))
connectCmd.AddCommand(newManagementCmd(globalFlags))
return connectCmd
}
11 changes: 3 additions & 8 deletions cmd/vclusterctl/cmd/platform/connect/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/platform/kubeconfig"
Expand All @@ -17,20 +16,16 @@ import (
// ManagementCmd holds the cmd flags
type ManagementCmd struct {
*flags.GlobalFlags

Print bool

log log.Logger

cfg *config.CLI
Print bool
}

// NewManagementCmd creates a new command
func newManagementCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func newManagementCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &ManagementCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
cfg: cfg,
}

description := product.ReplaceWithHeader("connect management", `
Expand Down Expand Up @@ -60,7 +55,7 @@ vcluster platform connect management
}

func (cmd *ManagementCmd) run(cobraCmd *cobra.Command) error {
platformClient, err := platform.NewClientFromConfig(cobraCmd.Context(), cmd.cfg)
platformClient, err := platform.NewClientFromConfig(cobraCmd.Context(), cmd.LoadedConfig(cmd.log))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/vclusterctl/cmd/platform/get/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

managementv1 "github.com/loft-sh/api/v4/pkg/apis/management/v1"
"github.com/loft-sh/loftctl/v4/pkg/config"
cliconfig "github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/projectutil"
Expand All @@ -26,13 +26,12 @@ var (

type clusterCmd struct {
*flags.GlobalFlags
cfg *cliconfig.CLI
log log.Logger
}

func newClusterCmd(globalFlags *flags.GlobalFlags, cfg *cliconfig.CLI) *cobra.Command {
func newClusterCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &clusterCmd{
GlobalFlags: globalFlags,
cfg: cfg,
}

return &cobra.Command{
Expand Down Expand Up @@ -61,7 +60,7 @@ func (c *clusterCmd) Run(ctx context.Context, _ []string) error {

isProject, projectName := isProjectContext(cluster)
if isProject {
platformClient, err := platform.NewClientFromConfig(ctx, c.cfg)
platformClient, err := platform.NewClientFromConfig(ctx, c.LoadedConfig(c.log))
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/vclusterctl/cmd/platform/get/cluster_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
util "github.com/loft-sh/vcluster/pkg/platform/loftutils"
Expand All @@ -22,16 +21,14 @@ import (
type ClusterTokenCmd struct {
*flags.GlobalFlags

cfg *config.CLI
log log.Logger
Output string
}

func newClusterAccessKeyCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func newClusterAccessKeyCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &ClusterTokenCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
cfg: cfg,
}
description := product.ReplaceWithHeader("get cluster-access-key", `
Returns the Network Peer Cluster Token
Expand Down Expand Up @@ -61,7 +58,7 @@ vcluster platform get cluster-access-key [CLUSTER_NAME]
func (cmd *ClusterTokenCmd) Run(ctx context.Context, args []string) error {
clusterName := args[0]

platformClient, err := platform.NewClientFromConfig(ctx, cmd.cfg)
platformClient, err := platform.NewClientFromConfig(ctx, cmd.LoadedConfig(cmd.log))
if err != nil {
return fmt.Errorf("new client from path: %w", err)
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/vclusterctl/cmd/platform/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package get

import (
"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform/defaults"
"github.com/spf13/cobra"
)

// NewGetCmd creates a new cobra command for the sub command
func NewGetCmd(globalFlags *flags.GlobalFlags, defaults *defaults.Defaults, cfg *config.CLI) *cobra.Command {
func NewGetCmd(globalFlags *flags.GlobalFlags, defaults *defaults.Defaults) *cobra.Command {
description := product.ReplaceWithHeader("var", "")

cmd := &cobra.Command{
Expand All @@ -19,9 +18,9 @@ func NewGetCmd(globalFlags *flags.GlobalFlags, defaults *defaults.Defaults, cfg
Args: cobra.NoArgs,
}

cmd.AddCommand(newClusterCmd(globalFlags, cfg))
cmd.AddCommand(newClusterAccessKeyCmd(globalFlags, cfg))
cmd.AddCommand(newSecretCmd(globalFlags, defaults, cfg))
cmd.AddCommand(newUserCmd(globalFlags, cfg))
cmd.AddCommand(newClusterCmd(globalFlags))
cmd.AddCommand(newClusterAccessKeyCmd(globalFlags))
cmd.AddCommand(newSecretCmd(globalFlags, defaults))
cmd.AddCommand(newUserCmd(globalFlags))
return cmd
}
8 changes: 2 additions & 6 deletions cmd/vclusterctl/cmd/platform/get/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/loft-sh/log"
"github.com/loft-sh/log/survey"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/platform/set"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
pdefaults "github.com/loft-sh/vcluster/pkg/platform/defaults"
Expand All @@ -38,16 +37,13 @@ type SecretCmd struct {
Project string
Output string
All bool

cfg *config.CLI
}

// newSecretCmd creates a new command
func newSecretCmd(globalFlags *flags.GlobalFlags, defaults *pdefaults.Defaults, cfg *config.CLI) *cobra.Command {
func newSecretCmd(globalFlags *flags.GlobalFlags, defaults *pdefaults.Defaults) *cobra.Command {
cmd := &SecretCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
cfg: cfg,
}
description := product.ReplaceWithHeader("get secret", `
Returns the key value of a project / shared secret.
Expand Down Expand Up @@ -78,7 +74,7 @@ vcluster platform get secret test-secret.key --project myproject

// RunUsers executes the functionality
func (cmd *SecretCmd) Run(ctx context.Context, args []string) error {
platformClient, err := platform.NewClientFromConfig(ctx, cmd.cfg)
platformClient, err := platform.NewClientFromConfig(ctx, cmd.LoadedConfig(cmd.log))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/vclusterctl/cmd/platform/get/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/log"
"github.com/loft-sh/log/table"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/platform/helper"
Expand All @@ -32,7 +31,7 @@ const (
)

// newUserCmd creates a new command
func newUserCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func newUserCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &UserCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
Expand All @@ -50,7 +49,7 @@ vcluster platform get current-user
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, _ []string) error {
return cmd.Run(cobraCmd.Context(), cfg)
return cmd.Run(cobraCmd.Context())
},
}

Expand All @@ -60,8 +59,8 @@ vcluster platform get current-user
}

// RunUsers executes the functionality
func (cmd *UserCmd) Run(ctx context.Context, cfg *config.CLI) error {
baseClient, err := platform.NewClientFromConfig(ctx, cfg)
func (cmd *UserCmd) Run(ctx context.Context) error {
baseClient, err := platform.NewClientFromConfig(ctx, cmd.LoadedConfig(cmd.log))
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/vclusterctl/cmd/platform/list/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/log"
"github.com/loft-sh/log/table"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
Expand All @@ -20,15 +19,13 @@ type ClustersCmd struct {
*flags.GlobalFlags

log log.Logger
cfg *config.CLI
}

// newClustersCmd creates a new spaces command
func newClustersCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func newClustersCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &ClustersCmd{
GlobalFlags: globalFlags,
log: log.GetInstance(),
cfg: cfg,
}
description := product.ReplaceWithHeader("list clusters", `
List the vcluster platform clusters you have access to
Expand All @@ -52,7 +49,7 @@ vcluster platform list clusters

// RunClusters executes the functionality
func (cmd *ClustersCmd) RunClusters(ctx context.Context) error {
platformClient, err := platform.NewClientFromConfig(ctx, cmd.cfg)
platformClient, err := platform.NewClientFromConfig(ctx, cmd.LoadedConfig(cmd.log))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/vclusterctl/cmd/platform/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package list
import (
"github.com/loft-sh/api/v4/pkg/product"

"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/spf13/cobra"
)

// NewListCmd creates a new cobra command
func NewListCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command {
func NewListCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
description := product.ReplaceWithHeader("list", "")
listCmd := &cobra.Command{
Use: "list",
Expand All @@ -18,8 +17,8 @@ func NewListCmd(globalFlags *flags.GlobalFlags, cfg *config.CLI) *cobra.Command
Args: cobra.NoArgs,
}

listCmd.AddCommand(newClustersCmd(globalFlags, cfg))
listCmd.AddCommand(newSharedSecretsCmd(globalFlags, cfg))
listCmd.AddCommand(newTeamsCmd(globalFlags, cfg))
listCmd.AddCommand(newClustersCmd(globalFlags))
listCmd.AddCommand(newSharedSecretsCmd(globalFlags))
listCmd.AddCommand(newTeamsCmd(globalFlags))
return listCmd
}
Loading

0 comments on commit 5d8e2db

Please sign in to comment.