Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: modify args to subcommand in dtm-list and dtm-show #549

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/devstream/develop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var developCreatePluginCMD = &cobra.Command{
Short: "Create a new plugin",
Long: `Create-plugin is used for creating a new plugin.
Exampls:
dtm develop create-plugin --name=YOUR-PLUGIN-NAME`,
dtm develop create-plugin --name=YOUR-PLUGIN-NAME`,
Run: developCreateCMDFunc,
}

Expand All @@ -31,8 +31,8 @@ var developValidatePluginCMD = &cobra.Command{
Short: "Validate a plugin",
Long: `Validate-plugin is used for validating an existing plugin or all plugins.
Examples:
dtm develop validate-plugin --name=YOUR-PLUGIN-NAME,
dtm develop validate-plugin --all`,
dtm develop validate-plugin --name=YOUR-PLUGIN-NAME,
dtm develop validate-plugin --all`,
Run: developValidateCMDFunc,
}

Expand Down
28 changes: 11 additions & 17 deletions cmd/devstream/list.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package main

import (
"fmt"

"github.com/spf13/cobra"

"github.com/devstream-io/devstream/cmd/devstream/options"

"github.com/devstream-io/devstream/cmd/devstream/list"
)

Expand All @@ -16,27 +12,25 @@ var (

var listCMD = &cobra.Command{
Use: "list",
Short: "This command lists all of the plugins",
Short: "This command only supports listing plugins now.",
}

var listPluginsCMD = &cobra.Command{
Use: "plugins",
Short: "List all plugins",
Long: `This command lists all of the plugins.
Examples:
dtm list plugins`,
Run: options.WithValidators(listCMDFunc, options.ArgsCountEqual(1), validateListCMDArgs),
Run: listPluginsCMDFunc,
}

func listCMDFunc(cmd *cobra.Command, args []string) {
func listPluginsCMDFunc(cmd *cobra.Command, args []string) {
list.List(pluginFilter)
}

func validateListCMDArgs(args []string) error {
// only support "plugins" now

if args[0] != "plugins" {
return fmt.Errorf("arg should be \"plugins\" only")
}
return nil
}

// TODO Use `--group=somegroup` to filter the specified groups on feature
func init() {
listCMD.PersistentFlags().StringVarP(&pluginFilter, "filter", "r", "", "filter plugin by regex")
listCMD.AddCommand(listPluginsCMD)

listPluginsCMD.PersistentFlags().StringVarP(&pluginFilter, "filter", "r", "", "filter plugin by regex")
}
5 changes: 4 additions & 1 deletion cmd/devstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func initConfig() {
if err := viper.BindPFlags(developValidatePluginCMD.Flags()); err != nil {
log.Fatal(err)
}
if err := viper.BindPFlags(showCMD.Flags()); err != nil {
if err := viper.BindPFlags(showConfigCMD.Flags()); err != nil {
log.Fatal(err)
}
if err := viper.BindPFlags(showStatusCMD.Flags()); err != nil {
log.Fatal(err)
}
}
Expand Down
53 changes: 32 additions & 21 deletions cmd/devstream/show.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
package main

import (
"fmt"

"github.com/spf13/cobra"

"github.com/devstream-io/devstream/cmd/devstream/options"

"github.com/devstream-io/devstream/internal/pkg/show"
"github.com/devstream-io/devstream/internal/pkg/show/config"
"github.com/devstream-io/devstream/internal/pkg/show/status"
"github.com/devstream-io/devstream/pkg/util/log"
)

var plugin string
var instanceName string

var showCMD = &cobra.Command{
Use: "show [config | status]",
Use: "show",
Short: "Show is used to print some useful information",
Long: `Show is used to print some useful information.
}

var showConfigCMD = &cobra.Command{
Use: "config",
Short: "Show configuration information",
Long: `Show config is used for showing plugins' template configuration information.
Examples:
dtm show config --plugin=A-PLUGIN-NAME`,
Run: showConfigCMDFunc,
}

var showStatusCMD = &cobra.Command{
Use: "status",
Short: "Show status information",
Long: `Show status is used for showing plugins' status information.
Examples:
dtm show config --plugin=A-PLUGIN-NAME
dtm show status --plugin=A-PLUGIN-NAME --name=A-PLUGIN-INSTANCE-NAME
dtm show status -p=A-PLUGIN-NAME -n=A-PLUGIN-INSTANCE-NAME`,
Run: options.WithValidators(showCMDFunc, options.ArgsCountEqual(1), validateShowArgs),
Run: showStatusCMDFunc,
}

func showCMDFunc(cmd *cobra.Command, args []string) {
showInfo := show.Info(args[0])
log.Debugf("The show info is: %s.", showInfo)
if err := show.GenerateInfo(configFile, showInfo); err != nil {
func showConfigCMDFunc(cmd *cobra.Command, args []string) {
log.Debug("Show configuration information.")
if err := config.Show(); err != nil {
log.Fatal(err)
}
}

func validateShowArgs(args []string) error {
// arg is "config" or "status" here, maybe will have "output" in the future.
showInfo := show.Info(args[0])
if !show.IsValideInfo(showInfo) {
return fmt.Errorf("invalide Show Info")
func showStatusCMDFunc(cmd *cobra.Command, args []string) {
log.Debug("Show status information.")
if err := status.Show(configFile); err != nil {
log.Fatal(err)
}
return nil
}

func init() {
showCMD.PersistentFlags().StringVarP(&plugin, "plugin", "p", "", "specify name with the plugin")
showCMD.PersistentFlags().StringVarP(&instanceName, "name", "n", "", "specify name with the plugin instance")
showCMD.AddCommand(showConfigCMD)
showCMD.AddCommand(showStatusCMD)

showConfigCMD.PersistentFlags().StringVarP(&plugin, "plugin", "p", "", "specify name with the plugin")
showStatusCMD.PersistentFlags().StringVarP(&plugin, "plugin", "p", "", "specify name with the plugin")
showStatusCMD.PersistentFlags().StringVarP(&instanceName, "name", "n", "", "specify name with the plugin instance")
}