From ba499a1ff26dd4df202c5a986f9c13c5081adcf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Fri, 26 Feb 2021 00:33:48 +0300 Subject: [PATCH] Add --in option to edit status of resource with specified yaml file --- cmd/edit_status.go | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/cmd/edit_status.go b/cmd/edit_status.go index 6b3e4ee..0e1f3c6 100644 --- a/cmd/edit_status.go +++ b/cmd/edit_status.go @@ -45,8 +45,12 @@ package cmd import ( "context" - "github.com/spf13/cobra" "io/ioutil" + "os" + "os/exec" + "strings" + + "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -56,15 +60,13 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/discovery" "k8s.io/client-go/rest" - "os" - "os/exec" - "strings" "k8s.io/client-go/dynamic" "k8s.io/client-go/restmapper" "sigs.k8s.io/yaml" "fmt" + jsonPatch "github.com/evanphx/json-patch" "k8s.io/cli-runtime/pkg/genericclioptions" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" @@ -110,6 +112,8 @@ type EditStatusOptions struct { originalJSon []byte genericclioptions.IOStreams + + filePath string } // NewEditStatusOptions provides an instance of EditStatusOptions with default values @@ -152,6 +156,7 @@ func NewCmdEditStatus(streams genericclioptions.IOStreams) *cobra.Command { fmt.Sprintf("editor to use. Either editor name in PATH or path to the editor executable. "+ "If not specified, first value of %q and then value of %q environment variables are substituted and checked", envKubeEditor, envEditor)) + cmd.Flags().StringVarP(&o.filePath, "in", "i", "", "Edit status of the resource with specified yaml file") // add K8s generic client flags o.configFlags.AddFlags(cmd.Flags()) @@ -248,6 +253,14 @@ func (o *EditStatusOptions) Run(_ *cobra.Command, _ []string) (err error) { return } + if o.filePath != "" { + if err = o.writeResourceStatusFromInputFile(); err != nil { + return + } + + return nil + } + if err = o.editResource(tmpEditFile); err != nil { return } @@ -365,3 +378,23 @@ func (o *EditStatusOptions) writeResourceStatus(f *os.File) error { return nil } + +func (o *EditStatusOptions) writeResourceStatusFromInputFile() error { + f, err := os.Open(o.filePath) + + if err != nil { + return err + } + + defer func() { + if errClose := f.Close(); err != nil { + err = errClose + } + }() + + if err = o.writeResourceStatus(f); err != nil { + return err + } + + return nil +}