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

     1  /*
     2  Copyright 2020 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 //nolint:dupl
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    25  )
    26  
    27  const editErrorMsg = "failed to edit project"
    28  
    29  func (c CLI) newEditCmd() *cobra.Command {
    30  	cmd := &cobra.Command{
    31  		Use:   "edit",
    32  		Short: "Update the project configuration",
    33  		Long: `Edit the project configuration.
    34  `,
    35  		RunE: errCmdFunc(
    36  			fmt.Errorf("project must be initialized"),
    37  		),
    38  	}
    39  
    40  	// In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
    41  	// this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
    42  	if len(c.resolvedPlugins) == 0 {
    43  		cmdErr(cmd, noResolvedPluginError{})
    44  		return cmd
    45  	}
    46  
    47  	// Obtain the plugin keys and subcommands from the plugins that implement plugin.Edit.
    48  	subcommands := c.filterSubcommands(
    49  		func(p plugin.Plugin) bool {
    50  			_, isValid := p.(plugin.Edit)
    51  			return isValid
    52  		},
    53  		func(p plugin.Plugin) plugin.Subcommand {
    54  			return p.(plugin.Edit).GetEditSubcommand()
    55  		},
    56  	)
    57  
    58  	// Verify that there is at least one remaining plugin.
    59  	if len(subcommands) == 0 {
    60  		cmdErr(cmd, noAvailablePluginError{"edit project"})
    61  		return cmd
    62  	}
    63  
    64  	c.applySubcommandHooks(cmd, subcommands, editErrorMsg, false)
    65  
    66  	return cmd
    67  }