Skip to content

Commit

Permalink
Add components to the help screen
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Pabón <[email protected]>
  • Loading branch information
lpabon committed Feb 5, 2021
1 parent 4c5ad40 commit 1c37f3e
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 77 deletions.
44 changes: 44 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/portworx/pxc/pkg/commander"
"github.com/portworx/pxc/pkg/config"
"github.com/portworx/pxc/pkg/kubernetes"
pkgplugin "github.com/portworx/pxc/pkg/plugin"
"github.com/portworx/pxc/pkg/util"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -102,6 +103,24 @@ var _ = commander.RegisterCommandInit(func() {
}
rootCmd.Flags().BoolVar(&rootOptions.showOptions, "options", false, "Show global options for all commands")
rootCmd.SetUsageTemplate(rootTmpl)

// Custom Help
defaultHelpFunc := rootCmd.HelpFunc()
defaultUsageFunc := rootCmd.UsageFunc()
fromHelp := false
rootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
fromHelp = true
defaultHelpFunc(cmd, args)
rootComponentUsage(cmd)
})

rootCmd.SetUsageFunc(func(cmd *cobra.Command) error {
err := defaultUsageFunc(cmd)
if !fromHelp {
rootComponentUsage(cmd)
}
return err
})
})

func rootCmdExec(cmd *cobra.Command, args []string) error {
Expand All @@ -112,6 +131,31 @@ func rootCmdExec(cmd *cobra.Command, args []string) error {
return nil
}

func rootComponentUsage(cmd *cobra.Command) {
arg0 := "pxc"
if util.InKubectlPluginMode() {
arg0 = "kubectl pxc"
}

if cmd != rootCmd {
return
}

lister := &pkgplugin.PluginLister{
NameOnly: true,
}
lister.Complete(rootCmd)
components, _ := lister.GetSortedRootComponents()

util.Printf("\nAvailable components:\n")

// Add components as subcommands
for _, component := range components {
util.Printf(" %s\n", component)
}
util.Printf("\nUse \"%s [component] --help\" for more information about the component\n", arg0)
}

func rootPersistentPreRunE(cmd *cobra.Command, args []string) error {

// Setup verbosity
Expand Down
87 changes: 14 additions & 73 deletions handler/plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,23 @@ limitations under the License.
package plugin

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/portworx/pxc/pkg/commander"
"github.com/portworx/pxc/pkg/config"
pluginpkg "github.com/portworx/pxc/pkg/plugin"
"github.com/portworx/pxc/pkg/util"
"github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)

type pluginListOptions struct {
Verifier PathVerifier
NameOnly bool
Lister pluginpkg.PluginLister

PluginPaths []string
}
Expand Down Expand Up @@ -88,86 +84,31 @@ func ListAddCommand(cmd *cobra.Command) {
}

func listExec(cmd *cobra.Command, args []string) error {
listOptions.Complete(cmd)
listOptions.Lister.Complete(cmd)
return listOptions.Run(cmd, args)
}

func (o *pluginListOptions) Complete(cmd *cobra.Command) error {
o.Verifier = &CommandOverrideVerifier{
root: cmd.Root(),
seenPlugins: make(map[string]string),
}

o.PluginPaths = filepath.SplitList(os.Getenv("PATH"))
o.PluginPaths = append(o.PluginPaths, path.Join(config.CM().GetFlags().ConfigDir, "bin"))
return nil
}

func (o *pluginListOptions) Run(cmd *cobra.Command, args []string) error {
pluginsFound := false
isFirstFile := true
pluginErrors := []error{}
pluginWarnings := 0

for _, dir := range uniquePathsList(o.PluginPaths) {
files, err := ioutil.ReadDir(dir)
if err != nil {
if _, ok := err.(*os.PathError); ok {
logrus.Warnf("Unable read directory %q from your PATH: %v. Skipping...", dir, err)
continue
}

pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to read directory %q in your PATH: %v", dir, err))
continue
}

for _, f := range files {
if f.IsDir() {
continue
}
if !hasValidPrefix(f.Name(), pluginpkg.ValidPluginFilenamePrefixes) {
continue
}

if isFirstFile {
util.Eprintf("The following compatible components are available:\n\n")
pluginsFound = true
isFirstFile = false
}

pluginPath := f.Name()
if !o.NameOnly {
pluginPath = filepath.Join(dir, pluginPath)
}

util.Printf("%s\n", pluginPath)
if errs := o.Verifier.Verify(filepath.Join(dir, f.Name())); len(errs) != 0 {
for _, err := range errs {
util.Eprintf(" - %s\n", err)
pluginWarnings++
}
}
}
components, err := o.Lister.GetList()
if err != nil {
return err
}

if !pluginsFound {
pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to find any pxc components in your PATH"))
if len(components) == 0 {
return fmt.Errorf("Unable to find any pxc components in your PATH")
}

if pluginWarnings > 0 {
if pluginWarnings == 1 {
pluginErrors = append(pluginErrors, fmt.Errorf("error: one component warning was found"))
} else {
pluginErrors = append(pluginErrors, fmt.Errorf("error: %v component warnings were found", pluginWarnings))
for _, component := range components {
if isFirstFile {
util.Eprintf("The following compatible components are available:\n\n")
isFirstFile = false
}
}
if len(pluginErrors) > 0 {
util.Eprintf("\n")
errs := bytes.NewBuffer(nil)
for _, e := range pluginErrors {
fmt.Fprintln(errs, e)
util.Printf("%s\n", component.Path)
for _, warning := range component.Warnings {
util.Eprintf(" - %s\n", warning)
}
return fmt.Errorf("%s", errs.String())
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions handler/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import (

var (
pluginLong = `
Provides utilities for interacting with components.
Configure components
Plugins provide extended functionality that is not part of the major command-line distribution.
Please refer to the documentation and examples for more information about how write your own components.`
Components provide extended functionality that is not part of the major command-line
distribution. Please refer to the documentation and examples for more information
about how write your own components.`
)

type pluginCmdFlags struct {
Expand All @@ -48,7 +49,7 @@ var _ = commander.RegisterCommandVar(func() {
pluginCmd = &cobra.Command{
Use: "component",
DisableFlagsInUseLine: true,
Short: "Provides utilities for interacting with components",
Short: "Configure components",
Long: pluginLong,
Run: func(cmd *cobra.Command, args []string) {
util.Printf("Please see pxc component --help for more information\n")
Expand Down
Loading

0 comments on commit 1c37f3e

Please sign in to comment.