From 38a327a66c08c13d47805b42119f1e494bb72c82 Mon Sep 17 00:00:00 2001 From: Yutaro Suzuki Date: Fri, 30 Jun 2023 02:32:29 +0900 Subject: [PATCH 1/5] Add fish shell completion Signed-off-by: Yutaro Suzuki --- cmd/completion/completion.go | 9 +++++++-- cmd/completion/fish/fish.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 cmd/completion/fish/fish.go diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index 1906804..d9985d4 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 | source + 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..ff26335 --- /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 shel completion for fish", + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Parent().Parent().GenFishCompletion(os.Stdout, true) + }, + } + return cmd +} From a375aa7d4248f84825db4a8b74befed8a11f5a3a Mon Sep 17 00:00:00 2001 From: Yutaro Suzuki Date: Fri, 30 Jun 2023 04:08:18 +0900 Subject: [PATCH 2/5] fix spaces Signed-off-by: Yutaro Suzuki --- cmd/completion/completion.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index d9985d4..a5322ba 100644 --- a/cmd/completion/completion.go +++ b/cmd/completion/completion.go @@ -23,7 +23,7 @@ func NewCommand() *cobra.Command { cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "completion SHELL", - Short: "Generate shell completion code for the specified shell (bash,zsh,fish)", + Short: "Generate shell completion code for the specified shell (bash, zsh, fish)", Long: longDescription, } @@ -35,7 +35,7 @@ func NewCommand() *cobra.Command { return cmd } -const longDescription = `Outputs terraform-doc shell completion for the given shell (bash,zsh,fish) +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 From 090efce37a8218738a5aca0a0d987e370396ce97 Mon Sep 17 00:00:00 2001 From: Yutaro Suzuki Date: Fri, 30 Jun 2023 04:33:06 +0900 Subject: [PATCH 3/5] Run make fmt Signed-off-by: Yutaro Suzuki --- internal/types/types.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) 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 { From eb2e3b77ea4c1801c2632e23720aedcd57d5fcd7 Mon Sep 17 00:00:00 2001 From: Yutaro Suzuki Date: Fri, 30 Jun 2023 06:10:10 +0900 Subject: [PATCH 4/5] fix completion installing command Signed-off-by: Yutaro Suzuki --- cmd/completion/completion.go | 2 +- docs/user-guide/installation.md | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/completion/completion.go b/cmd/completion/completion.go index a5322ba..777b7db 100644 --- a/cmd/completion/completion.go +++ b/cmd/completion/completion.go @@ -48,7 +48,7 @@ This depends on the bash-completion binary. Example installation instructions: % terraform-doc completion zsh > "${fpath[1]}/_terraform-doc" # for fish users - $ terraform-doc completion fish | source + $ 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/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 From 9449cf8e7d2df6a56f1329a8b51a67245966f4df Mon Sep 17 00:00:00 2001 From: Khosrow Moossavi Date: Mon, 18 Dec 2023 18:10:12 -0500 Subject: [PATCH 5/5] fix typo --- cmd/completion/fish/fish.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/completion/fish/fish.go b/cmd/completion/fish/fish.go index ff26335..a7e2e38 100644 --- a/cmd/completion/fish/fish.go +++ b/cmd/completion/fish/fish.go @@ -21,7 +21,7 @@ func NewCommand() *cobra.Command { cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "fish", - Short: "Generate shel completion for fish", + Short: "Generate shell completion for fish", RunE: func(cmd *cobra.Command, args []string) error { return cmd.Parent().Parent().GenFishCompletion(os.Stdout, true) },