genautocomplete.go (hugo-0.80.0) | : | genautocomplete.go (hugo-0.81.0) | ||
---|---|---|---|---|
skipping to change at line 17 | skipping to change at line 17 | |||
// | // | |||
// Unless required by applicable law or agreed to in writing, software | // Unless required by applicable law or agreed to in writing, software | |||
// distributed under the License is distributed on an "AS IS" BASIS, | // distributed under the License is distributed on an "AS IS" BASIS, | |||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
// See the License for the specific language governing permissions and | // See the License for the specific language governing permissions and | |||
// limitations under the License. | // limitations under the License. | |||
package commands | package commands | |||
import ( | import ( | |||
"io" | ||||
"os" | ||||
"github.com/spf13/cobra" | "github.com/spf13/cobra" | |||
jww "github.com/spf13/jwalterweatherman" | jww "github.com/spf13/jwalterweatherman" | |||
) | ) | |||
var _ cmder = (*genautocompleteCmd)(nil) | var _ cmder = (*genautocompleteCmd)(nil) | |||
type genautocompleteCmd struct { | type genautocompleteCmd struct { | |||
autocompleteTarget string | autocompleteTarget string | |||
// bash for now (zsh and others will come) | // bash, zsh, fish or powershell | |||
autocompleteType string | autocompleteType string | |||
*baseCmd | *baseCmd | |||
} | } | |||
func newGenautocompleteCmd() *genautocompleteCmd { | func newGenautocompleteCmd() *genautocompleteCmd { | |||
cc := &genautocompleteCmd{} | cc := &genautocompleteCmd{} | |||
cc.baseCmd = newBaseCmd(&cobra.Command{ | cc.baseCmd = newBaseCmd(&cobra.Command{ | |||
Use: "autocomplete", | Use: "autocomplete", | |||
Short: "Generate shell autocompletion script for Hugo", | Short: "Generate shell autocompletion script for Hugo", | |||
Long: `Generates a shell autocompletion script for Hugo. | Long: `Generates a shell autocompletion script for Hugo. | |||
NOTE: The current version supports Bash only. | ||||
This should work for *nix systems with Bash installed. | ||||
By default, the file is written directly to /etc/bash_completion.d | By default, the file is written directly to /etc/bash_completion.d | |||
for convenience, and the command may need superuser rights, e.g.: | for convenience, and the command may need superuser rights, e.g.: | |||
$ sudo hugo gen autocomplete | $ sudo hugo gen autocomplete | |||
Add ` + "`--completionfile=/path/to/file`" + ` flag to set alternative | Add ` + "`--completionfile=/path/to/file`" + ` flag to set alternative | |||
file-path and name. | file-path and name. | |||
Add ` + "`--type={bash, zsh, fish or powershell}`" + ` flag to set alternative | ||||
shell type. | ||||
Logout and in again to reload the completion scripts, | Logout and in again to reload the completion scripts, | |||
or just source them in directly: | or just source them in directly: | |||
$ . /etc/bash_completion`, | $ . /etc/bash_completion or /path/to/file`, | |||
RunE: func(cmd *cobra.Command, args []string) error { | RunE: func(cmd *cobra.Command, args []string) error { | |||
if cc.autocompleteType != "bash" { | var err error | |||
return newUserError("Only Bash is supported for n | var target io.Writer | |||
ow") | ||||
if cc.autocompleteTarget == "" { | ||||
target = os.Stdout | ||||
} else { | ||||
target, _ = os.OpenFile(cc.autocompleteTarget, os | ||||
.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||||
} | ||||
switch cc.autocompleteType { | ||||
case "bash": | ||||
err = cmd.Root().GenBashCompletion(target) | ||||
case "zsh": | ||||
err = cmd.Root().GenZshCompletion(target) | ||||
case "fish": | ||||
err = cmd.Root().GenFishCompletion(target, true) | ||||
case "powershell": | ||||
err = cmd.Root().GenPowerShellCompletion(target) | ||||
default: | ||||
return newUserError("Unsupported completion type" | ||||
) | ||||
} | } | |||
err := cmd.Root().GenBashCompletionFile(cc.autocompleteTa rget) | ||||
if err != nil { | if err != nil { | |||
return err | return err | |||
} | } | |||
jww.FEEDBACK.Println("Bash completion file for Hugo saved | if cc.autocompleteTarget != "" { | |||
to", cc.autocompleteTarget) | jww.FEEDBACK.Println(cc.autocompleteType+" comple | |||
tion file for Hugo saved to", cc.autocompleteTarget) | ||||
} | ||||
return nil | return nil | |||
}, | }, | |||
}) | }) | |||
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfi | cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteTarget, "completionfi | |||
le", "", "/etc/bash_completion.d/hugo.sh", "autocompletion file") | le", "f", "", "autocompletion file, defaults to stdout") | |||
cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "", "ba | cc.cmd.PersistentFlags().StringVarP(&cc.autocompleteType, "type", "t", "b | |||
sh", "autocompletion type (currently only bash supported)") | ash", "autocompletion type (bash, zsh, fish, or powershell)") | |||
// For bash-completion | ||||
cc.cmd.PersistentFlags().SetAnnotation("completionfile", cobra.BashCompFi | ||||
lenameExt, []string{}) | ||||
return cc | return cc | |||
} | } | |||
End of changes. 9 change blocks. | ||||
20 lines changed or deleted | 38 lines changed or added |