sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/edit.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 v4
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spf13/pflag"
    23  
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    27  	"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds"
    28  )
    29  
    30  var _ plugin.EditSubcommand = &editSubcommand{}
    31  
    32  type editSubcommand struct {
    33  	config config.Config
    34  
    35  	multigroup bool
    36  }
    37  
    38  func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
    39  	subcmdMeta.Description = `This command will edit the project configuration.
    40  Features supported:
    41    - Toggle between single or multi group projects.
    42  `
    43  	subcmdMeta.Examples = fmt.Sprintf(`  # Enable the multigroup layout
    44    %[1]s edit --multigroup
    45  
    46    # Disable the multigroup layout
    47    %[1]s edit --multigroup=false
    48  `, cliMeta.CommandName)
    49  }
    50  
    51  func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
    52  	fs.BoolVar(&p.multigroup, "multigroup", false, "enable or disable multigroup layout")
    53  }
    54  
    55  func (p *editSubcommand) InjectConfig(c config.Config) error {
    56  	p.config = c
    57  
    58  	return nil
    59  }
    60  
    61  func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
    62  	scaffolder := scaffolds.NewEditScaffolder(p.config, p.multigroup)
    63  	scaffolder.InjectFS(fs)
    64  	return scaffolder.Scaffold()
    65  }