diff --git a/pkg/genericcli/cmds.go b/pkg/genericcli/cmds.go index cbbb3d3..88ae093 100644 --- a/pkg/genericcli/cmds.go +++ b/pkg/genericcli/cmds.go @@ -1,6 +1,7 @@ package genericcli import ( + "errors" "fmt" "io" "strings" @@ -173,15 +174,31 @@ func NewCmds[C any, U any, R any](c *CmdsConfig[C, U, R], additionalCmds ...*cob return err } + 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()) }, } + //Adding dry-run flas to the command + cmd.Flags().Bool("dry-run", false, "Ser dry-run mode. When set only prints the output") + c.addFileFlags(cmd) if c.CreateCmdMutateFn != nil { 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