Skip to content

Commit

Permalink
dry-run is bool & create SimplePrint
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Ishan-Khojaev committed Feb 13, 2024
1 parent 1ad1451 commit 0881507
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
20 changes: 13 additions & 7 deletions pkg/genericcli/cmds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package genericcli

import (
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -173,25 +174,30 @@ 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())
},
}

//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)

Expand Down
5 changes: 5 additions & 0 deletions pkg/genericcli/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0881507

Please sign in to comment.