Skip to content

Commit

Permalink
feat: Add completion subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 13, 2023
1 parent cc3b776 commit 163db81
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
103 changes: 103 additions & 0 deletions pkg/cmd/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright © 2019 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package completion

import (
"bytes"
"fmt"

"github.com/spf13/cobra"
)

const (
desc = `This command prints shell completion code which must be evaluated to provide
interactive completion
Supported Shells:
- bash
- zsh
- fish
- powershell
`
eg = `To load completions:
Bash:
$ source <(tkn-graph completion bash)
# To load completions for each session, execute once:
Linux:
$ tkn-graph completion bash > /etc/bash_completion.d/tkn
MacOS:
$ tkn-graph completion bash > /usr/local/etc/bash_completion.d/tkn
Zsh:
$ source <(tkn-graph completion zsh)
# To load completions for every sessions, you can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# and add the completion to your fpath (may differ from the first one in the fpath array)
$ tkn-graph completion zsh > "${fpath[1]}/_tkn"
# You will need to start a new shell for this setup to take effect.
Fish:
$ tkn-graph completion fish | source
# To load completions for each session, execute once:
$ tkn-graph completion fish > ~/.config/fish/completions/tkn.fish
`
)

func genZshCompletion(cmd *cobra.Command) string {
var output bytes.Buffer
_ = cmd.Root().GenZshCompletion(&output)
return fmt.Sprintf("#compdef %s\ncompdef _%s %s\n%s", cmd.Root().Use,
cmd.Root().Use, cmd.Root().Use, output.String())
}

func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "completion [SHELL]",
Short: "Prints shell completion scripts",
Long: desc,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Example: eg,
Annotations: map[string]string{
"commandType": "utility",
},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
switch args[0] {
case "bash":
_ = cmd.Root().GenBashCompletion(cmd.OutOrStdout())
case "zsh":
fmt.Fprint(cmd.OutOrStdout(), genZshCompletion(cmd))
case "fish":
_ = cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
case "powershell":
_ = cmd.Root().GenPowerShellCompletion(cmd.OutOrStdout())
}

return nil
},
}
return cmd
}
25 changes: 25 additions & 0 deletions pkg/cmd/completion/completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package completion

import (
"strings"
"testing"

"github.com/sergk/tkn-graph/pkg/test"
"github.com/stretchr/testify/assert"
)

func TestCompletion_Empty(t *testing.T) {
completion := Command()
out, err := test.ExecuteCommand(completion)
if err == nil {
t.Errorf("No errors was defined. Output: %s", out)
}
expected := "accepts 1 arg(s), received 0"
assert.Contains(t, out, expected)
}

func TestCompletionZSH(t *testing.T) {
cmd := Command()
output := genZshCompletion(cmd)
assert.True(t, strings.HasPrefix(output, "#compdef"))
}
2 changes: 2 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/sergk/tkn-graph/pkg/cmd/completion"
"github.com/sergk/tkn-graph/pkg/cmd/pipeline"
"github.com/sergk/tkn-graph/pkg/cmd/pipelinerun"
"github.com/sergk/tkn-graph/pkg/cmd/version"
Expand Down Expand Up @@ -49,6 +50,7 @@ func Root(p cli.Params) *cobra.Command {
pipeline.Command(p),
pipelinerun.Command(p),
version.Command(),
completion.Command(),
)

return cmd
Expand Down
41 changes: 41 additions & 0 deletions pkg/test/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2019 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"bytes"

"github.com/spf13/cobra"
)

// ExecuteCommand executes the root command passing the args and returns
// the output as a string and error
func ExecuteCommand(root *cobra.Command, args ...string) (string, error) {
_, output, err := ExecuteCommandC(root, args...)
return output, err
}

// ExecuteCommandC executes the root command passing the args and returns
// the root command, output as a string and error if any
func ExecuteCommandC(c *cobra.Command, args ...string) (*cobra.Command, string, error) {
buf := new(bytes.Buffer)
c.SetOutput(buf)
c.SetArgs(args)
c.SilenceUsage = true

root, err := c.ExecuteC()

return root, buf.String(), err
}

0 comments on commit 163db81

Please sign in to comment.