-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sergiy Kulanov <[email protected]>
- Loading branch information
Showing
4 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |