ValidAliasNameFunc returns a function that will check if the given string is a valid alias name. A name is valid if: - it does not shadow an existing command, - it is not nested under a command that is runnable, - it is not nested under a command that does not exist.
(cmd *cobra.Command)
| 13 | // - it is not nested under a command that is runnable, |
| 14 | // - it is not nested under a command that does not exist. |
| 15 | func ValidAliasNameFunc(cmd *cobra.Command) func(string) bool { |
| 16 | return func(args string) bool { |
| 17 | split, err := shlex.Split(args) |
| 18 | if err != nil || len(split) == 0 { |
| 19 | return false |
| 20 | } |
| 21 | |
| 22 | rootCmd := cmd.Root() |
| 23 | foundCmd, foundArgs, _ := rootCmd.Find(split) |
| 24 | if foundCmd != nil && !foundCmd.Runnable() && len(foundArgs) == 1 { |
| 25 | return true |
| 26 | } |
| 27 | |
| 28 | return false |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // ValidAliasExpansionFunc returns a function that will check if the given string |
| 33 | // is a valid alias expansion. An expansion is valid if: |