Merge pull request #690 from yutaro1985/add_fish_completion

Add fish shell completion
This commit is contained in:
Khosrow Moossavi
2023-12-18 18:13:01 -05:00
committed by GitHub
4 changed files with 59 additions and 16 deletions

View File

@@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/terraform-docs/terraform-docs/cmd/completion/bash"
"github.com/terraform-docs/terraform-docs/cmd/completion/fish"
"github.com/terraform-docs/terraform-docs/cmd/completion/zsh"
)
@@ -22,18 +23,19 @@ func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "completion SHELL",
Short: "Generate shell completion code for the specified shell (bash or zsh)",
Short: "Generate shell completion code for the specified shell (bash, zsh, fish)",
Long: longDescription,
}
// subcommands
cmd.AddCommand(bash.NewCommand())
cmd.AddCommand(zsh.NewCommand())
cmd.AddCommand(fish.NewCommand())
return cmd
}
const longDescription = `Outputs terraform-doc shell completion for the given shell (bash or zsh)
const longDescription = `Outputs terraform-doc shell completion for the given shell (bash, zsh, fish)
This depends on the bash-completion binary. Example installation instructions:
# for bash users
$ terraform-doc completion bash > ~/.terraform-doc-completion
@@ -45,6 +47,9 @@ This depends on the bash-completion binary. Example installation instructions:
# or if zsh-completion is installed via homebrew
% terraform-doc completion zsh > "${fpath[1]}/_terraform-doc"
# for fish users
$ terraform-doc completion fish > ~/.config/fish/completions/terraform-docs.fish
Additionally, you may want to output the completion to a file and source in your .bashrc
Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2
`

View File

@@ -0,0 +1,30 @@
/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package fish
import (
"os"
"github.com/spf13/cobra"
)
// NewCommand returns a new cobra.Command for 'completion fish' command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "fish",
Short: "Generate shell completion for fish",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Parent().Parent().GenFishCompletion(os.Stdout, true)
},
}
return cmd
}

View File

@@ -138,11 +138,17 @@ source <(terraform-docs completion bash)
### zsh
```bash
```zsh
terraform-docs completion zsh > /usr/local/share/zsh/site-functions/_terraform-docs
autoload -U compinit && compinit
```
### fish
```fish
terraform-doc completion fish > ~/.config/fish/completions/terraform-docs.fish
```
To make this change permanent, the above commands can be added to `~/.profile` file.
[Chocolatey]: https://www.chocolatey.org

View File

@@ -347,24 +347,26 @@ func (s sortmapkeys) Less(i, j int) bool { return s[i] < s[j] }
// MarshalXML custom marshal function which converts map to its literal
// XML representation. For example:
//
// m := Map{
// "a": 1,
// "b": 2,
// "c": 3,
// }
// m := Map{
// "a": 1,
// "b": 2,
// "c": 3,
// }
//
// type foo struct {
// Value Map `xml:"value"`
// }
// type foo struct {
// Value Map `xml:"value"`
// }
//
// will get marshaled to:
//
// <foo>
// <value>
// <a>1</a>
// <b>2</b>
// <c>3</c>
// </value>
//
// <value>
// <a>1</a>
// <b>2</b>
// <c>3</c>
// </value>
//
// </foo>
func (m Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {