| 140 | } |
| 141 | |
| 142 | func newJSONAwareHelpCommand(root *cobra.Command) *cobra.Command { |
| 143 | cmd := &cobra.Command{ |
| 144 | Use: "help [command]", |
| 145 | Short: "Help about any command", |
| 146 | Long: "Help provides help for any command in the application.\nSimply type dbxcli help [path to command] for full details.", |
| 147 | ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { |
| 148 | var completions []cobra.Completion |
| 149 | target, _, err := root.Find(args) |
| 150 | if err != nil { |
| 151 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 152 | } |
| 153 | if target == nil { |
| 154 | target = root |
| 155 | } |
| 156 | for _, child := range target.Commands() { |
| 157 | if (child.IsAvailableCommand() || child == cmd) && strings.HasPrefix(child.Name(), toComplete) { |
| 158 | completions = append(completions, cobra.CompletionWithDesc(child.Name(), child.Short)) |
| 159 | } |
| 160 | } |
| 161 | return completions, cobra.ShellCompDirectiveNoFileComp |
| 162 | }, |
| 163 | RunE: func(cmd *cobra.Command, args []string) error { |
| 164 | target, _, err := root.Find(args) |
| 165 | if target == nil || err != nil { |
| 166 | if shouldRenderJSONHelpForHelpCommand(cmd) { |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | return fmt.Errorf("unknown help topic %#q", args) |
| 171 | } |
| 172 | _, _ = fmt.Fprintf(cmd.OutOrStdout(), "Unknown help topic %#q\n", args) |
| 173 | return root.Usage() |
| 174 | } |
| 175 | if shouldRenderJSONHelpForHelpCommand(cmd) { |
| 176 | return renderJSONHelp(cmd, target) |
| 177 | } |
| 178 | target.InitDefaultHelpFlag() |
| 179 | target.InitDefaultVersionFlag() |
| 180 | return target.Help() |
| 181 | }, |
| 182 | } |
| 183 | if cmd.Annotations == nil { |
| 184 | cmd.Annotations = make(map[string]string) |
| 185 | } |
| 186 | cmd.Annotations[commandJSONHelpAnnotation] = "true" |
| 187 | return cmd |
| 188 | } |
| 189 | |
| 190 | func shouldRenderJSONHelpForHelpFlag(cmd *cobra.Command) bool { |
| 191 | if !jsonHelpOutputRequested(cmd) { |