diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index 1906804..777b7db 100644 --- a/cmd/completion/completion.go +++ b/cmd/completion/completion.go @@ -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 ` diff --git a/cmd/completion/fish/fish.go b/cmd/completion/fish/fish.go new file mode 100644 index 0000000..a7e2e38 --- /dev/null +++ b/cmd/completion/fish/fish.go @@ -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 +} diff --git a/docs/user-guide/installation.md b/docs/user-guide/installation.md index adec7b1..b224afb 100644 --- a/docs/user-guide/installation.md +++ b/docs/user-guide/installation.md @@ -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 diff --git a/internal/types/types.go b/internal/types/types.go index 29371d2..a2b374a 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -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: // // -// -// 1 -// 2 -// 3 -// +// +// +// 1 +// 2 +// 3 +// +// // func (m Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error { if len(m) == 0 {