From 1ad14512e8b622fde2b1ff5821659e5e377db5a1 Mon Sep 17 00:00:00 2001 From: Aziz Ishan-Khojaev Date: Tue, 13 Feb 2024 14:41:51 +0100 Subject: [PATCH 1/2] dry-run added --- pkg/genericcli/cmds.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/genericcli/cmds.go b/pkg/genericcli/cmds.go index cbbb3d3..fc200ed 100644 --- a/pkg/genericcli/cmds.go +++ b/pkg/genericcli/cmds.go @@ -173,6 +173,14 @@ func NewCmds[C any, U any, R any](c *CmdsConfig[C, U, R], additionalCmds ...*cob return err } + if viper.GetString("dry-run") == "client" { + fmt.Println("Create request as client:", rq) + return nil + } else if viper.GetString("dry-run") == "server" { + fmt.Println("Create request as server:", rq) + return nil + } + return c.GenericCLI.CreateAndPrint(rq, c.DescribePrinter()) } @@ -182,6 +190,9 @@ func NewCmds[C any, U any, R any](c *CmdsConfig[C, U, R], additionalCmds ...*cob }, } + //Adding dry-run flas to the command + cmd.Flags().String("dry-run", "", "Ser dry-run mode. Values: client, server") + c.addFileFlags(cmd) if c.CreateCmdMutateFn != nil { From 08815077ca32d9f3a8e04aa07d8af94def2cf159 Mon Sep 17 00:00:00 2001 From: Aziz Ishan-Khojaev Date: Tue, 13 Feb 2024 19:09:41 +0100 Subject: [PATCH 2/2] dry-run is bool & create SimplePrint --- pkg/genericcli/cmds.go | 20 +++++++++++++------- pkg/genericcli/crud.go | 5 +++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/genericcli/cmds.go b/pkg/genericcli/cmds.go index fc200ed..88ae093 100644 --- a/pkg/genericcli/cmds.go +++ b/pkg/genericcli/cmds.go @@ -1,6 +1,7 @@ package genericcli import ( + "errors" "fmt" "io" "strings" @@ -173,17 +174,22 @@ func NewCmds[C any, U any, R any](c *CmdsConfig[C, U, R], additionalCmds ...*cob return err } - if viper.GetString("dry-run") == "client" { - fmt.Println("Create request as client:", rq) - return nil - } else if viper.GetString("dry-run") == "server" { - fmt.Println("Create request as server:", rq) - return nil + if viper.GetBool("dry-run") { + + // If dry-run flas is set, print and return + + return c.GenericCLI.SimplePrint(rq, c.DescribePrinter()) } return c.GenericCLI.CreateAndPrint(rq, c.DescribePrinter()) } + // In case -f is set and dry-run is true + if viper.GetBool("dry-run") && viper.IsSet("file") { + // Errror out + return errors.New("cannot use dry-run in combination with -f") + } + p := c.evalBulkFlags() return c.GenericCLI.CreateFromFileAndPrint(viper.GetString("file"), p()) @@ -191,7 +197,7 @@ func NewCmds[C any, U any, R any](c *CmdsConfig[C, U, R], additionalCmds ...*cob } //Adding dry-run flas to the command - cmd.Flags().String("dry-run", "", "Ser dry-run mode. Values: client, server") + cmd.Flags().Bool("dry-run", false, "Ser dry-run mode. When set only prints the output") c.addFileFlags(cmd) diff --git a/pkg/genericcli/crud.go b/pkg/genericcli/crud.go index d71e993..2833274 100644 --- a/pkg/genericcli/crud.go +++ b/pkg/genericcli/crud.go @@ -102,6 +102,11 @@ func (a *GenericCLI[C, U, R]) CreateAndPrint(rq C, p printers.Printer) error { return p.Print(resp) } +func (a *GenericCLI[C, U, R]) SimplePrint(rq C, p printers.Printer) error { + + return p.Print(rq) +} + func (a *GenericCLI[C, U, R]) Update(rq U) (R, error) { var zero R