diff --git a/cmd/skupper/skupper.go b/cmd/skupper/skupper.go index c9a2a2e7d4..92966a578c 100644 --- a/cmd/skupper/skupper.go +++ b/cmd/skupper/skupper.go @@ -1039,12 +1039,27 @@ func addCommands(skupperCli SkupperClient, rootCmd *cobra.Command, cmds ...*cobr } } +func parsePlatformFlagOnly(args []string) { + // This function is called when not every supported command flag is defined yet + for i, p := range args { + if p == "--platform" && i < len(args)-1 { + config.Platform = args[i+1] + break + } else if strings.HasPrefix(p, "--platform=") { + _, v, _ := strings.Cut(p, "=") + config.Platform = v + break + } + } +} + func init() { rootCmd = &cobra.Command{Use: "skupper"} routev1.AddToScheme(scheme.Scheme) - rootCmd.PersistentFlags().StringVarP(&config.Platform, "platform", "", "", "The platform type to use [kubernetes, podman]") - rootCmd.ParseFlags(os.Args) + rootCmd.PersistentFlags().StringVarP(&config.Platform, "platform", "", "", "The platform type t [kubernetes, podman]") + // Only the "platform" flag is defined at this point so it is unsafe to call rootCmd.ParseFlags(os.Args) here + parsePlatformFlagOnly(os.Args) var skupperCli SkupperClient switch config.GetPlatform() { diff --git a/cmd/skupper/skupper_test.go b/cmd/skupper/skupper_test.go index b3ac795b00..78e0f943e0 100644 --- a/cmd/skupper/skupper_test.go +++ b/cmd/skupper/skupper_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/skupperproject/skupper/api/types" + "github.com/skupperproject/skupper/pkg/config" "gotest.tools/assert" ) @@ -317,3 +318,31 @@ func TestSkupperInitFlowCollectorParseArgs(t *testing.T) { assert.Equal(t, routerCreateOpts.FlowCollector.MemoryLimit, "3G") assert.Equal(t, routerCreateOpts.FlowCollector.FlowRecordTtl, time.Minute*15) } + +func TestParsePlatformFlagOnly(t *testing.T) { + config.Platform = "" + args := []string{"skupper", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080", "--platform", "podman"} + parsePlatformFlagOnly(args) + assert.Equal(t, config.Platform, types.PlatformPodman) + + config.Platform = "" + args = []string{"skupper", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080", "--platform=podman"} + parsePlatformFlagOnly(args) + assert.Equal(t, config.Platform, types.PlatformPodman) + + config.Platform = "" + args = []string{"skupper", "--platform", "podman", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080"} + parsePlatformFlagOnly(args) + assert.Equal(t, config.Platform, types.PlatformPodman) + + config.Platform = "" + args = []string{"skupper", "--platform=podman", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080"} + parsePlatformFlagOnly(args) + assert.Equal(t, config.Platform, types.PlatformPodman) + + // Bad value + config.Platform = "" + args = []string{"skupper", "--platform=", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080"} + parsePlatformFlagOnly(args) + assert.Equal(t, config.Platform, "") +}