From 810e238f1cb5b8aa3002d4efb5936bdcf0f0764a Mon Sep 17 00:00:00 2001 From: Jonathan Berkhahn Date: Thu, 9 Jul 2020 15:13:52 -0700 Subject: [PATCH] refactor cmd/cleanup/packagemanifests to be more testable --- .../packagemanifests/packagemanifests.go | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go b/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go index 30a45b3c404..80dc1fe22fe 100644 --- a/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go +++ b/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go @@ -36,22 +36,11 @@ func NewCmd() *cobra.Command { Long: `'cleanup packagemanifests' destroys an Operator deployed with OLM using the 'run packagemanifests' command. The command's argument must be set to a valid package manifests root directory, ex. '/packagemanifests'.`, + PreRunE: func(cmd *cobra.Command, args []string) error { + return c.validate(args) + }, RunE: func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { - if len(args) > 1 { - return fmt.Errorf("exactly one argument is required") - } - c.ManifestsDir = args[0] - } else { - c.ManifestsDir = "packagemanifests" - } - - log.Infof("Cleaning up operator in directory %s", c.ManifestsDir) - - if err := c.Cleanup(); err != nil { - log.Fatalf("Failed to clean up operator: %v", err) - } - return nil + return c.run() }, } @@ -59,3 +48,24 @@ ex. '/packagemanifests'.`, return cmd } + +func (c *packagemanifestsCmd) validate(args []string) error { + if len(args) > 0 { + if len(args) > 1 { + return fmt.Errorf("exactly one argument is required") + } + c.ManifestsDir = args[0] + } else { + c.ManifestsDir = "packagemanifests" + } + return nil +} + +func (c *packagemanifestsCmd) run() error { + log.Infof("Cleaning up operator in directory %s", c.ManifestsDir) + + if err := c.Cleanup(); err != nil { + log.Fatalf("Failed to clean up operator: %v", err) + } + return nil +}