Skip to content

Conversation

@nicob87
Copy link
Member

@nicob87 nicob87 commented Jan 11, 2021

Closes #367

based/stolen from:
https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/cmd.go#L293

It requires "kubectl" to be installed.

To give it a try just:

$ make build-cmd #build skupper cli
$ source <(./skupper completion) #source the autocompletion script
$ complete -F __start_skupper ./skupper #hack to make autocompletion work for "./skupper" also (normally it works for "skupper")
$ #try it!
$ ./skupper -n <tab> <tab> #autocompleting flag as first argument
default          kube-node-lease  kube-public      kube-system      private-http-1   private-mongo-1  public-http-1    public-mongo-1
$ ./skupper service -n <tab> <tab>  #autocompleting flag after calling a subcommand
default          kube-node-lease  kube-public      kube-system      private-http-1   private-mongo-1  public-http-1    public-mongo-1
$ ./skupper  <tab> <tab> #verify it is still working for normal subcommands
bind              connect           delete            init              service           unexpose          
check-connection  connection-token  disconnect        list-connectors   status            version           
completion        debug             expose            list-exposed      unbind 


@nicob87 nicob87 added the cli Issues associated with the CLI label Jan 11, 2021
const (
bash_completion_func = `# $1 is the name of resource (required)
# $2 is template string for kubectl get (optional)
__kubectl_parse_get()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the assumption be in an openshift environment (e.g. oc might be present but not kubectl)?

Would adding list to pkg/kube/namespace be an alternative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically to auto-complete, bash executes "some executable", it may be anything, in this case we are executing "kubectl -n ..." we could execute "skupper -something" or build a particular new skupper executable like "skupper-completer" orrr whatever option we want.
Will take a look now if from bash I can "try" with "oc" and if fails, then try with "kubectl", which I think it is totally possible.

rootCmd = &cobra.Command{Use: "skupper"}
rootCmd = &cobra.Command{
Use: "skupper",
BashCompletionFunction: bash_completion_func,
Copy link
Member

@fgiorgetti fgiorgetti Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicob87 I would suggest writing the bash_completion in a separate .sh file,
which would make it way easier to maintain as we go.

Then we could tweak the build command for the skupper binary to include a
build-time variable that points to the bash_completion.sh file, like:

go build -ldflags "-X main.BashCompletionEncoded=`cat bash_completion.sh | base64 -w 0`" ...

Then inside skupper.go, you could decode it as:

bashCompletionFunction, err := base64.StdEncoding.DecodeString(BashCompletionEncoded)

Not sure if shell will impose some limitation, but if it works, might be worth trying.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it locally using a 65k shell script and it worked just fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmm and that works for windows and macos? (windows does not have a cat, to begin) will investigate options anyway...
awesome review!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just the "bash_completion_func" variable definition to a dedicated *.go file is a simple solution, if you want that file to be a ".sh" I think we should "read" that file from our go code, but, this makes a dependence and a possible runtime error, on a .sh file, which I do not like so much ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependency exists only at build-time, after that it no longer exists, but a separate go file just with the constant
might also work.

@nicob87 nicob87 force-pushed the nb-bash-completion-namespace branch from 7d9f270 to 4c4af01 Compare February 8, 2021 16:21
@fgiorgetti
Copy link
Member

@ajssmith @grs one comment here (just for tracking). Once we move to go 1.16 we
could use the new go:embed feature to load shell content directly into the go variable
at build time, so this way we can actually keep the bash completion script separately.

@fgiorgetti
Copy link
Member

Closing as issue #574 has been raised to track this proposal.

@fgiorgetti fgiorgetti closed this Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli Issues associated with the CLI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skupper autocomplete namespaces

3 participants