Skip to content

Commit

Permalink
update command
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei committed Oct 18, 2024
1 parent 48085a6 commit bb37f77
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 198 deletions.
11 changes: 10 additions & 1 deletion pkg/action/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"cuelang.org/go/cue/cuecontext"
cuejson "cuelang.org/go/encoding/json"
"github.com/leaanthony/debme"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -104,6 +105,14 @@ type CreateOptions struct {
genericiooptions.IOStreams
}

func (o *CreateOptions) AddCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.DryRun, "dry-run", "none", `Must be "client", or "server". If with client strategy, only print the object that would be sent, and no data is actually sent. If with server strategy, submit the server-side request, but no data is persistent.`)
cmd.Flags().Lookup("dry-run").NoOptDefVal = "unchanged"
cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating")
// add print flags
printer.AddOutputFlagForCreate(cmd, &o.Format, false)
}

func (o *CreateOptions) Complete() error {
var err error
if o.Namespace == "" {
Expand Down Expand Up @@ -159,7 +168,7 @@ func (o *CreateOptions) Run() error {
}

if o.EditBeforeCreate {
customEdit := NewCustomEditOptions(o.Factory, o.IOStreams, "create")
customEdit := NewCustomEditOptions(o.Factory, o.IOStreams, EditForCreate)
if err := customEdit.Run(resObj); err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/action/custom_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ import (
"github.com/apecloud/kbcli/pkg/util/prompt"
)

const (
EditForCreate = "create"
EditForPatched = "patched"
)

// CustomEditOptions is used to edit the resource manifest when creating or updating the resource,
// instead of using -o yaml to output the yaml file before editing the manifest.
type CustomEditOptions struct {
Expand Down Expand Up @@ -126,13 +131,13 @@ func (o *CustomEditOptions) Run(originalObj runtime.Object) error {
return fmt.Errorf("failed to decode edited object: %v", err)
}

if o.Method == "patched" {
if o.Method == EditForPatched {
diff, err := util.GetUnifiedDiffString(string(original), string(edited), "Original", "Current", 3)
if err != nil {
return fmt.Errorf("failed to get diff: %v", err)
}
util.DisplayDiffWithColor(o.IOStreams.Out, diff)
} else if o.Method == "create" {
} else if o.Method == EditForCreate {
err := editPrinter.PrintObj(originalObj, o.IOStreams.Out)
if err != nil {
return fmt.Errorf("failed to print object: %v", err)
Expand Down
Loading

0 comments on commit bb37f77

Please sign in to comment.