diff --git a/cmd/create/network/cmd.go b/cmd/create/network/cmd.go index 479fa7d77..611ebc637 100644 --- a/cmd/create/network/cmd.go +++ b/cmd/create/network/cmd.go @@ -72,7 +72,8 @@ func NetworkRunner(userOptions *opts.NetworkUserOptions) rosa.CommandRunner { var err error templateCommand := "rosa-quickstart-default-vpc" options := NewNetworkOptions() - options.args = userOptions + userOptions.CleanTemplateDir() + options.Bind(userOptions) defer r.Cleanup() diff --git a/cmd/create/network/options.go b/cmd/create/network/options.go index b5c995494..2f9f31ed2 100644 --- a/cmd/create/network/options.go +++ b/cmd/create/network/options.go @@ -11,6 +11,10 @@ type Options struct { args *opts.NetworkUserOptions } +func (o *Options) Bind(userOptions *opts.NetworkUserOptions) { + o.args = userOptions +} + func NewNetworkUserOptions() *opts.NetworkUserOptions { return &opts.NetworkUserOptions{ Params: []string{}, diff --git a/pkg/options/network/create.go b/pkg/options/network/create.go index e1577c211..7898862aa 100644 --- a/pkg/options/network/create.go +++ b/pkg/options/network/create.go @@ -6,7 +6,6 @@ import ( "github.com/spf13/cobra" "github.com/openshift/rosa/pkg/constants" - "github.com/openshift/rosa/pkg/helper" "github.com/openshift/rosa/pkg/reporter" ) @@ -38,8 +37,11 @@ func NewNetworkUserOptions() *NetworkUserOptions { options := &NetworkUserOptions{} // Set template directory from environment variable or use default - templateDir := os.Getenv(constants.OcmTemplateDir) - if helper.HandleEscapedEmptyString(templateDir) != "" { + templateDir, isSet := os.LookupEnv(constants.OcmTemplateDir) + if isSet { + if templateDir == "\"\"" { + templateDir = "" + } options.TemplateDir = templateDir } else { options.TemplateDir = defaultTemplateDir @@ -48,6 +50,13 @@ func NewNetworkUserOptions() *NetworkUserOptions { return options } +func (n *NetworkUserOptions) CleanTemplateDir() { + // Clean up trailing '/' to work with filepath logic later + if len(n.TemplateDir) > 0 && n.TemplateDir[len(n.TemplateDir)-1] == '/' { + n.TemplateDir = n.TemplateDir[:len(n.TemplateDir)-1] + } +} + func NewNetworkOptions() *NetworkOptions { return &NetworkOptions{ reporter: reporter.CreateReporter(), @@ -72,9 +81,18 @@ func BuildNetworkCommandWithOptions() (*cobra.Command, *NetworkUserOptions) { } flags := cmd.Flags() - flags.StringVar(&options.TemplateDir, "template-dir", defaultTemplateDir, "Use a specific template directory,"+ - " overriding the OCM_TEMPLATE_DIR environment variable.") - flags.StringArrayVar(&options.Params, "param", []string{}, "List of parameters") + flags.StringVar( + &options.TemplateDir, + "template-dir", + options.TemplateDir, + "Use a specific template directory, overriding the OCM_TEMPLATE_DIR environment variable.", + ) + flags.StringArrayVar( + &options.Params, + "param", + []string{}, + "List of parameters", + ) return cmd, options } diff --git a/pkg/options/network/create_test.go b/pkg/options/network/create_test.go index cc02d1deb..d6ca8668c 100644 --- a/pkg/options/network/create_test.go +++ b/pkg/options/network/create_test.go @@ -1,10 +1,13 @@ package network import ( + "os" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/spf13/cobra" + "github.com/openshift/rosa/pkg/constants" "github.com/openshift/rosa/pkg/reporter" ) @@ -32,6 +35,40 @@ var _ = Describe("BuildMachinePoolCreateCommandWithOptions", func() { }) }) + Context("NewNetworkUserOptions", func() { + It("should create default network options with env var", func() { + testDir := "test/1" + Expect(os.Setenv(constants.OcmTemplateDir, testDir)).ToNot(HaveOccurred()) + options := NewNetworkUserOptions() + options.CleanTemplateDir() + Expect(options.TemplateDir).To(Equal(testDir)) + }) + It("should create default network options with env var which has trailing '/' and remove it", func() { + testDir := "/test/1/" + finalTestDir := "/test/1" + Expect(os.Setenv(constants.OcmTemplateDir, testDir)).ToNot(HaveOccurred()) + options := NewNetworkUserOptions() + options.CleanTemplateDir() + Expect(options.TemplateDir).To(Equal(finalTestDir)) + }) + It("should create default network options with env var which has trailing '/' and remove it", func() { + testDir := "/" + finalTestDir := "" + Expect(os.Setenv(constants.OcmTemplateDir, testDir)).ToNot(HaveOccurred()) + options := NewNetworkUserOptions() + options.CleanTemplateDir() + Expect(options.TemplateDir).To(Equal(finalTestDir)) + }) + It("should create default network options with empty dir and not fail", func() { + testDir := "" + finalTestDir := "" + Expect(os.Setenv(constants.OcmTemplateDir, testDir)).ToNot(HaveOccurred()) + options := NewNetworkUserOptions() + options.CleanTemplateDir() + Expect(options.TemplateDir).To(Equal(finalTestDir)) + }) + }) + Context("NetworkOptions.Network", func() { It("should return the args field", func() { options := NewNetworkOptions()