From 72c81b3e6e106a975adef8c9aba2beb0251c2f35 Mon Sep 17 00:00:00 2001 From: Soumik Majumder Date: Wed, 19 Jan 2022 01:05:54 +0530 Subject: [PATCH 01/16] Add flag to view controller version. TODO: Give more thought to how we wanna deal with kapp-controller installed in non-default namespaces --- cli/pkg/kctrl/cmd/kctrl.go | 2 +- cli/pkg/kctrl/cmd/version.go | 49 +++++++++++++++++++++++-- cli/test/e2e/controller_version_test.go | 20 ++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 cli/test/e2e/controller_version_test.go diff --git a/cli/pkg/kctrl/cmd/kctrl.go b/cli/pkg/kctrl/cmd/kctrl.go index be4a12cf0..c6c17a3ae 100644 --- a/cli/pkg/kctrl/cmd/kctrl.go +++ b/cli/pkg/kctrl/cmd/kctrl.go @@ -74,7 +74,7 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm ConfigurePathResolvers(o, cmd, flagsFactory) - cmd.AddCommand(NewVersionCmd(NewVersionOptions(o.ui), flagsFactory)) + cmd.AddCommand(NewVersionCmd(NewVersionOptions(o.ui, o.depsFactory), flagsFactory)) pkgCmd := cmdpkg.NewCmd() AddPackageCommands(o, pkgCmd, flagsFactory, false) diff --git a/cli/pkg/kctrl/cmd/version.go b/cli/pkg/kctrl/cmd/version.go index 53aa8a4ab..2e80fcb54 100644 --- a/cli/pkg/kctrl/cmd/version.go +++ b/cli/pkg/kctrl/cmd/version.go @@ -4,20 +4,32 @@ package cmd import ( + "context" "fmt" "github.com/cppforlife/go-cli-ui/ui" "github.com/spf13/cobra" cmdcore "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/cmd/core" "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/version" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + kappControllerNamespace = "kapp-controller" + kappControllerDeployment = "kapp-controller" + kappControllerVersionAnn = "kapp-controller.carvel.dev/version" ) type VersionOptions struct { - ui ui.UI + ui ui.UI + depsFactory cmdcore.DepsFactory + + controllerVersion bool } -func NewVersionOptions(ui ui.UI) *VersionOptions { - return &VersionOptions{ui} +func NewVersionOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *VersionOptions { + return &VersionOptions{ui: ui, depsFactory: depsFactory} } func NewVersionCmd(o *VersionOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -26,11 +38,42 @@ func NewVersionCmd(o *VersionOptions, flagsFactory cmdcore.FlagsFactory) *cobra. Short: "Print client version", RunE: func(_ *cobra.Command, _ []string) error { return o.Run() }, } + + cmd.Flags().BoolVar(&o.controllerVersion, "controller", false, "Get the version of kapp-controller deployed on the cluster") + return cmd } func (o *VersionOptions) Run() error { + if o.controllerVersion { + err := o.showControllerVersion() + if err != nil { + return err + } + return nil + } + o.ui.PrintBlock([]byte(fmt.Sprintf("kctrl version %s\n", version.Version))) return nil } + +func (o *VersionOptions) showControllerVersion() error { + coreClient, err := o.depsFactory.CoreClient() + if err != nil { + return err + } + + controllerDeployment, err := coreClient.AppsV1().Deployments(kappControllerNamespace).Get(context.Background(), kappControllerDeployment, metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return fmt.Errorf("kapp-controller not installed on cluster") + } + + return err + } + + o.ui.PrintBlock([]byte(fmt.Sprintf("kapp controller version %s\n", controllerDeployment.GetAnnotations()[kappControllerVersionAnn]))) + + return nil +} diff --git a/cli/test/e2e/controller_version_test.go b/cli/test/e2e/controller_version_test.go new file mode 100644 index 000000000..f28c93bea --- /dev/null +++ b/cli/test/e2e/controller_version_test.go @@ -0,0 +1,20 @@ +// Copyright 2020 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package e2e + +import ( + "strings" + "testing" +) + +func TestControllerVersion(t *testing.T) { + env := BuildEnv(t) + kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, Logger{}} + + out, _ := kappCtrl.RunWithOpts([]string{"version", "--controller"}, RunOpts{NoNamespace: true}) + + if !strings.Contains(out, "kapp controller version") { + t.Fatalf("Expected to find controller version") + } +} From c4a74286c53e1cabf506ac69b30f6addfc2c9bef Mon Sep 17 00:00:00 2001 From: Soumik Majumder Date: Wed, 19 Jan 2022 01:53:52 +0530 Subject: [PATCH 02/16] Support StdIn input while adding values file --- cli/pkg/kctrl/cmd/core/file_sources.go | 46 +++++++++++++++++++ .../cmd/package/installed/create_or_update.go | 6 +-- 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 cli/pkg/kctrl/cmd/core/file_sources.go diff --git a/cli/pkg/kctrl/cmd/core/file_sources.go b/cli/pkg/kctrl/cmd/core/file_sources.go new file mode 100644 index 000000000..f6e03b788 --- /dev/null +++ b/cli/pkg/kctrl/cmd/core/file_sources.go @@ -0,0 +1,46 @@ +// Copyright 2020 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package core + +import ( + "io/ioutil" + "os" +) + +type InputFile struct { + source FileSource +} + +func NewInputFile(file string) InputFile { + switch file { + case "-": + return InputFile{source: NewStdinSource()} + default: + return InputFile{source: NewLocalFileSource(file)} + } +} + +func (f InputFile) Bytes() ([]byte, error) { + return f.source.Bytes() +} + +type FileSource interface { + Bytes() ([]byte, error) +} + +type StdinSource struct{} + +var _ FileSource = StdinSource{} + +func NewStdinSource() StdinSource { return StdinSource{} } +func (s StdinSource) Bytes() ([]byte, error) { return ioutil.ReadAll(os.Stdin) } + +type LocalFileSource struct { + path string +} + +var _ FileSource = LocalFileSource{} + +func NewLocalFileSource(path string) LocalFileSource { return LocalFileSource{path} } +func (s LocalFileSource) Bytes() ([]byte, error) { return ioutil.ReadFile(s.path) } diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index 8b20732cb..b4ab49c06 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -6,7 +6,6 @@ package installed import ( "context" "fmt" - "io/ioutil" "time" "github.com/cppforlife/go-cli-ui/ui" @@ -449,7 +448,7 @@ func (o *CreateOrUpdateOptions) createOrUpdateDataValuesSecret(client kubernetes dataValues := make(map[string][]byte) - dataValues[valuesFileKey], err = ioutil.ReadFile(o.valuesFile) + dataValues[valuesFileKey], err = cmdcore.NewInputFile(o.valuesFile).Bytes() if err != nil { return false, fmt.Errorf("Reading data values file '%s': %s", o.valuesFile, err.Error()) } @@ -652,7 +651,8 @@ func (o *CreateOrUpdateOptions) updateDataValuesSecret(client kubernetes.Interfa } } - if dataValues[dataKey], err = ioutil.ReadFile(o.valuesFile); err != nil { + dataValues[dataKey], err = cmdcore.NewInputFile(o.valuesFile).Bytes() + if err != nil { return fmt.Errorf("Reading data values file '%s': %s", o.valuesFile, err.Error()) } secret := &corev1.Secret{ From d48ec59e7ae38d077c27afa43207ab4cebfd7801 Mon Sep 17 00:00:00 2001 From: Soumik Majumder Date: Wed, 19 Jan 2022 01:59:52 +0530 Subject: [PATCH 03/16] Rephrasing 'kapp controller' => 'kapp-controller' while displaying controller version --- cli/pkg/kctrl/cmd/version.go | 2 +- cli/test/e2e/controller_version_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/pkg/kctrl/cmd/version.go b/cli/pkg/kctrl/cmd/version.go index 2e80fcb54..7ecafd382 100644 --- a/cli/pkg/kctrl/cmd/version.go +++ b/cli/pkg/kctrl/cmd/version.go @@ -73,7 +73,7 @@ func (o *VersionOptions) showControllerVersion() error { return err } - o.ui.PrintBlock([]byte(fmt.Sprintf("kapp controller version %s\n", controllerDeployment.GetAnnotations()[kappControllerVersionAnn]))) + o.ui.PrintBlock([]byte(fmt.Sprintf("kapp-controller version %s\n", controllerDeployment.GetAnnotations()[kappControllerVersionAnn]))) return nil } diff --git a/cli/test/e2e/controller_version_test.go b/cli/test/e2e/controller_version_test.go index f28c93bea..9e6f7f714 100644 --- a/cli/test/e2e/controller_version_test.go +++ b/cli/test/e2e/controller_version_test.go @@ -14,7 +14,7 @@ func TestControllerVersion(t *testing.T) { out, _ := kappCtrl.RunWithOpts([]string{"version", "--controller"}, RunOpts{NoNamespace: true}) - if !strings.Contains(out, "kapp controller version") { + if !strings.Contains(out, "kapp-controller version") { t.Fatalf("Expected to find controller version") } } From fe43d3b79adc526602388d29668ae3622dcf7710 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 13:25:49 +0530 Subject: [PATCH 04/16] Use structured examples in package available get --- cli/pkg/kctrl/cmd/core/examples.go | 32 +++++++++++++++++++ cli/pkg/kctrl/cmd/package/available/get.go | 16 ++++++---- cli/pkg/kctrl/cmd/package/available/list.go | 34 ++++++++++++--------- 3 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 cli/pkg/kctrl/cmd/core/examples.go diff --git a/cli/pkg/kctrl/cmd/core/examples.go b/cli/pkg/kctrl/cmd/core/examples.go new file mode 100644 index 000000000..c74a120ac --- /dev/null +++ b/cli/pkg/kctrl/cmd/core/examples.go @@ -0,0 +1,32 @@ +package core + +import ( + "fmt" + "strings" +) + +type Example struct { + Description string + Args []string +} + +func (e Example) AsString(binaryName, nameFlag string, positionalNameArg bool) string { + command := binaryName + for _, arg := range e.Args { + if positionalNameArg && arg == nameFlag { + continue + } + command += " " + arg + } + return fmt.Sprintf("# %s \n%s", e.Description, command) +} + +type Examples []Example + +func (es Examples) Description(binaryName, nameFlag string, positionalNameArg bool) string { + var description string + for _, example := range es { + description += example.AsString(binaryName, nameFlag, positionalNameArg) + "\n\n" + } + return strings.TrimSuffix(description, "\n\n") +} diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index bef511037..cc0428fff 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -38,17 +38,21 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Get details about an available package", + []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com"}, + }, + cmdcore.Example{"Get the values schema for a particular version of the package", + []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}, + }) + cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for an available package or the openAPI schema of a package with a specific version", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Get details about an available package -kctrl package available get --package cert-manager.community.tanzu.vmware.com - -# Get the values schema for a particular version of the package -kctrl package available get --package cert-manager.community.tanzu.vmware.com --values-schema`, + Example: examples.Description("kctrl", "-p", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index 8958c882c..b2d20ffad 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -37,26 +37,30 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"List packages available on the cluster", + []string{"package", "available", "list"}, + }, + cmdcore.Example{"List packages available on the cluster with their short descriptions", + []string{"package", "available", "list", "--wide"}, + }, + cmdcore.Example{"List all available package versions with release dates", + []string{"package", "available", "list", "--summary=false"}, + }, + cmdcore.Example{"List packages available in all namespaces", + []string{"package", "available", "list", "-A"}, + }, + cmdcore.Example{"List all available versions of a package", + []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}, + }) + cmd := &cobra.Command{ Use: "list", Aliases: []string{"l", "ls"}, Short: "List available packages in a namespace", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# List packages available on the cluster -kctrl package available list - -# List packages available on the cluster with their short descriptions -kctrl package available list --wide - -# List all available package versions with release dates -kctrl package available list --summary=false - -# List packages available in all namespaces -kctrl package available list -A - -# List all available versions of a package -kctrl package available --package cert-manager.community.tanzu.vmware.com`, + Example: examples.Description("kctrl", "-p", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List available packages in all namespaces") From e76ba8e1742676e5ed2e207f962bc0baede65060 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:20:59 +0530 Subject: [PATCH 05/16] Use structured examples in package installed --- cli/pkg/kctrl/cmd/package/available/list.go | 2 +- .../cmd/package/installed/create_or_update.go | 78 +++++++++++-------- cli/pkg/kctrl/cmd/package/installed/delete.go | 15 ++-- cli/pkg/kctrl/cmd/package/installed/get.go | 22 +++--- cli/pkg/kctrl/cmd/package/installed/list.go | 16 ++-- 5 files changed, 78 insertions(+), 55 deletions(-) diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index b2d20ffad..b96b33101 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -39,7 +39,7 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { var examples cmdcore.Examples examples = append(examples, - cmdcore.Example{"List packages available on the cluster", + cmdcore.Example{"List packages available on the cluster", []string{"package", "available", "list"}, }, cmdcore.Example{"List packages available on the cluster with their short descriptions", diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index b4ab49c06..cf24709f5 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -57,19 +57,23 @@ func NewCreateOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger } func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - cmd := &cobra.Command{ - Use: "create", - Short: "Install package", - RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, - Example: ` -# Install a package -kctrl package installed create -i cert-man -p cert-manager.community.tanzu.vmware.com --version 1.6.1 - -# Install package with values file -kctrl package installed create -i cert-man -p cert-manager.community.tanzu.vmware.com --version 1.6.1 --values-file values.yml + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Install a package", + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1"}, + }, + cmdcore.Example{"Install package with values file", + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, + }, + cmdcore.Example{"Install package and ask it to use an existing service account", + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}, + }) -# Install package and ask it to use an existing service account -kctrl package installed create -i cert-man -p cert-manager.community.tanzu.vmware.com --version 1.6.1 --service-account-name existing-sa`, + cmd := &cobra.Command{ + Use: "create", + Short: "Install package", + RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, + Example: examples.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -92,19 +96,23 @@ kctrl package installed create -i cert-man -p cert-manager.community.tanzu.vmwar } func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - cmd := &cobra.Command{ - Use: "install", - Short: "Install package", - RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, - Example: ` -# Install a package -kctrl package install -i cert-man -p cert-manager.community.tanzu.vmware.com --version 1.6.1 - -# Install package with values file -kctrl package install -i cert-man -p cert-manager.community.tanzu.vmware.com --version 1.6.1 --values-file values.yml + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Install a package", + []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1"}, + }, + cmdcore.Example{"Install package with values file", + []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, + }, + cmdcore.Example{"Install package and ask it to use an existing service account", + []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}, + }) -# Install package and ask it to use an existing service account -kctrl package install -i cert-man -p cert-manager.community.tanzu.vmware.com --version 1.6.1 --service-account-name existing-sa`, + cmd := &cobra.Command{ + Use: "install", + Short: "Install package", + RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, + Example: examples.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -127,16 +135,20 @@ kctrl package install -i cert-man -p cert-manager.community.tanzu.vmware.com --v } func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Upgrade package install to a newer version", + []string{"package", "installed", "update", "-i", "cert-man", "--version", "1.6.2"}, + }, + cmdcore.Example{"Update package install with new values file", + []string{"package", "installed", "update", "-i", "cert-man", "--values-file", "values.yml"}, + }) + cmd := &cobra.Command{ - Use: "update", - Short: "Update package", - RunE: func(_ *cobra.Command, args []string) error { return o.RunUpdate(args) }, - Example: ` -# Upgrade package install to a newer version -kctrl package installed update -i cert-man --version 1.6.1 - -#Update package install with new values file -kctrl package installed update -i --values-file updated-values.yml`, + Use: "update", + Short: "Update package", + RunE: func(_ *cobra.Command, args []string) error { return o.RunUpdate(args) }, + Example: examples.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 6a00d98a8..22996e8d8 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -43,13 +43,16 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + + example := cmdcore.Example{"Delete package install", + []string{"package", "installed", "delete", "-i", "cert-man"}, + } + cmd := &cobra.Command{ - Use: "delete", - Short: "Uninstall installed package", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Delete package install -kctrl package installed delete -i cert-man`, + Use: "delete", + Short: "Uninstall installed package", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: example.AsString("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 3132eb755..7ecef0191 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -37,20 +37,24 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Get details for a package install", + []string{"package", "installed", "get", "-i", "cert-man"}, + }, + cmdcore.Example{"View values being used by package install", + []string{"package", "installed", "get", "-i", "cert-man", "--values"}, + }, + cmdcore.Example{"Download values being used by package install", + []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}, + }) + cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for installed package", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Get details for a package install -kctrl package installed get -i cert-man - -# View values being used by package install -kctrl package installed get -i cert-man --values - -# Download values being used by package install -kctrl package installed get -i cert-man --values-file-output `, + Example: examples.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index f3a9b6917..0fc34af45 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -29,17 +29,21 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"List installed packages", + []string{"package", "installed", "list"}, + }, + cmdcore.Example{"List installed packages in all namespaces", + []string{"package", "installed", "list", "A"}, + }) + cmd := &cobra.Command{ Use: "list", Aliases: []string{"l", "ls"}, Short: "List installed packages in a namespace", RunE: func(_ *cobra.Command, _ []string) error { return o.Run() }, - Example: ` -# List installed packages -kctrl package installed list - -# List installed packages in all namespaces -kctrl package installed list -A`, + Example: examples.Description("kctrl", "", false), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List installed packages in all namespaces") From 199118d2531dcf970caa0c75a2848b3c29dc69d0 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:36:13 +0530 Subject: [PATCH 06/16] Use structured examples in package repository --- cli/pkg/kctrl/cmd/package/available/get.go | 3 +-- cli/pkg/kctrl/cmd/package/available/list.go | 3 +-- .../cmd/package/installed/create_or_update.go | 3 +-- cli/pkg/kctrl/cmd/package/installed/delete.go | 3 +-- cli/pkg/kctrl/cmd/package/installed/get.go | 3 +-- cli/pkg/kctrl/cmd/package/installed/list.go | 3 +-- .../cmd/package/repository/add_or_update.go | 26 ++++++++++--------- .../kctrl/cmd/package/repository/delete.go | 13 +++++----- cli/pkg/kctrl/cmd/package/repository/get.go | 7 ++--- cli/pkg/kctrl/cmd/package/repository/list.go | 15 ++++++----- 10 files changed, 40 insertions(+), 39 deletions(-) diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index cc0428fff..9d6e062f6 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -44,8 +44,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com"}, }, cmdcore.Example{"Get the values schema for a particular version of the package", - []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}, - }) + []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}) cmd := &cobra.Command{ Use: "get", diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index b96b33101..ef3ee8f12 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -52,8 +52,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman []string{"package", "available", "list", "-A"}, }, cmdcore.Example{"List all available versions of a package", - []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}, - }) + []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}) cmd := &cobra.Command{ Use: "list", diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index cf24709f5..b0f642772 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -66,8 +66,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, }, cmdcore.Example{"Install package and ask it to use an existing service account", - []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}, - }) + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}) cmd := &cobra.Command{ Use: "create", diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 22996e8d8..f098c2a95 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -45,8 +45,7 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { example := cmdcore.Example{"Delete package install", - []string{"package", "installed", "delete", "-i", "cert-man"}, - } + []string{"package", "installed", "delete", "-i", "cert-man"}} cmd := &cobra.Command{ Use: "delete", diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 7ecef0191..47cf93cf3 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -46,8 +46,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command []string{"package", "installed", "get", "-i", "cert-man", "--values"}, }, cmdcore.Example{"Download values being used by package install", - []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}, - }) + []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}) cmd := &cobra.Command{ Use: "get", diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index 0fc34af45..599b126ca 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -35,8 +35,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman []string{"package", "installed", "list"}, }, cmdcore.Example{"List installed packages in all namespaces", - []string{"package", "installed", "list", "A"}, - }) + []string{"package", "installed", "list", "A"}}) cmd := &cobra.Command{ Use: "list", diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index b04f12e33..8e1b209d5 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -45,13 +45,14 @@ func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger log } func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + example := cmdcore.Example{"Add a package repository", + []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}} + cmd := &cobra.Command{ - Use: "add", - Short: "Add a package repository", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Add a package repository -kctrl package repository add -r tce --url projects.registry.vmware.com/tce/main:0.9.1`, + Use: "add", + Short: "Add a package repository", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: example.AsString("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -76,13 +77,14 @@ kctrl package repository add -r tce --url projects.registry.vmware.com/tce/main: } func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + example := cmdcore.Example{"Update a package repository with a new URL", + []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}} + cmd := &cobra.Command{ - Use: "update", - Short: "Update a package repository", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Update a package repository with a new URL -kctrl package repository update -r tce --url projects.registry.vmware.com/tce/main:0.9.1`, + Use: "update", + Short: "Update a package repository", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: example.AsString("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index d3a057381..9d766dbf1 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -38,13 +38,14 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + example := cmdcore.Example{"Delete a package repository", + []string{"package", "repository", "delete", "-r", "tce"}} + cmd := &cobra.Command{ - Use: "delete", - Short: "Delete a package repository", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Delete a package repository -kctrl package repository delete -r tce`, + Use: "delete", + Short: "Delete a package repository", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: example.AsString("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index 15932e4f3..79961a907 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -31,14 +31,15 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + example := cmdcore.Example{"Get details for a package repository", + []string{"package", "repository", "get", "-r", "tce"}} + cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for a package repository", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: ` -# Get details for a package repository -kctrl package repository get -r tce`, + Example: example.AsString("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/list.go b/cli/pkg/kctrl/cmd/package/repository/list.go index f9e96db04..1127ee2ba 100644 --- a/cli/pkg/kctrl/cmd/package/repository/list.go +++ b/cli/pkg/kctrl/cmd/package/repository/list.go @@ -30,17 +30,20 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"List package repositories", + []string{"package", "repository", "list"}, + }, + cmdcore.Example{"List package repositories in all namespaces", + []string{"package", "repository", "list", "A"}}) + cmd := &cobra.Command{ Use: "list", Aliases: []string{"l", "ls"}, Short: "List package repositories in a namespace", RunE: func(_ *cobra.Command, _ []string) error { return o.Run() }, - Example: ` -# List package repositories -kctrl package repository list - -# List package repositories in all namespaces -kctrl package repository list -A`, + Example: examples.Description("kctrl", "", false), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List repositories in all namespaces") From 9aca4a5db6e5cba48f7bc1b012042d26020525b5 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 15:45:58 +0530 Subject: [PATCH 07/16] Make example asString private --- cli/pkg/kctrl/cmd/core/examples.go | 4 ++-- cli/pkg/kctrl/cmd/package/installed/delete.go | 9 +++++---- .../cmd/package/repository/add_or_update.go | 16 ++++++++++------ cli/pkg/kctrl/cmd/package/repository/delete.go | 8 +++++--- cli/pkg/kctrl/cmd/package/repository/get.go | 8 +++++--- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/cli/pkg/kctrl/cmd/core/examples.go b/cli/pkg/kctrl/cmd/core/examples.go index c74a120ac..12cee249b 100644 --- a/cli/pkg/kctrl/cmd/core/examples.go +++ b/cli/pkg/kctrl/cmd/core/examples.go @@ -10,7 +10,7 @@ type Example struct { Args []string } -func (e Example) AsString(binaryName, nameFlag string, positionalNameArg bool) string { +func (e Example) asString(binaryName, nameFlag string, positionalNameArg bool) string { command := binaryName for _, arg := range e.Args { if positionalNameArg && arg == nameFlag { @@ -26,7 +26,7 @@ type Examples []Example func (es Examples) Description(binaryName, nameFlag string, positionalNameArg bool) string { var description string for _, example := range es { - description += example.AsString(binaryName, nameFlag, positionalNameArg) + "\n\n" + description += example.asString(binaryName, nameFlag, positionalNameArg) + "\n\n" } return strings.TrimSuffix(description, "\n\n") } diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index f098c2a95..26bf67c90 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -43,15 +43,16 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - - example := cmdcore.Example{"Delete package install", - []string{"package", "installed", "delete", "-i", "cert-man"}} + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Delete package install", + []string{"package", "installed", "delete", "-i", "cert-man"}}) cmd := &cobra.Command{ Use: "delete", Short: "Uninstall installed package", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: example.AsString("kctrl", "-i", o.positionalNameArg), + Example: examples.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index 8e1b209d5..c7bf474cf 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -45,14 +45,16 @@ func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger log } func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - example := cmdcore.Example{"Add a package repository", - []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}} + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Add a package repository", + []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}}) cmd := &cobra.Command{ Use: "add", Short: "Add a package repository", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: example.AsString("kctrl", "-r", o.positionalNameArg), + Example: examples.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -77,14 +79,16 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. } func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - example := cmdcore.Example{"Update a package repository with a new URL", - []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}} + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Update a package repository with a new URL", + []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}}) cmd := &cobra.Command{ Use: "update", Short: "Update a package repository", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: example.AsString("kctrl", "-r", o.positionalNameArg), + Example: examples.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index 9d766dbf1..76b8b6b00 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -38,14 +38,16 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - example := cmdcore.Example{"Delete a package repository", - []string{"package", "repository", "delete", "-r", "tce"}} + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Delete a package repository", + []string{"package", "repository", "delete", "-r", "tce"}}) cmd := &cobra.Command{ Use: "delete", Short: "Delete a package repository", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: example.AsString("kctrl", "-r", o.positionalNameArg), + Example: examples.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index 79961a907..060d67bee 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -31,15 +31,17 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - example := cmdcore.Example{"Get details for a package repository", - []string{"package", "repository", "get", "-r", "tce"}} + var examples cmdcore.Examples + examples = append(examples, + cmdcore.Example{"Get details for a package repository", + []string{"package", "repository", "get", "-r", "tce"}}) cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for a package repository", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: example.AsString("kctrl", "-r", o.positionalNameArg), + Example: examples.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) From fb91b3d764fbe18076a2c072c4de4ac2d68bf05d Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 17:34:41 +0530 Subject: [PATCH 08/16] Update example usage to be inline --- cli/pkg/kctrl/cmd/package/available/get.go | 16 ++-- cli/pkg/kctrl/cmd/package/available/list.go | 34 ++++---- .../cmd/package/installed/create_or_update.go | 80 +++++++++---------- cli/pkg/kctrl/cmd/package/installed/delete.go | 16 ++-- cli/pkg/kctrl/cmd/package/installed/get.go | 22 +++-- cli/pkg/kctrl/cmd/package/installed/list.go | 16 ++-- .../cmd/package/repository/add_or_update.go | 32 ++++---- .../kctrl/cmd/package/repository/delete.go | 16 ++-- cli/pkg/kctrl/cmd/package/repository/get.go | 10 +-- cli/pkg/kctrl/cmd/package/repository/list.go | 16 ++-- 10 files changed, 115 insertions(+), 143 deletions(-) diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index 9d6e062f6..47d14ec45 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -38,20 +38,18 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Get details about an available package", - []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com"}, - }, - cmdcore.Example{"Get the values schema for a particular version of the package", - []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}) - cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for an available package or the openAPI schema of a package with a specific version", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-p", o.positionalNameArg), + Example: cmdcore.Examples{ + cmdcore.Example{"Get details about an available package", + []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com"}, + }, + cmdcore.Example{"Get the values schema for a particular version of the package", + []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}, + }.Description("kctrl", "-p", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index ef3ee8f12..a7a88343c 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -37,29 +37,27 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"List packages available on the cluster", - []string{"package", "available", "list"}, - }, - cmdcore.Example{"List packages available on the cluster with their short descriptions", - []string{"package", "available", "list", "--wide"}, - }, - cmdcore.Example{"List all available package versions with release dates", - []string{"package", "available", "list", "--summary=false"}, - }, - cmdcore.Example{"List packages available in all namespaces", - []string{"package", "available", "list", "-A"}, - }, - cmdcore.Example{"List all available versions of a package", - []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}) - cmd := &cobra.Command{ Use: "list", Aliases: []string{"l", "ls"}, Short: "List available packages in a namespace", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-p", o.positionalNameArg), + Example: cmdcore.Examples{ + cmdcore.Example{"List packages available on the cluster", + []string{"package", "available", "list"}, + }, + cmdcore.Example{"List packages available on the cluster with their short descriptions", + []string{"package", "available", "list", "--wide"}, + }, + cmdcore.Example{"List all available package versions with release dates", + []string{"package", "available", "list", "--summary=false"}, + }, + cmdcore.Example{"List packages available in all namespaces", + []string{"package", "available", "list", "-A"}, + }, + cmdcore.Example{"List all available versions of a package", + []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}, + }.Description("kctrl", "-p", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List available packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index b0f642772..ff69630f9 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -57,22 +57,20 @@ func NewCreateOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger } func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Install a package", - []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1"}, - }, - cmdcore.Example{"Install package with values file", - []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, - }, - cmdcore.Example{"Install package and ask it to use an existing service account", - []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}) - cmd := &cobra.Command{ - Use: "create", - Short: "Install package", - RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, - Example: examples.Description("kctrl", "-i", o.positionalNameArg), + Use: "create", + Short: "Install package", + RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Install a package", + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1"}, + }, + cmdcore.Example{"Install package with values file", + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, + }, + cmdcore.Example{"Install package and ask it to use an existing service account", + []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, + }.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -95,23 +93,20 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * } func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Install a package", - []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1"}, - }, - cmdcore.Example{"Install package with values file", - []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, - }, - cmdcore.Example{"Install package and ask it to use an existing service account", - []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}, - }) - cmd := &cobra.Command{ - Use: "install", - Short: "Install package", - RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, - Example: examples.Description("kctrl", "-i", o.positionalNameArg), + Use: "install", + Short: "Install package", + RunE: func(_ *cobra.Command, args []string) error { return o.RunCreate(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Install a package", + []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1"}, + }, + cmdcore.Example{"Install package with values file", + []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--values-file", "values.yml"}, + }, + cmdcore.Example{"Install package and ask it to use an existing service account", + []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, + }.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -134,20 +129,17 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) } func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Upgrade package install to a newer version", - []string{"package", "installed", "update", "-i", "cert-man", "--version", "1.6.2"}, - }, - cmdcore.Example{"Update package install with new values file", - []string{"package", "installed", "update", "-i", "cert-man", "--values-file", "values.yml"}, - }) - cmd := &cobra.Command{ - Use: "update", - Short: "Update package", - RunE: func(_ *cobra.Command, args []string) error { return o.RunUpdate(args) }, - Example: examples.Description("kctrl", "-i", o.positionalNameArg), + Use: "update", + Short: "Update package", + RunE: func(_ *cobra.Command, args []string) error { return o.RunUpdate(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Upgrade package install to a newer version", + []string{"package", "installed", "update", "-i", "cert-man", "--version", "1.6.2"}, + }, + cmdcore.Example{"Update package install with new values file", + []string{"package", "installed", "update", "-i", "cert-man", "--values-file", "values.yml"}}, + }.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 26bf67c90..6760149c4 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -43,16 +43,14 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Delete package install", - []string{"package", "installed", "delete", "-i", "cert-man"}}) - cmd := &cobra.Command{ - Use: "delete", - Short: "Uninstall installed package", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-i", o.positionalNameArg), + Use: "delete", + Short: "Uninstall installed package", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Delete package install", + []string{"package", "installed", "delete", "-i", "cert-man"}}, + }.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 47cf93cf3..a8b857633 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -37,23 +37,21 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Get details for a package install", - []string{"package", "installed", "get", "-i", "cert-man"}, - }, - cmdcore.Example{"View values being used by package install", - []string{"package", "installed", "get", "-i", "cert-man", "--values"}, - }, - cmdcore.Example{"Download values being used by package install", - []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}) - cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for installed package", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-i", o.positionalNameArg), + Example: cmdcore.Examples{ + cmdcore.Example{"Get details for a package install", + []string{"package", "installed", "get", "-i", "cert-man"}, + }, + cmdcore.Example{"View values being used by package install", + []string{"package", "installed", "get", "-i", "cert-man", "--values"}, + }, + cmdcore.Example{"Download values being used by package install", + []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}, + }.Description("kctrl", "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index 599b126ca..d682d3bcf 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -29,20 +29,18 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"List installed packages", - []string{"package", "installed", "list"}, - }, - cmdcore.Example{"List installed packages in all namespaces", - []string{"package", "installed", "list", "A"}}) - cmd := &cobra.Command{ Use: "list", Aliases: []string{"l", "ls"}, Short: "List installed packages in a namespace", RunE: func(_ *cobra.Command, _ []string) error { return o.Run() }, - Example: examples.Description("kctrl", "", false), + Example: cmdcore.Examples{ + cmdcore.Example{"List installed packages", + []string{"package", "installed", "list"}, + }, + cmdcore.Example{"List installed packages in all namespaces", + []string{"package", "installed", "list", "A"}}, + }.Description("kctrl", "", false), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List installed packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index c7bf474cf..500fd4ef9 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -45,16 +45,14 @@ func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger log } func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Add a package repository", - []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}}) - cmd := &cobra.Command{ - Use: "add", - Short: "Add a package repository", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-r", o.positionalNameArg), + Use: "add", + Short: "Add a package repository", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Add a package repository", + []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}}, + }.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -79,16 +77,14 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. } func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Update a package repository with a new URL", - []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}}) - cmd := &cobra.Command{ - Use: "update", - Short: "Update a package repository", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-r", o.positionalNameArg), + Use: "update", + Short: "Update a package repository", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Update a package repository with a new URL", + []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}}, + }.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index 76b8b6b00..118c78243 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -38,16 +38,14 @@ func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.L } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Delete a package repository", - []string{"package", "repository", "delete", "-r", "tce"}}) - cmd := &cobra.Command{ - Use: "delete", - Short: "Delete a package repository", - RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-r", o.positionalNameArg), + Use: "delete", + Short: "Delete a package repository", + RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, + Example: cmdcore.Examples{ + cmdcore.Example{"Delete a package repository", + []string{"package", "repository", "delete", "-r", "tce"}}, + }.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index 060d67bee..a610328e4 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -31,17 +31,15 @@ func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logg } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"Get details for a package repository", - []string{"package", "repository", "get", "-r", "tce"}}) - cmd := &cobra.Command{ Use: "get", Aliases: []string{"g"}, Short: "Get details for a package repository", RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, - Example: examples.Description("kctrl", "-r", o.positionalNameArg), + Example: cmdcore.Examples{ + cmdcore.Example{"Get details for a package repository", + []string{"package", "repository", "get", "-r", "tce"}}, + }.Description("kctrl", "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/list.go b/cli/pkg/kctrl/cmd/package/repository/list.go index 1127ee2ba..db246a4d4 100644 --- a/cli/pkg/kctrl/cmd/package/repository/list.go +++ b/cli/pkg/kctrl/cmd/package/repository/list.go @@ -30,20 +30,18 @@ func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Log } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { - var examples cmdcore.Examples - examples = append(examples, - cmdcore.Example{"List package repositories", - []string{"package", "repository", "list"}, - }, - cmdcore.Example{"List package repositories in all namespaces", - []string{"package", "repository", "list", "A"}}) - cmd := &cobra.Command{ Use: "list", Aliases: []string{"l", "ls"}, Short: "List package repositories in a namespace", RunE: func(_ *cobra.Command, _ []string) error { return o.Run() }, - Example: examples.Description("kctrl", "", false), + Example: cmdcore.Examples{ + cmdcore.Example{"List package repositories", + []string{"package", "repository", "list"}, + }, + cmdcore.Example{"List package repositories in all namespaces", + []string{"package", "repository", "list", "A"}}, + }.Description("kctrl", "", false), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List repositories in all namespaces") From 3769b982cf9f4d612a4685221909a956dbb21d70 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 19:25:01 +0530 Subject: [PATCH 09/16] Update copyright information --- cli/pkg/kctrl/cmd/core/examples.go | 3 +++ cli/pkg/kctrl/cmd/core/semver_value.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cli/pkg/kctrl/cmd/core/examples.go b/cli/pkg/kctrl/cmd/core/examples.go index 12cee249b..dc15063ef 100644 --- a/cli/pkg/kctrl/cmd/core/examples.go +++ b/cli/pkg/kctrl/cmd/core/examples.go @@ -1,3 +1,6 @@ +// Copyright 2022 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + package core import ( diff --git a/cli/pkg/kctrl/cmd/core/semver_value.go b/cli/pkg/kctrl/cmd/core/semver_value.go index f9144f22e..8ed4279e0 100644 --- a/cli/pkg/kctrl/cmd/core/semver_value.go +++ b/cli/pkg/kctrl/cmd/core/semver_value.go @@ -1,4 +1,4 @@ -// Copyright 20210 VMware, Inc. +// Copyright 2021 VMware, Inc. // SPDX-License-Identifier: Apache-2.0 package core From 07c6ed324cf53a3ac5bb4316c49e0c691815a4b9 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Fri, 4 Feb 2022 20:05:58 +0530 Subject: [PATCH 10/16] Use PackageCommandTreeOpts to set binaryName Use PackageCommandTreeOpts to set binaryName and positionalNameArgs and enable or disable color or json flags --- cli/pkg/kctrl/cmd/kctrl.go | 57 +++++++++++-------- cli/pkg/kctrl/cmd/package/available/get.go | 7 ++- cli/pkg/kctrl/cmd/package/available/list.go | 7 ++- .../cmd/package/installed/create_or_update.go | 11 ++-- cli/pkg/kctrl/cmd/package/installed/delete.go | 7 ++- cli/pkg/kctrl/cmd/package/installed/get.go | 7 ++- cli/pkg/kctrl/cmd/package/installed/list.go | 8 ++- .../cmd/package/repository/add_or_update.go | 9 +-- .../kctrl/cmd/package/repository/delete.go | 7 ++- cli/pkg/kctrl/cmd/package/repository/get.go | 7 ++- cli/pkg/kctrl/cmd/package/repository/list.go | 8 ++- cli/pkg/kctrl/cmd/ui_flags.go | 10 +++- 12 files changed, 85 insertions(+), 60 deletions(-) diff --git a/cli/pkg/kctrl/cmd/kctrl.go b/cli/pkg/kctrl/cmd/kctrl.go index c6c17a3ae..ad3a34ac3 100644 --- a/cli/pkg/kctrl/cmd/kctrl.go +++ b/cli/pkg/kctrl/cmd/kctrl.go @@ -70,14 +70,16 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm cmdcore.RestOfCommandsHelpGroup, })) - SetGlobalFlags(o, cmd, flagsFactory) + pkgOpts := PackageCommandTreeOpts{"kctrl", false, true, true} + + SetGlobalFlags(o, cmd, flagsFactory, pkgOpts) ConfigurePathResolvers(o, cmd, flagsFactory) cmd.AddCommand(NewVersionCmd(NewVersionOptions(o.ui, o.depsFactory), flagsFactory)) pkgCmd := cmdpkg.NewCmd() - AddPackageCommands(o, pkgCmd, flagsFactory, false) + AddPackageCommands(o, pkgCmd, flagsFactory, pkgOpts) cmd.AddCommand(pkgCmd) @@ -86,8 +88,8 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm return cmd } -func SetGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory) { - o.UIFlags.Set(cmd, flagsFactory) +func SetGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { + o.UIFlags.Set(cmd, flagsFactory, opts) o.LoggerFlags.Set(cmd, flagsFactory) o.KubeAPIFlags.Set(cmd, flagsFactory) o.KubeconfigFlags.Set(cmd, flagsFactory) @@ -128,45 +130,45 @@ func ConfigureGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdc } } -func AddPackageCommands(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, positionalNameArg bool) { +func AddPackageCommands(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { pkgrepoCmd := pkgrepo.NewCmd() - pkgrepoCmd.AddCommand(pkgrepo.NewListCmd(pkgrepo.NewListOptions(o.ui, o.depsFactory, o.logger), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewGetCmd(pkgrepo.NewGetOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewDeleteCmd(pkgrepo.NewDeleteOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewAddCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewUpdateCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewListCmd(pkgrepo.NewListOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewGetCmd(pkgrepo.NewGetOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewDeleteCmd(pkgrepo.NewDeleteOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewAddCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewUpdateCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) pkgiCmd := pkginst.NewCmd() - pkgiCmd.AddCommand(pkginst.NewListCmd(pkginst.NewListOptions(o.ui, o.depsFactory, o.logger), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewGetCmd(pkginst.NewGetOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewCreateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewUpdateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewDeleteCmd(pkginst.NewDeleteOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewListCmd(pkginst.NewListOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewGetCmd(pkginst.NewGetOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewCreateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewUpdateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewDeleteCmd(pkginst.NewDeleteOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) pkgaCmd := pkgavail.NewCmd() - pkgaCmd.AddCommand(pkgavail.NewListCmd(pkgavail.NewListOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) - pkgaCmd.AddCommand(pkgavail.NewGetCmd(pkgavail.NewGetOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) + pkgaCmd.AddCommand(pkgavail.NewListCmd(pkgavail.NewListOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgaCmd.AddCommand(pkgavail.NewGetCmd(pkgavail.NewGetOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) cmd.AddCommand(pkgrepoCmd) cmd.AddCommand(pkgiCmd) cmd.AddCommand(pkgaCmd) - cmd.AddCommand(pkginst.NewInstallCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, positionalNameArg), flagsFactory)) + cmd.AddCommand(pkginst.NewInstallCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) } -func AttachGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, positionalNameArg bool) { - SetGlobalFlags(o, cmd, flagsFactory) +func AttachGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { + SetGlobalFlags(o, cmd, flagsFactory, opts) ConfigurePathResolvers(o, cmd, flagsFactory) - ConfigureGlobalFlags(o, cmd, flagsFactory, positionalNameArg) + ConfigureGlobalFlags(o, cmd, flagsFactory, opts.PositionalArgs) } -func AttachKctrlPackageCommandTree(cmd *cobra.Command, confUI *ui.ConfUI) { +func AttachKctrlPackageCommandTree(cmd *cobra.Command, confUI *ui.ConfUI, opts PackageCommandTreeOpts) { configFactory := cmdcore.NewConfigFactoryImpl() depsFactory := cmdcore.NewDepsFactoryImpl(configFactory, confUI) options := NewKctrlOptions(confUI, configFactory, depsFactory) flagsFactory := cmdcore.NewFlagsFactory(configFactory, depsFactory) - AddPackageCommands(options, cmd, flagsFactory, true) - AttachGlobalFlags(options, cmd, flagsFactory, true) + AddPackageCommands(options, cmd, flagsFactory, opts) + AttachGlobalFlags(options, cmd, flagsFactory, opts) } type uiBlockWriter struct { @@ -179,3 +181,10 @@ func (w uiBlockWriter) Write(p []byte) (n int, err error) { w.ui.PrintBlock(p) return len(p), nil } + +type PackageCommandTreeOpts struct { + BinaryName string + PositionalArgs bool + Color bool + JSON bool +} diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index 47d14ec45..e7517d843 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -30,11 +30,12 @@ type GetOptions struct { ValuesSchema bool + binaryName string positionalNameArg bool } -func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *GetOptions { - return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *GetOptions { + return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -49,7 +50,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command }, cmdcore.Example{"Get the values schema for a particular version of the package", []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}, - }.Description("kctrl", "-p", o.positionalNameArg), + }.Description(o.binaryName, "-p", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index a7a88343c..86752aa35 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -29,11 +29,12 @@ type ListOptions struct { Summary bool Wide bool + binaryName string positionalNameArg bool } -func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *ListOptions { - return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *ListOptions { + return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -57,7 +58,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman }, cmdcore.Example{"List all available versions of a package", []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}, - }.Description("kctrl", "-p", o.positionalNameArg), + }.Description(o.binaryName, "-p", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List available packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index ff69630f9..44e2bc746 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -49,11 +49,12 @@ type CreateOrUpdateOptions struct { NamespaceFlags cmdcore.NamespaceFlags createdAnnotations *CreatedResourceAnnotations + binaryName string positionalNameArg bool } -func NewCreateOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *CreateOrUpdateOptions { - return &CreateOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewCreateOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *CreateOrUpdateOptions { + return &CreateOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -70,7 +71,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * }, cmdcore.Example{"Install package and ask it to use an existing service account", []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, - }.Description("kctrl", "-i", o.positionalNameArg), + }.Description(o.binaryName, "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -106,7 +107,7 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) }, cmdcore.Example{"Install package and ask it to use an existing service account", []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, - }.Description("kctrl", "-i", o.positionalNameArg), + }.Description(o.binaryName, "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -139,7 +140,7 @@ func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * }, cmdcore.Example{"Update package install with new values file", []string{"package", "installed", "update", "-i", "cert-man", "--values-file", "values.yml"}}, - }.Description("kctrl", "-i", o.positionalNameArg), + }.Description(o.binaryName, "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 6760149c4..77b92b790 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -35,11 +35,12 @@ type DeleteOptions struct { WaitFlags cmdcore.WaitFlags + binaryName string positionalNameArg bool } -func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *DeleteOptions { - return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *DeleteOptions { + return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -50,7 +51,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co Example: cmdcore.Examples{ cmdcore.Example{"Delete package install", []string{"package", "installed", "delete", "-i", "cert-man"}}, - }.Description("kctrl", "-i", o.positionalNameArg), + }.Description(o.binaryName, "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index a8b857633..0f5fb152d 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -29,11 +29,12 @@ type GetOptions struct { valuesFileOutput string values bool + binaryName string positionalNameArg bool } -func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *GetOptions { - return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *GetOptions { + return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -51,7 +52,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command }, cmdcore.Example{"Download values being used by package install", []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}, - }.Description("kctrl", "-i", o.positionalNameArg), + }.Description(o.binaryName, "-i", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index d682d3bcf..ecc30de32 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -22,10 +22,12 @@ type ListOptions struct { NamespaceFlags cmdcore.NamespaceFlags AllNamespaces bool + + binaryName string } -func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *ListOptions { - return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger} +func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string) *ListOptions { + return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName} } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -40,7 +42,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman }, cmdcore.Example{"List installed packages in all namespaces", []string{"package", "installed", "list", "A"}}, - }.Description("kctrl", "", false), + }.Description(o.binaryName, "", false), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List installed packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index 500fd4ef9..8d04c5d1c 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -37,11 +37,12 @@ type AddOrUpdateOptions struct { WaitFlags cmdcore.WaitFlags + binaryName string positionalNameArg bool } -func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *AddOrUpdateOptions { - return &AddOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *AddOrUpdateOptions { + return &AddOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -52,7 +53,7 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. Example: cmdcore.Examples{ cmdcore.Example{"Add a package repository", []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}}, - }.Description("kctrl", "-r", o.positionalNameArg), + }.Description(o.binaryName, "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -84,7 +85,7 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob Example: cmdcore.Examples{ cmdcore.Example{"Update a package repository with a new URL", []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}}, - }.Description("kctrl", "-r", o.positionalNameArg), + }.Description(o.binaryName, "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index 118c78243..931b46588 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -30,11 +30,12 @@ type DeleteOptions struct { WaitFlags cmdcore.WaitFlags + binaryName string positionalNameArg bool } -func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *DeleteOptions { - return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *DeleteOptions { + return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -45,7 +46,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co Example: cmdcore.Examples{ cmdcore.Example{"Delete a package repository", []string{"package", "repository", "delete", "-r", "tce"}}, - }.Description("kctrl", "-r", o.positionalNameArg), + }.Description(o.binaryName, "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index a610328e4..ea5953cac 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -23,11 +23,12 @@ type GetOptions struct { NamespaceFlags cmdcore.NamespaceFlags Name string + binaryName string positionalNameArg bool } -func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, positionalNameArg bool) *GetOptions { - return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, positionalNameArg: positionalNameArg} +func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *GetOptions { + return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -39,7 +40,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command Example: cmdcore.Examples{ cmdcore.Example{"Get details for a package repository", []string{"package", "repository", "get", "-r", "tce"}}, - }.Description("kctrl", "-r", o.positionalNameArg), + }.Description(o.binaryName, "-r", o.positionalNameArg), } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/list.go b/cli/pkg/kctrl/cmd/package/repository/list.go index db246a4d4..166f7bb34 100644 --- a/cli/pkg/kctrl/cmd/package/repository/list.go +++ b/cli/pkg/kctrl/cmd/package/repository/list.go @@ -23,10 +23,12 @@ type ListOptions struct { NamespaceFlags cmdcore.NamespaceFlags AllNamespaces bool + + binaryName string } -func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *ListOptions { - return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger} +func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string) *ListOptions { + return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName} } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -41,7 +43,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman }, cmdcore.Example{"List package repositories in all namespaces", []string{"package", "repository", "list", "A"}}, - }.Description("kctrl", "", false), + }.Description(o.binaryName, "", false), } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List repositories in all namespaces") diff --git a/cli/pkg/kctrl/cmd/ui_flags.go b/cli/pkg/kctrl/cmd/ui_flags.go index 6b48a360c..f45b7b41c 100644 --- a/cli/pkg/kctrl/cmd/ui_flags.go +++ b/cli/pkg/kctrl/cmd/ui_flags.go @@ -18,11 +18,15 @@ type UIFlags struct { Columns []string } -func (f *UIFlags) Set(cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory) { +func (f *UIFlags) Set(cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { // Default tty to true: https://github.com/vmware-tanzu/carvel-kapp/issues/28 cmd.PersistentFlags().BoolVar(&f.TTY, "tty", true, "Force TTY-like output") - cmd.PersistentFlags().BoolVar(&f.Color, "color", true, "Set color output") - cmd.PersistentFlags().BoolVar(&f.JSON, "json", false, "Output as JSON") + if opts.Color { + cmd.PersistentFlags().BoolVar(&f.Color, "color", true, "Set color output") + } + if opts.JSON { + cmd.PersistentFlags().BoolVar(&f.JSON, "json", false, "Output as JSON") + } cmd.PersistentFlags().BoolVarP(&f.NonInteractive, "yes", "y", false, "Assume yes for any prompt") cmd.PersistentFlags().StringSliceVar(&f.Columns, "column", nil, "Filter to show only given columns") } From 8247942b9f7eacad3144c310f266444c2b5f20b3 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Tue, 8 Feb 2022 19:48:00 +0530 Subject: [PATCH 11/16] Update error message for kctrl version --- cli/pkg/kctrl/cmd/version.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/cli/pkg/kctrl/cmd/version.go b/cli/pkg/kctrl/cmd/version.go index 7ecafd382..834216075 100644 --- a/cli/pkg/kctrl/cmd/version.go +++ b/cli/pkg/kctrl/cmd/version.go @@ -46,11 +46,7 @@ func NewVersionCmd(o *VersionOptions, flagsFactory cmdcore.FlagsFactory) *cobra. func (o *VersionOptions) Run() error { if o.controllerVersion { - err := o.showControllerVersion() - if err != nil { - return err - } - return nil + return o.showControllerVersion() } o.ui.PrintBlock([]byte(fmt.Sprintf("kctrl version %s\n", version.Version))) @@ -67,7 +63,7 @@ func (o *VersionOptions) showControllerVersion() error { controllerDeployment, err := coreClient.AppsV1().Deployments(kappControllerNamespace).Get(context.Background(), kappControllerDeployment, metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { - return fmt.Errorf("kapp-controller not installed on cluster") + return fmt.Errorf("kapp-controller not installed on cluster (or is not present in the default location namespace=kapp-controller deployment=kapp-controller)") } return err From 7585000cb94d529d465c21ec80f3cd20f2085994 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Tue, 8 Feb 2022 20:05:46 +0530 Subject: [PATCH 12/16] Use cmd.Args for positionalArgs --- cli/pkg/kctrl/cmd/kctrl.go | 2 +- cli/pkg/kctrl/cmd/package/available/get.go | 1 + cli/pkg/kctrl/cmd/package/available/list.go | 1 + cli/pkg/kctrl/cmd/package/installed/get.go | 1 + cli/pkg/kctrl/cmd/package/repository/get.go | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/pkg/kctrl/cmd/kctrl.go b/cli/pkg/kctrl/cmd/kctrl.go index ad3a34ac3..980a10043 100644 --- a/cli/pkg/kctrl/cmd/kctrl.go +++ b/cli/pkg/kctrl/cmd/kctrl.go @@ -83,7 +83,7 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm cmd.AddCommand(pkgCmd) - ConfigureGlobalFlags(o, cmd, flagsFactory, false) + ConfigureGlobalFlags(o, cmd, flagsFactory, pkgOpts.PositionalArgs) return cmd } diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index e7517d843..80948d15e 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -43,6 +43,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command Use: "get", Aliases: []string{"g"}, Short: "Get details for an available package or the openAPI schema of a package with a specific version", + Args: cobra.ExactArgs(1), RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, Example: cmdcore.Examples{ cmdcore.Example{"Get details about an available package", diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index 86752aa35..4ef134f13 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -42,6 +42,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman Use: "list", Aliases: []string{"l", "ls"}, Short: "List available packages in a namespace", + Args: cobra.MaximumNArgs(1), RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, Example: cmdcore.Examples{ cmdcore.Example{"List packages available on the cluster", diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 0f5fb152d..d2e371dda 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -42,6 +42,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command Use: "get", Aliases: []string{"g"}, Short: "Get details for installed package", + Args: cobra.ExactArgs(1), RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, Example: cmdcore.Examples{ cmdcore.Example{"Get details for a package install", diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index ea5953cac..fdb3a5997 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -36,6 +36,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command Use: "get", Aliases: []string{"g"}, Short: "Get details for a package repository", + Args: cobra.ExactArgs(1), RunE: func(_ *cobra.Command, args []string) error { return o.Run(args) }, Example: cmdcore.Examples{ cmdcore.Example{"Get details for a package repository", From de018448bbedfd31a5589fd26a93984660ede706 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:31:42 +0530 Subject: [PATCH 13/16] Update command usage for positional arguments --- cli/pkg/kctrl/cmd/package/available/get.go | 2 ++ cli/pkg/kctrl/cmd/package/available/list.go | 3 +++ cli/pkg/kctrl/cmd/package/installed/create_or_update.go | 6 ++++++ cli/pkg/kctrl/cmd/package/installed/delete.go | 2 ++ cli/pkg/kctrl/cmd/package/installed/get.go | 2 ++ cli/pkg/kctrl/cmd/package/repository/add_or_update.go | 4 ++++ cli/pkg/kctrl/cmd/package/repository/delete.go | 2 ++ cli/pkg/kctrl/cmd/package/repository/get.go | 2 ++ 8 files changed, 23 insertions(+) diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index 80948d15e..060937ebe 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -58,6 +58,8 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package", "p", "", "Set package name (required)") + } else { + cmd.Use = "get PACKAGE_NAME or PACKAGE_NAME/VERSION" } cmd.Flags().BoolVar(&o.ValuesSchema, "values-schema", false, "Values schema of the package (optional)") diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index 4ef134f13..7c98322d5 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -65,8 +65,11 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List available packages in all namespaces") cmd.Flags().BoolVar(&o.Summary, "summary", true, "Show summarized list of packages") + if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package", "p", "", "List all available versions of package") + } else { + cmd.Use = "list or list PACKAGE_NAME" } cmd.Flags().BoolVar(&o.Wide, "wide", false, "Show additional info") diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index 44e2bc746..fc5454d0b 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -77,6 +77,8 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") + } else { + cmd.Use = "create INSTALLED_PACKAGE_NAME --package-name PACKAGE_NAME --version VERSION" } cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Set package name (required)") @@ -113,6 +115,8 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") + } else { + cmd.Use = "create INSTALLED_PACKAGE_NAME --package-name PACKAGE_NAME --version VERSION" } cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Set package name (required)") @@ -146,6 +150,8 @@ func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name") + } else { + cmd.Use = "update INSTALLED_PACKAGE_NAME" } cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Name of package install to be updated") diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 77b92b790..f91186376 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -57,6 +57,8 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") + } else { + cmd.Use = "delete INSTALLED_PACKAGE_NAME" } o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index d2e371dda..24be7b28f 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -59,6 +59,8 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") + } else { + cmd.Use = "get INSTALLED_PACKAGE_NAME" } cmd.Flags().StringVar(&o.valuesFileOutput, "values-file-output", "", "File path for exporting configuration values file") diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index 8d04c5d1c..341472371 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -60,6 +60,8 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") + } else { + cmd.Use = "add REPOSITORY_NAME --url REPOSITORY_URL" } // TODO consider how to support other repository types @@ -92,6 +94,8 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") + } else { + cmd.Use = "update REPOSITORY_NAME --url REPOSITORY_URL" } cmd.Flags().StringVarP(&o.URL, "url", "", "", "OCI registry url for package repository bundle (required)") diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index 931b46588..0a3bab411 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -53,6 +53,8 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") + } else { + cmd.Use = "delete REPOSITORY_NAME" } o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index fdb3a5997..e6835a286 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -47,6 +47,8 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command if !o.positionalNameArg { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") + } else { + cmd.Use = "get REPOSITORY_NAME" } return cmd From 0386f33f4ad545dad2a70ad74029d9d94add43e9 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Wed, 9 Feb 2022 14:02:48 +0530 Subject: [PATCH 14/16] Add silence usage to sub commands --- cli/pkg/kctrl/cmd/package/available/get.go | 1 + cli/pkg/kctrl/cmd/package/available/list.go | 1 + cli/pkg/kctrl/cmd/package/installed/create_or_update.go | 1 + cli/pkg/kctrl/cmd/package/installed/delete.go | 1 + cli/pkg/kctrl/cmd/package/installed/get.go | 1 + cli/pkg/kctrl/cmd/package/installed/list.go | 1 + cli/pkg/kctrl/cmd/package/repository/add_or_update.go | 2 ++ cli/pkg/kctrl/cmd/package/repository/delete.go | 1 + cli/pkg/kctrl/cmd/package/repository/get.go | 1 + cli/pkg/kctrl/cmd/package/repository/list.go | 1 + 10 files changed, 11 insertions(+) diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index 060937ebe..46935d79e 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -52,6 +52,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command cmdcore.Example{"Get the values schema for a particular version of the package", []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}, }.Description(o.binaryName, "-p", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index 7c98322d5..0c5f9f6bd 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -60,6 +60,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman cmdcore.Example{"List all available versions of a package", []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}, }.Description(o.binaryName, "-p", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List available packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index fc5454d0b..c52c8bd71 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -72,6 +72,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * cmdcore.Example{"Install package and ask it to use an existing service account", []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, }.Description(o.binaryName, "-i", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index f91186376..632c2e9ef 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -52,6 +52,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co cmdcore.Example{"Delete package install", []string{"package", "installed", "delete", "-i", "cert-man"}}, }.Description(o.binaryName, "-i", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 24be7b28f..912b60856 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -54,6 +54,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command cmdcore.Example{"Download values being used by package install", []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}, }.Description(o.binaryName, "-i", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index ecc30de32..25face801 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -43,6 +43,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman cmdcore.Example{"List installed packages in all namespaces", []string{"package", "installed", "list", "A"}}, }.Description(o.binaryName, "", false), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List installed packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index 341472371..9576a1258 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -54,6 +54,7 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. cmdcore.Example{"Add a package repository", []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}}, }.Description(o.binaryName, "-r", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) @@ -88,6 +89,7 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob cmdcore.Example{"Update a package repository with a new URL", []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}}, }.Description(o.binaryName, "-r", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index 0a3bab411..b04387c8c 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -47,6 +47,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co cmdcore.Example{"Delete a package repository", []string{"package", "repository", "delete", "-r", "tce"}}, }.Description(o.binaryName, "-r", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index e6835a286..a3b7b6525 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -42,6 +42,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command cmdcore.Example{"Get details for a package repository", []string{"package", "repository", "get", "-r", "tce"}}, }.Description(o.binaryName, "-r", o.positionalNameArg), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/list.go b/cli/pkg/kctrl/cmd/package/repository/list.go index 166f7bb34..ed993ea5e 100644 --- a/cli/pkg/kctrl/cmd/package/repository/list.go +++ b/cli/pkg/kctrl/cmd/package/repository/list.go @@ -44,6 +44,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman cmdcore.Example{"List package repositories in all namespaces", []string{"package", "repository", "list", "A"}}, }.Description(o.binaryName, "", false), + SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List repositories in all namespaces") From be284096f6c0203a5cc8da0d797cb8e885d65a68 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Tue, 15 Feb 2022 15:34:22 +0530 Subject: [PATCH 15/16] Add annotations to get and list commands --- cli/pkg/kctrl/cmd/package/available/get.go | 1 + cli/pkg/kctrl/cmd/package/available/list.go | 1 + cli/pkg/kctrl/cmd/package/installed/get.go | 1 + cli/pkg/kctrl/cmd/package/installed/list.go | 1 + cli/pkg/kctrl/cmd/package/repository/get.go | 1 + cli/pkg/kctrl/cmd/package/repository/list.go | 1 + 6 files changed, 6 insertions(+) diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index 46935d79e..727dec561 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -53,6 +53,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}, }.Description(o.binaryName, "-p", o.positionalNameArg), SilenceUsage: true, + Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index 0c5f9f6bd..deeafa386 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -61,6 +61,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}, }.Description(o.binaryName, "-p", o.positionalNameArg), SilenceUsage: true, + Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List available packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 912b60856..0cd583727 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -55,6 +55,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}, }.Description(o.binaryName, "-i", o.positionalNameArg), SilenceUsage: true, + Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index 25face801..453b241ee 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -44,6 +44,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman []string{"package", "installed", "list", "A"}}, }.Description(o.binaryName, "", false), SilenceUsage: true, + Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List installed packages in all namespaces") diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index a3b7b6525..1856014ae 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -43,6 +43,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command []string{"package", "repository", "get", "-r", "tce"}}, }.Description(o.binaryName, "-r", o.positionalNameArg), SilenceUsage: true, + Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) diff --git a/cli/pkg/kctrl/cmd/package/repository/list.go b/cli/pkg/kctrl/cmd/package/repository/list.go index ed993ea5e..c3ad71105 100644 --- a/cli/pkg/kctrl/cmd/package/repository/list.go +++ b/cli/pkg/kctrl/cmd/package/repository/list.go @@ -45,6 +45,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman []string{"package", "repository", "list", "A"}}, }.Description(o.binaryName, "", false), SilenceUsage: true, + Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", false, "List repositories in all namespaces") From 08d1a64a34b2b24e4783f3b351321e7fc41a9e29 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Tue, 15 Feb 2022 17:15:51 +0530 Subject: [PATCH 16/16] Move package command tree opts to cmdcore --- cli/pkg/kctrl/cmd/core/examples.go | 18 +++++--- cli/pkg/kctrl/cmd/kctrl.go | 43 ++++++++----------- cli/pkg/kctrl/cmd/package/available/get.go | 13 +++--- cli/pkg/kctrl/cmd/package/available/list.go | 13 +++--- .../cmd/package/installed/create_or_update.go | 23 +++++----- cli/pkg/kctrl/cmd/package/installed/delete.go | 13 +++--- cli/pkg/kctrl/cmd/package/installed/get.go | 13 +++--- cli/pkg/kctrl/cmd/package/installed/list.go | 8 ++-- .../cmd/package/repository/add_or_update.go | 17 ++++---- .../kctrl/cmd/package/repository/delete.go | 13 +++--- cli/pkg/kctrl/cmd/package/repository/get.go | 13 +++--- cli/pkg/kctrl/cmd/package/repository/list.go | 8 ++-- cli/pkg/kctrl/cmd/ui_flags.go | 2 +- 13 files changed, 95 insertions(+), 102 deletions(-) diff --git a/cli/pkg/kctrl/cmd/core/examples.go b/cli/pkg/kctrl/cmd/core/examples.go index dc15063ef..1405e976d 100644 --- a/cli/pkg/kctrl/cmd/core/examples.go +++ b/cli/pkg/kctrl/cmd/core/examples.go @@ -8,15 +8,23 @@ import ( "strings" ) +type PackageCommandTreeOpts struct { + BinaryName string + PositionalArgs bool + + Color bool + JSON bool +} + type Example struct { Description string Args []string } -func (e Example) asString(binaryName, nameFlag string, positionalNameArg bool) string { - command := binaryName +func (e Example) asString(nameFlag string, opts PackageCommandTreeOpts) string { + command := opts.BinaryName for _, arg := range e.Args { - if positionalNameArg && arg == nameFlag { + if opts.PositionalArgs && arg == nameFlag { continue } command += " " + arg @@ -26,10 +34,10 @@ func (e Example) asString(binaryName, nameFlag string, positionalNameArg bool) s type Examples []Example -func (es Examples) Description(binaryName, nameFlag string, positionalNameArg bool) string { +func (es Examples) Description(nameFlag string, opts PackageCommandTreeOpts) string { var description string for _, example := range es { - description += example.asString(binaryName, nameFlag, positionalNameArg) + "\n\n" + description += example.asString(nameFlag, opts) + "\n\n" } return strings.TrimSuffix(description, "\n\n") } diff --git a/cli/pkg/kctrl/cmd/kctrl.go b/cli/pkg/kctrl/cmd/kctrl.go index 980a10043..0445aa153 100644 --- a/cli/pkg/kctrl/cmd/kctrl.go +++ b/cli/pkg/kctrl/cmd/kctrl.go @@ -70,7 +70,7 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm cmdcore.RestOfCommandsHelpGroup, })) - pkgOpts := PackageCommandTreeOpts{"kctrl", false, true, true} + pkgOpts := cmdcore.PackageCommandTreeOpts{BinaryName: "kctrl", PositionalArgs: false, Color: true, JSON: true} SetGlobalFlags(o, cmd, flagsFactory, pkgOpts) @@ -88,7 +88,7 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm return cmd } -func SetGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { +func SetGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts cmdcore.PackageCommandTreeOpts) { o.UIFlags.Set(cmd, flagsFactory, opts) o.LoggerFlags.Set(cmd, flagsFactory) o.KubeAPIFlags.Set(cmd, flagsFactory) @@ -130,38 +130,38 @@ func ConfigureGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdc } } -func AddPackageCommands(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { +func AddPackageCommands(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts cmdcore.PackageCommandTreeOpts) { pkgrepoCmd := pkgrepo.NewCmd() - pkgrepoCmd.AddCommand(pkgrepo.NewListCmd(pkgrepo.NewListOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewGetCmd(pkgrepo.NewGetOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewDeleteCmd(pkgrepo.NewDeleteOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewAddCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgrepoCmd.AddCommand(pkgrepo.NewUpdateCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewListCmd(pkgrepo.NewListOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewGetCmd(pkgrepo.NewGetOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewDeleteCmd(pkgrepo.NewDeleteOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewAddCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgrepoCmd.AddCommand(pkgrepo.NewUpdateCmd(pkgrepo.NewAddOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) pkgiCmd := pkginst.NewCmd() - pkgiCmd.AddCommand(pkginst.NewListCmd(pkginst.NewListOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewGetCmd(pkginst.NewGetOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewCreateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewUpdateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgiCmd.AddCommand(pkginst.NewDeleteCmd(pkginst.NewDeleteOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewListCmd(pkginst.NewListOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewGetCmd(pkginst.NewGetOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewCreateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewUpdateCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgiCmd.AddCommand(pkginst.NewDeleteCmd(pkginst.NewDeleteOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) pkgaCmd := pkgavail.NewCmd() - pkgaCmd.AddCommand(pkgavail.NewListCmd(pkgavail.NewListOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) - pkgaCmd.AddCommand(pkgavail.NewGetCmd(pkgavail.NewGetOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + pkgaCmd.AddCommand(pkgavail.NewListCmd(pkgavail.NewListOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) + pkgaCmd.AddCommand(pkgavail.NewGetCmd(pkgavail.NewGetOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) cmd.AddCommand(pkgrepoCmd) cmd.AddCommand(pkgiCmd) cmd.AddCommand(pkgaCmd) - cmd.AddCommand(pkginst.NewInstallCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts.BinaryName, opts.PositionalArgs), flagsFactory)) + cmd.AddCommand(pkginst.NewInstallCmd(pkginst.NewCreateOrUpdateOptions(o.ui, o.depsFactory, o.logger, opts), flagsFactory)) } -func AttachGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { +func AttachGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts cmdcore.PackageCommandTreeOpts) { SetGlobalFlags(o, cmd, flagsFactory, opts) ConfigurePathResolvers(o, cmd, flagsFactory) ConfigureGlobalFlags(o, cmd, flagsFactory, opts.PositionalArgs) } -func AttachKctrlPackageCommandTree(cmd *cobra.Command, confUI *ui.ConfUI, opts PackageCommandTreeOpts) { +func AttachKctrlPackageCommandTree(cmd *cobra.Command, confUI *ui.ConfUI, opts cmdcore.PackageCommandTreeOpts) { configFactory := cmdcore.NewConfigFactoryImpl() depsFactory := cmdcore.NewDepsFactoryImpl(configFactory, confUI) options := NewKctrlOptions(confUI, configFactory, depsFactory) @@ -181,10 +181,3 @@ func (w uiBlockWriter) Write(p []byte) (n int, err error) { w.ui.PrintBlock(p) return len(p), nil } - -type PackageCommandTreeOpts struct { - BinaryName string - PositionalArgs bool - Color bool - JSON bool -} diff --git a/cli/pkg/kctrl/cmd/package/available/get.go b/cli/pkg/kctrl/cmd/package/available/get.go index 727dec561..127fb4a2b 100644 --- a/cli/pkg/kctrl/cmd/package/available/get.go +++ b/cli/pkg/kctrl/cmd/package/available/get.go @@ -30,12 +30,11 @@ type GetOptions struct { ValuesSchema bool - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *GetOptions { - return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *GetOptions { + return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -51,14 +50,14 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command }, cmdcore.Example{"Get the values schema for a particular version of the package", []string{"package", "available", "get", "-p", "cert-manager.community.tanzu.vmware.com", "--values-schema"}}, - }.Description(o.binaryName, "-p", o.positionalNameArg), + }.Description("-p", o.pkgCmdTreeOpts), SilenceUsage: true, Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package", "p", "", "Set package name (required)") } else { cmd.Use = "get PACKAGE_NAME or PACKAGE_NAME/VERSION" @@ -71,7 +70,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command func (o *GetOptions) Run(args []string) error { var pkgName, pkgVersion string - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/available/list.go b/cli/pkg/kctrl/cmd/package/available/list.go index deeafa386..a8b9885a4 100644 --- a/cli/pkg/kctrl/cmd/package/available/list.go +++ b/cli/pkg/kctrl/cmd/package/available/list.go @@ -29,12 +29,11 @@ type ListOptions struct { Summary bool Wide bool - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *ListOptions { - return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *ListOptions { + return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -59,7 +58,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman }, cmdcore.Example{"List all available versions of a package", []string{"package", "available", "list", "-p", "cert-manager.community.tanzu.vmware.com"}}, - }.Description(o.binaryName, "-p", o.positionalNameArg), + }.Description("-p", o.pkgCmdTreeOpts), SilenceUsage: true, Annotations: map[string]string{"table": ""}, } @@ -68,7 +67,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman cmd.Flags().BoolVar(&o.Summary, "summary", true, "Show summarized list of packages") - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package", "p", "", "List all available versions of package") } else { cmd.Use = "list or list PACKAGE_NAME" @@ -80,7 +79,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman } func (o *ListOptions) Run(args []string) error { - if o.positionalNameArg && len(args) > 0 { + if o.pkgCmdTreeOpts.PositionalArgs && len(args) > 0 { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index c52c8bd71..e28a35ded 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -49,12 +49,11 @@ type CreateOrUpdateOptions struct { NamespaceFlags cmdcore.NamespaceFlags createdAnnotations *CreatedResourceAnnotations - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewCreateOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *CreateOrUpdateOptions { - return &CreateOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewCreateOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *CreateOrUpdateOptions { + return &CreateOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -71,12 +70,12 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * }, cmdcore.Example{"Install package and ask it to use an existing service account", []string{"package", "installed", "create", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, - }.Description(o.binaryName, "-i", o.positionalNameArg), + }.Description("-i", o.pkgCmdTreeOpts), SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") } else { cmd.Use = "create INSTALLED_PACKAGE_NAME --package-name PACKAGE_NAME --version VERSION" @@ -110,11 +109,11 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) }, cmdcore.Example{"Install package and ask it to use an existing service account", []string{"package", "install", "-i", "cert-man", "-p", "cert-manager.community.tanzu.vmware.com", "--version", "1.6.1", "--service-account-name", "existing-sa"}}, - }.Description(o.binaryName, "-i", o.positionalNameArg), + }.Description("-i", o.pkgCmdTreeOpts), } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") } else { cmd.Use = "create INSTALLED_PACKAGE_NAME --package-name PACKAGE_NAME --version VERSION" @@ -145,11 +144,11 @@ func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * }, cmdcore.Example{"Update package install with new values file", []string{"package", "installed", "update", "-i", "cert-man", "--values-file", "values.yml"}}, - }.Description(o.binaryName, "-i", o.positionalNameArg), + }.Description("-i", o.pkgCmdTreeOpts), } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name") } else { cmd.Use = "update INSTALLED_PACKAGE_NAME" @@ -170,7 +169,7 @@ func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * } func (o *CreateOrUpdateOptions) RunCreate(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } @@ -250,7 +249,7 @@ func (o *CreateOrUpdateOptions) create(client kubernetes.Interface, kcClient kcc } func (o *CreateOrUpdateOptions) RunUpdate(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 632c2e9ef..d4f7229fe 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -35,12 +35,11 @@ type DeleteOptions struct { WaitFlags cmdcore.WaitFlags - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *DeleteOptions { - return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *DeleteOptions { + return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -51,12 +50,12 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co Example: cmdcore.Examples{ cmdcore.Example{"Delete package install", []string{"package", "installed", "delete", "-i", "cert-man"}}, - }.Description(o.binaryName, "-i", o.positionalNameArg), + }.Description("-i", o.pkgCmdTreeOpts), SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") } else { cmd.Use = "delete INSTALLED_PACKAGE_NAME" @@ -72,7 +71,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co } func (o *DeleteOptions) Run(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/installed/get.go b/cli/pkg/kctrl/cmd/package/installed/get.go index 0cd583727..c5d336a73 100644 --- a/cli/pkg/kctrl/cmd/package/installed/get.go +++ b/cli/pkg/kctrl/cmd/package/installed/get.go @@ -29,12 +29,11 @@ type GetOptions struct { valuesFileOutput string values bool - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *GetOptions { - return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *GetOptions { + return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -53,13 +52,13 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command }, cmdcore.Example{"Download values being used by package install", []string{"package", "installed", "get", "-i", "cert-man", "--values-file-output", "values.yml"}}, - }.Description(o.binaryName, "-i", o.positionalNameArg), + }.Description("-i", o.pkgCmdTreeOpts), SilenceUsage: true, Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") } else { cmd.Use = "get INSTALLED_PACKAGE_NAME" @@ -71,7 +70,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command } func (o *GetOptions) Run(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/installed/list.go b/cli/pkg/kctrl/cmd/package/installed/list.go index 453b241ee..5381de26a 100644 --- a/cli/pkg/kctrl/cmd/package/installed/list.go +++ b/cli/pkg/kctrl/cmd/package/installed/list.go @@ -23,11 +23,11 @@ type ListOptions struct { NamespaceFlags cmdcore.NamespaceFlags AllNamespaces bool - binaryName string + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string) *ListOptions { - return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName} +func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *ListOptions { + return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -42,7 +42,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman }, cmdcore.Example{"List installed packages in all namespaces", []string{"package", "installed", "list", "A"}}, - }.Description(o.binaryName, "", false), + }.Description("", o.pkgCmdTreeOpts), SilenceUsage: true, Annotations: map[string]string{"table": ""}, } diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index 9576a1258..e5074b33f 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -37,12 +37,11 @@ type AddOrUpdateOptions struct { WaitFlags cmdcore.WaitFlags - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *AddOrUpdateOptions { - return &AddOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewAddOrUpdateOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *AddOrUpdateOptions { + return &AddOrUpdateOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -53,13 +52,13 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. Example: cmdcore.Examples{ cmdcore.Example{"Add a package repository", []string{"package", "repository", "add", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.1"}}, - }.Description(o.binaryName, "-r", o.positionalNameArg), + }.Description("-r", o.pkgCmdTreeOpts), SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") } else { cmd.Use = "add REPOSITORY_NAME --url REPOSITORY_URL" @@ -88,13 +87,13 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob Example: cmdcore.Examples{ cmdcore.Example{"Update a package repository with a new URL", []string{"package", "repository", "update", "-r", "tce", "--url", "projects.registry.vmware.com/tce/main:0.9.2"}}, - }.Description(o.binaryName, "-r", o.positionalNameArg), + }.Description("-r", o.pkgCmdTreeOpts), SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") } else { cmd.Use = "update REPOSITORY_NAME --url REPOSITORY_URL" @@ -114,7 +113,7 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob } func (o *AddOrUpdateOptions) Run(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index b04387c8c..232c85d4a 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -30,12 +30,11 @@ type DeleteOptions struct { WaitFlags cmdcore.WaitFlags - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *DeleteOptions { - return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *DeleteOptions { + return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -46,13 +45,13 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co Example: cmdcore.Examples{ cmdcore.Example{"Delete a package repository", []string{"package", "repository", "delete", "-r", "tce"}}, - }.Description(o.binaryName, "-r", o.positionalNameArg), + }.Description("-r", o.pkgCmdTreeOpts), SilenceUsage: true, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") } else { cmd.Use = "delete REPOSITORY_NAME" @@ -68,7 +67,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co } func (o *DeleteOptions) Run(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/repository/get.go b/cli/pkg/kctrl/cmd/package/repository/get.go index 1856014ae..09420b385 100644 --- a/cli/pkg/kctrl/cmd/package/repository/get.go +++ b/cli/pkg/kctrl/cmd/package/repository/get.go @@ -23,12 +23,11 @@ type GetOptions struct { NamespaceFlags cmdcore.NamespaceFlags Name string - binaryName string - positionalNameArg bool + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string, positionalNameArg bool) *GetOptions { - return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName, positionalNameArg: positionalNameArg} +func NewGetOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *GetOptions { + return &GetOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -41,13 +40,13 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command Example: cmdcore.Examples{ cmdcore.Example{"Get details for a package repository", []string{"package", "repository", "get", "-r", "tce"}}, - }.Description(o.binaryName, "-r", o.positionalNameArg), + }.Description("-r", o.pkgCmdTreeOpts), SilenceUsage: true, Annotations: map[string]string{"table": ""}, } o.NamespaceFlags.Set(cmd, flagsFactory) - if !o.positionalNameArg { + if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") } else { cmd.Use = "get REPOSITORY_NAME" @@ -57,7 +56,7 @@ func NewGetCmd(o *GetOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command } func (o *GetOptions) Run(args []string) error { - if o.positionalNameArg { + if o.pkgCmdTreeOpts.PositionalArgs { o.Name = args[0] } diff --git a/cli/pkg/kctrl/cmd/package/repository/list.go b/cli/pkg/kctrl/cmd/package/repository/list.go index c3ad71105..23f707cff 100644 --- a/cli/pkg/kctrl/cmd/package/repository/list.go +++ b/cli/pkg/kctrl/cmd/package/repository/list.go @@ -24,11 +24,11 @@ type ListOptions struct { NamespaceFlags cmdcore.NamespaceFlags AllNamespaces bool - binaryName string + pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts } -func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, binaryName string) *ListOptions { - return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, binaryName: binaryName} +func NewListOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger, pkgCmdTreeOpts cmdcore.PackageCommandTreeOpts) *ListOptions { + return &ListOptions{ui: ui, depsFactory: depsFactory, logger: logger, pkgCmdTreeOpts: pkgCmdTreeOpts} } func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command { @@ -43,7 +43,7 @@ func NewListCmd(o *ListOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman }, cmdcore.Example{"List package repositories in all namespaces", []string{"package", "repository", "list", "A"}}, - }.Description(o.binaryName, "", false), + }.Description("", o.pkgCmdTreeOpts), SilenceUsage: true, Annotations: map[string]string{"table": ""}, } diff --git a/cli/pkg/kctrl/cmd/ui_flags.go b/cli/pkg/kctrl/cmd/ui_flags.go index f45b7b41c..b4bb708ff 100644 --- a/cli/pkg/kctrl/cmd/ui_flags.go +++ b/cli/pkg/kctrl/cmd/ui_flags.go @@ -18,7 +18,7 @@ type UIFlags struct { Columns []string } -func (f *UIFlags) Set(cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts PackageCommandTreeOpts) { +func (f *UIFlags) Set(cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory, opts cmdcore.PackageCommandTreeOpts) { // Default tty to true: https://github.com/vmware-tanzu/carvel-kapp/issues/28 cmd.PersistentFlags().BoolVar(&f.TTY, "tty", true, "Force TTY-like output") if opts.Color {