sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/cli/alpha.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cli
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/spf13/cobra"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/cli/alpha"
    25  )
    26  
    27  const (
    28  	alphaCommand = "alpha"
    29  )
    30  
    31  var alphaCommands = []*cobra.Command{
    32  	newAlphaCommand(),
    33  	alpha.NewScaffoldCommand(),
    34  }
    35  
    36  func newAlphaCommand() *cobra.Command {
    37  	cmd := &cobra.Command{
    38  		//TODO: If we need to create alpha commands please add a new file for each command
    39  	}
    40  	return cmd
    41  }
    42  
    43  func (c *CLI) newAlphaCmd() *cobra.Command {
    44  	alpha := &cobra.Command{
    45  		Use:        alphaCommand,
    46  		SuggestFor: []string{"experimental"},
    47  		Short:      "Alpha-stage subcommands",
    48  		Long: strings.TrimSpace(`
    49  Alpha subcommands are for unstable features.
    50  
    51  - Alpha subcommands are exploratory and may be removed without warning.
    52  - No backwards compatibility is provided for any alpha subcommands.
    53  `),
    54  	}
    55  	// TODO: Add alpha commands here if we need to have them
    56  	for i := range alphaCommands {
    57  		alpha.AddCommand(alphaCommands[i])
    58  	}
    59  	return alpha
    60  }
    61  
    62  func (c *CLI) addAlphaCmd() {
    63  	if (len(alphaCommands) + len(c.extraAlphaCommands)) > 0 {
    64  		c.cmd.AddCommand(c.newAlphaCmd())
    65  	}
    66  }
    67  
    68  func (c *CLI) addExtraAlphaCommands() error {
    69  	// Search for the alpha subcommand
    70  	var alpha *cobra.Command
    71  	for _, subCmd := range c.cmd.Commands() {
    72  		if subCmd.Name() == alphaCommand {
    73  			alpha = subCmd
    74  			break
    75  		}
    76  	}
    77  	if alpha == nil {
    78  		return fmt.Errorf("no %q command found", alphaCommand)
    79  	}
    80  
    81  	for _, cmd := range c.extraAlphaCommands {
    82  		for _, subCmd := range alpha.Commands() {
    83  			if cmd.Name() == subCmd.Name() {
    84  				return fmt.Errorf("command %q already exists", fmt.Sprintf("%s %s", alphaCommand, cmd.Name()))
    85  			}
    86  		}
    87  		alpha.AddCommand(cmd)
    88  	}
    89  	return nil
    90  }