Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

//nolint:dupl
package cli

import (
Expand All @@ -37,6 +36,19 @@ func (c CLI) newCreateAPICmd() *cobra.Command {
),
}

// Show hint message on how to list flags instead of showing file completion
cmd.ValidArgsFunction = func(
_ *cobra.Command,
args []string,
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list more flags")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camilamacedo86 I removed the group flag requirement and added ActiveHelp to hint users about other flags. It's going to work like this:

Only version and kind are marked as required, so completion shows them proactively:

$ kubebuilder create api<TAB>
--kind     (Resource Kind (e.g., CronJob, Deployment))  --version  (Resource Version (e.g., v1, v1beta1))

After the two flags are passed, tab completion shows a hint at other flags (Cobra's ActiveHelp):

$ kubebuilder create api --kind MyKind --version v1alpha 
Type '--' and press TAB to list more flags

I'm not 100% happy with the hint message, though. We can change that.

}
return completions, cobra.ShellCompDirectiveNoFileComp
}

// In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
// this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
if len(c.resolvedPlugins) == 0 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ func initializationHooks(
var options *resourceOptions
if requiresResource {
options = bindResourceFlags(cmd.Flags())
if err := cmd.MarkFlagRequired("version"); err != nil {
return nil, fmt.Errorf("failed to mark 'version' flag as required: %w", err)
}
if err := cmd.MarkFlagRequired("kind"); err != nil {
return nil, fmt.Errorf("failed to mark 'kind' flag as required: %w", err)
}
Comment on lines +271 to +276
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added proper err check, as pointed by @copilot.

}

// Bind flags hook: each plugin binds to a temporary FlagSet, then we merge into the command so
Expand Down