github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/edit/edit.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package edit
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/spf13/cobra"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/cli-runtime/pkg/genericiooptions"
    28  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    29  	"k8s.io/kubectl/pkg/cmd/util/editor"
    30  
    31  	"github.com/1aal/kubeblocks/pkg/cli/util"
    32  )
    33  
    34  type EditOptions struct {
    35  	editor.EditOptions
    36  	Factory cmdutil.Factory
    37  	// Name is the resource name
    38  	Name string
    39  	GVR  schema.GroupVersionResource
    40  }
    41  
    42  func NewEditOptions(f cmdutil.Factory, streams genericiooptions.IOStreams,
    43  	gvr schema.GroupVersionResource) *EditOptions {
    44  	return &EditOptions{
    45  		Factory:     f,
    46  		GVR:         gvr,
    47  		EditOptions: *editor.NewEditOptions(editor.NormalEditMode, streams),
    48  	}
    49  }
    50  
    51  func (o *EditOptions) AddFlags(cmd *cobra.Command) {
    52  	// bind flag structs
    53  	o.RecordFlags.AddFlags(cmd)
    54  	o.PrintFlags.AddFlags(cmd)
    55  
    56  	usage := "to use to edit the resource"
    57  	cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
    58  	cmdutil.AddValidateFlags(cmd)
    59  	cmd.Flags().BoolVarP(&o.OutputPatch, "output-patch", "", o.OutputPatch, "Output the patch if the resource is edited.")
    60  	cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
    61  		"Defaults to the line ending native to your platform.")
    62  	cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-edit")
    63  	cmdutil.AddApplyAnnotationVarFlags(cmd, &o.ApplyAnnotation)
    64  	cmdutil.AddSubresourceFlags(cmd, &o.Subresource, "If specified, edit will operate on the subresource of the requested object.", editor.SupportedSubresources...)
    65  }
    66  
    67  func (o *EditOptions) Complete(cmd *cobra.Command, args []string) error {
    68  	if len(args) == 0 {
    69  		return fmt.Errorf("missing the name")
    70  	}
    71  	if len(args) > 0 {
    72  		o.Name = args[0]
    73  	}
    74  	return o.EditOptions.Complete(o.Factory, []string{util.GVRToString(o.GVR), o.Name}, cmd)
    75  }