Skip to content

Commit

Permalink
Fixes terminal completion command for odo (#5856)
Browse files Browse the repository at this point in the history
* Fixes terminal completion command for odo

<!--
Thank you for opening a PR! Here are some things you need to know before submitting:

1. Please read our developer guideline: https://github.com/redhat-developer/odo/wiki/Dev:-odo-Dev-Guidelines
2. Label this PR accordingly with the '/kind' line
3. Ensure you have written and ran the appropriate tests: https://github.com/redhat-developer/odo/wiki/Dev:-Writing-and-running-tests
4. Read how we approve and LGTM each PR: https://github.com/redhat-developer/odo/wiki/Pull-Requests:-Review-guideline

Documentation:

If you are pushing a change to documentation, please read: https://github.com/redhat-developer/odo/wiki/Documentation:-Contributing
-->

**What type of PR is this:**

<!--
Add one of the following kinds:
/kind bug
/kind feature
/kind tests
/kind documentation

Feel free to use other [labels](https://github.com/redhat-developer/odo/labels) as needed. However one of the above labels must be present or the PR will not be reviewed. This instruction is for reviewers as well.
-->
/kind cleanup

**What does this PR do / why we need it:**

This PR:
* Changes the command `odo utils terminal` to `odo completion` similar
  to kubectl.
* Re-enables / fixes the terminal completion command support for bash,
  zsh, fish and powershelll
* Updates Cobra to 1.13 to enable support for autocompleting descriptive
  commands.

**Which issue(s) this PR fixes:**
<!--
Specifying the issue will automatically close it when this PR is merged
-->

Fixes #5536

**How to test changes / Special notes to the reviewer:**

Signed-off-by: Charlie Drage <charlie@charliedrage.com>

* Update pkg/odo/cli/completion/completion.go

Co-authored-by: Parthvi Vala <pvala@redhat.com>

* Update pkg/odo/cli/completion/completion.go

Co-authored-by: Parthvi Vala <pvala@redhat.com>

* Update pkg/odo/cli/completion/completion.go

Co-authored-by: Parthvi Vala <pvala@redhat.com>

* Update pkg/odo/cli/completion/completion.go

Co-authored-by: Parthvi Vala <pvala@redhat.com>

* Remove hiding the output flag

Signed-off-by: Charlie Drage <charlie@charliedrage.com>

* Update pkg/odo/cli/completion/completion.go

Co-authored-by: Parthvi Vala <pvala@redhat.com>

* Fix

Signed-off-by: Charlie Drage <charlie@charliedrage.com>

* Update pkg/odo/cli/completion/completion.go

Co-authored-by: Parthvi Vala <pvala@redhat.com>

* fix help menu

Signed-off-by: Charlie Drage <charlie@charliedrage.com>

* fix vendoring

* Fix vendoring

* Fix descriptions

* add viper

* Revert back to old style

* Update vendoring

* Remove commented out code

* ignore validation

Co-authored-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
cdrage and valaparthvi committed Jul 1, 2022
1 parent c2abde1 commit 2217725
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 211 deletions.
33 changes: 4 additions & 29 deletions pkg/odo/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/redhat-developer/odo/pkg/odo/cli/add"
"github.com/redhat-developer/odo/pkg/odo/cli/alizer"
"github.com/redhat-developer/odo/pkg/odo/cli/build_images"
"github.com/redhat-developer/odo/pkg/odo/cli/completion"
"github.com/redhat-developer/odo/pkg/odo/cli/create"
_delete "github.com/redhat-developer/odo/pkg/odo/cli/delete"
"github.com/redhat-developer/odo/pkg/odo/cli/deploy"
Expand All @@ -30,7 +31,6 @@ import (
"github.com/redhat-developer/odo/pkg/odo/cli/remove"
"github.com/redhat-developer/odo/pkg/odo/cli/set"
"github.com/redhat-developer/odo/pkg/odo/cli/telemetry"
"github.com/redhat-developer/odo/pkg/odo/cli/utils"
"github.com/redhat-developer/odo/pkg/odo/cli/version"
"github.com/redhat-developer/odo/pkg/odo/util"

Expand Down Expand Up @@ -135,7 +135,9 @@ func odoRootCmd(name, fullName string) *cobra.Command {
// We use "flag" in order to make this accessible throughtout ALL of odo, rather than the
// above traditional "persistentflags" usage that does not make it a pointer within the 'pflag'
// package

flag.CommandLine.String("o", "", "Specify output format, supported format: json")
_ = pflag.CommandLine.MarkHidden("o")

// Here we add the necessary "logging" flags.. However, we choose to hide some of these from the user
// as they are not necessarily needed and more for advanced debugging
Expand All @@ -152,16 +154,11 @@ func odoRootCmd(name, fullName string) *cobra.Command {
_ = pflag.CommandLine.MarkHidden("skip_headers")
_ = pflag.CommandLine.MarkHidden("skip_log_headers")

// We will mark the command as hidden and then re-enable if the command
// supports json output
_ = pflag.CommandLine.MarkHidden("o")

// Override the verbosity flag description
verbosity := pflag.Lookup("v")
verbosity.Usage += ". Level varies from 0 to 9 (default 0)."

cobra.AddTemplateFunc("CapitalizeFlagDescriptions", capitalizeFlagDescriptions)
cobra.AddTemplateFunc("ModifyAdditionalFlags", modifyAdditionalFlags)
rootCmd.SetUsageTemplate(rootUsageTemplate)

// Create a custom help function that will exit when we enter an invalid command, for example:
Expand All @@ -181,7 +178,6 @@ func odoRootCmd(name, fullName string) *cobra.Command {
login.NewCmdLogin(login.RecommendedCommandName, util.GetFullName(fullName, login.RecommendedCommandName)),
logout.NewCmdLogout(logout.RecommendedCommandName, util.GetFullName(fullName, logout.RecommendedCommandName)),
project.NewCmdProject(project.RecommendedCommandName, util.GetFullName(fullName, project.RecommendedCommandName)),
utils.NewCmdUtils(utils.RecommendedCommandName, util.GetFullName(fullName, utils.RecommendedCommandName)),
version.NewCmdVersion(version.RecommendedCommandName, util.GetFullName(fullName, version.RecommendedCommandName)),
preference.NewCmdPreference(preference.RecommendedCommandName, util.GetFullName(fullName, preference.RecommendedCommandName)),
telemetry.NewCmdTelemetry(telemetry.RecommendedCommandName),
Expand All @@ -199,6 +195,7 @@ func odoRootCmd(name, fullName string) *cobra.Command {
create.NewCmdCreate(create.RecommendedCommandName, util.GetFullName(fullName, create.RecommendedCommandName)),
set.NewCmdSet(set.RecommendedCommandName, util.GetFullName(fullName, set.RecommendedCommandName)),
logs.NewCmdLogs(logs.RecommendedCommandName, util.GetFullName(fullName, logs.RecommendedCommandName)),
completion.NewCmdCompletion(completion.RecommendedCommandName, util.GetFullName(fullName, completion.RecommendedCommandName)),
)

// Add all subcommands to base commands
Expand All @@ -209,28 +206,6 @@ func odoRootCmd(name, fullName string) *cobra.Command {
return rootCmd
}

// modifyAdditionalFlags modifies the flags and updates the descriptions
// as well as changes whether or not machine readable output
// has been passed in..
//
// Return the flag usages for the help output
func modifyAdditionalFlags(cmd *cobra.Command) string {

// Hide the machine readable output if the command
// does not have the annotation.
machineOutput := cmd.Annotations["machineoutput"]
f := cmd.InheritedFlags()

f.VisitAll(func(f *pflag.Flag) {
// Remove json flag if machineoutput has not been passed in
if f.Name == "o" && machineOutput == "json" {
f.Hidden = false
}
})

return capitalizeFlagDescriptions(f)
}

// capitalizeFlagDescriptions adds capitalizations
func capitalizeFlagDescriptions(f *pflag.FlagSet) string {
f.VisitAll(func(f *pflag.Flag) {
Expand Down
94 changes: 94 additions & 0 deletions pkg/odo/cli/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package completion

import (
"fmt"
"os"

"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/spf13/cobra"

ktemplates "k8s.io/kubectl/pkg/util/templates"
)

const (
RecommendedCommandName = "completion"
)

var (
completionExample = ktemplates.Examples(` # BASH
## Load into your current shell environment
source <(%[1]s bash)
## Load persistently
### Save the completion to a file
%[1]s bash > ~/.odo/completion.bash.inc
### Load the completion from within your $HOME/.bash_profile
source ~/.odo/completion.bash.inc
# ZSH
## Load into your current shell environment
source <(odo zsh)
## Load persistently
%[1]s zsh > "${fpath[1]}/_odo"
# FISH
## Load into your current shell environment
source <(%[1]s fish)
## Load persistently
%[1]s fish > ~/.config/fish/completions/odo.fish
# POWERSHELL
## Load into your current shell environment
%[1]s powershell | Out-String | Invoke-Expression
## Load persistently
%[1]s powershell >> $PROFILE
`)
completionLongDesc = ktemplates.LongDesc(`Add odo completion support to your development environment.
This will append your PS1 environment variable with odo component and application information.`)
)

// NewCmdCompletion implements the utils completion odo command
func NewCmdCompletion(name, fullName string) *cobra.Command {
completionCmd := &cobra.Command{
Use: name,
Short: "Add odo completion support to your development environment",
Long: completionLongDesc,
Example: fmt.Sprintf(completionExample, fullName),
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Below we ignore the error returns in order to pass golint validation
// We will handle the error in the main function / output when the user inputs `odo completion`.
switch args[0] {
case "bash":
_ = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
// Due to https://github.com/spf13/cobra/issues/1529 we cannot load zsh
// via using source, so we need to add compdef to the beginning of the output so we can easily do:
// source <(odo completion zsh)
zsh := "#compdef odo\ncompdef _odo odo\n"
out := os.Stdout
_, _ = out.Write([]byte(zsh))
_ = cmd.Root().GenZshCompletion(out)
case "fish":
_ = cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
_ = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
},
}

completionCmd.SetUsageTemplate(util.CmdUsageTemplate)
completionCmd.Annotations = map[string]string{"command": "utility"}
return completionCmd
}
122 changes: 0 additions & 122 deletions pkg/odo/cli/utils/terminal.go

This file was deleted.

30 changes: 0 additions & 30 deletions pkg/odo/cli/utils/terminal_test.go

This file was deleted.

29 changes: 0 additions & 29 deletions pkg/odo/cli/utils/utils.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/odo/util/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Flags:
{{CapitalizeFlagDescriptions .LocalFlags | trimRightSpace}}{{end}}{{ if .HasAvailableInheritedFlags}}
Additional Flags:
{{ModifyAdditionalFlags . | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
{{CapitalizeFlagDescriptions .InheritedFlags | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasAvailableSubCommands }}
Expand Down

0 comments on commit 2217725

Please sign in to comment.