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

chore: completion enhancement #740

Merged
merged 3 commits into from
Jun 27, 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
7 changes: 4 additions & 3 deletions cmd/devstream/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ func applyCMDFunc(cmd *cobra.Command, args []string) {
}

func init() {
applyCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
applyCMD.Flags().StringVarP(&pluginDir, "plugin-dir", "d", pluginengine.DefaultPluginDir, "plugins directory")
applyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
applyCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", pluginengine.DefaultPluginDir, "plugins directory")
applyCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "apply directly without confirmation")

completion.FlagConfigFileCompletion(applyCMD)
completion.FlagFilenameCompletion(applyCMD, configFlagName)
completion.FlagDirnameCompletion(applyCMD, pluginDirFlagName)
}
5 changes: 5 additions & 0 deletions cmd/devstream/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ var (
pluginDir string
continueDirectly bool
)

IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
const (
configFlagName = "config-file"
pluginDirFlagName = "plugin-dir"
)
7 changes: 4 additions & 3 deletions cmd/devstream/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func deleteCMDFunc(cmd *cobra.Command, args []string) {

func init() {
deleteCMD.Flags().BoolVarP(&isForceDelete, "force", "", false, "force delete by config")
deleteCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
deleteCMD.Flags().StringVarP(&pluginDir, "plugin-dir", "d", pluginengine.DefaultPluginDir, "plugins directory")
deleteCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
deleteCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", pluginengine.DefaultPluginDir, "plugins directory")
deleteCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "delete directly without confirmation")

completion.FlagConfigFileCompletion(applyCMD)
completion.FlagFilenameCompletion(deleteCMD, configFlagName)
completion.FlagDirnameCompletion(deleteCMD, pluginDirFlagName)
}
4 changes: 2 additions & 2 deletions cmd/devstream/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func destroyCMDFunc(cmd *cobra.Command, args []string) {
}

func init() {
destroyCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
destroyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
destroyCMD.Flags().BoolVarP(&continueDirectly, "yes", "y", false, "destroy directly without confirmation")

completion.FlagConfigFileCompletion(destroyCMD)
completion.FlagFilenameCompletion(destroyCMD, configFlagName)
}
7 changes: 4 additions & 3 deletions cmd/devstream/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func initCMDFunc(cmd *cobra.Command, args []string) {
}

func init() {
initCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
initCMD.Flags().StringVarP(&pluginDir, "plugin-dir", "d", pluginengine.DefaultPluginDir, "plugins directory")
initCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
initCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", pluginengine.DefaultPluginDir, "plugins directory")

completion.FlagConfigFileCompletion(initCMD)
completion.FlagFilenameCompletion(initCMD, configFlagName)
completion.FlagDirnameCompletion(initCMD, pluginDirFlagName)
}
5 changes: 4 additions & 1 deletion cmd/devstream/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ var listPluginsCMD = &cobra.Command{
Short: "List all plugins",
Long: `This command lists all of the plugins.
Examples:
dtm list plugins`,
dtm list plugins
dtm list plugins --filter=argo.*
dtm list plugins -r ^argo
`,
Run: listPluginsCMDFunc,
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/devstream/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func verifyCMDFunc(cmd *cobra.Command, args []string) {
}

func init() {
verifyCMD.Flags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file")
verifyCMD.Flags().StringVarP(&pluginDir, "plugin-dir", "d", pluginengine.DefaultPluginDir, "plugins directory")
verifyCMD.Flags().StringVarP(&configFile, configFlagName, "f", "config.yaml", "config file")
verifyCMD.Flags().StringVarP(&pluginDir, pluginDirFlagName, "d", pluginengine.DefaultPluginDir, "plugins directory")

completion.FlagConfigFileCompletion(verifyCMD)
completion.FlagFilenameCompletion(verifyCMD, configFlagName)
completion.FlagDirnameCompletion(verifyCMD, pluginDirFlagName)
}
14 changes: 11 additions & 3 deletions internal/pkg/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ func FlagPluginsCompletion(cmd *cobra.Command, flag string) {
}
}

func FlagConfigFileCompletion(cmd *cobra.Command) {
validConfigFilenames := []string{"yaml"}
func FlagFilenameCompletion(cmd *cobra.Command, flagName string) {

if err := cmd.Flags().SetAnnotation("config-file", cobra.BashCompFilenameExt, validConfigFilenames); err != nil {
// Ref: https://github.com/spf13/cobra/blob/master/shell_completions.md#specify-valid-filename-extensions-for-flags-that-take-a-filename
if err := cmd.MarkFlagFilename(flagName, "yaml", "yml"); err != nil {
log.Warn(err)
}
}

func FlagDirnameCompletion(cmd *cobra.Command, flagName string) {

// Ref: https://github.com/spf13/cobra/blob/master/shell_completions.md#limit-flag-completions-to-directory-names
if err := cmd.MarkFlagDirname(flagName); err != nil {
log.Warn(err)
}
}
Expand Down