Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a command-line option to edit status of resource with specified yaml file #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions cmd/edit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -110,6 +112,8 @@ type EditStatusOptions struct {
originalJSon []byte

genericclioptions.IOStreams

filePath string
}

// NewEditStatusOptions provides an instance of EditStatusOptions with default values
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Comment on lines +257 to +261
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err = o.writeResourceStatusFromInputFile(); err != nil {
return
}
return nil
return o.writeResourceStatusFromInputFile()

}

if err = o.editResource(tmpEditFile); err != nil {
return
}
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will not be returning the IO error from File.Close from writeResourceStatusFromInputFile. In order for this to work, you should be assigning the error in the deferred call to a named return value. For an example, please see the deferred call in EditStatusOptions.Run.

}
}()

if err = o.writeResourceStatus(f); err != nil {
return err
}

return nil
Comment on lines +395 to +399
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err = o.writeResourceStatus(f); err != nil {
return err
}
return nil
return o.writeResourceStatus(f)

}