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

dry-run added #127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 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,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 {
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
Loading