diff --git a/cmd/env_flags_test.go b/cmd/env_flags_test.go index 62a3d59c5..d9b2bc2d2 100644 --- a/cmd/env_flags_test.go +++ b/cmd/env_flags_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + "github.com/devsy-org/devsy/pkg/config" pkgflags "github.com/devsy-org/devsy/pkg/flags" "github.com/devsy-org/devsy/pkg/flags/names" "github.com/spf13/cobra" @@ -23,7 +24,7 @@ const ( // setting the corresponding DEVSY_* env var pushes the value into the bound // pflag at registration time, satisfying cobra's required-flag check. func TestOptInEnvFlags_AppliesEnvValueToFlag(t *testing.T) { - t.Setenv(envPlatformEnabled, "true") + t.Setenv(config.EnvPlatformEnabled, "true") cases := []struct { cmdPath string flagName string @@ -99,7 +100,7 @@ func TestOptInEnvFlags_NoEnvLeavesDefault(t *testing.T) { // BindEnv: setting DEVSY_HOST="" must NOT mark the flag as Changed (otherwise // MarkFlagRequired would be satisfied by an empty value). func TestOptInEnvFlags_EmptyEnvIsNoOp(t *testing.T) { - t.Setenv(envPlatformEnabled, "true") + t.Setenv(config.EnvPlatformEnabled, "true") t.Setenv(envDevsyHost, "") rootCmd, _ := BuildRoot() cmd := resolveCommand(t, rootCmd, "platform workspace list") @@ -112,7 +113,7 @@ func TestOptInEnvFlags_EmptyEnvIsNoOp(t *testing.T) { // wins over a value supplied via DEVSY_*. This is the standard precedence // users expect: flag > env > default. func TestOptInEnvFlags_CLIOverridesEnv(t *testing.T) { - t.Setenv(envPlatformEnabled, "true") + t.Setenv(config.EnvPlatformEnabled, "true") t.Setenv(envDevsyHost, "env-value") rootCmd, _ := BuildRoot() // Replace the leaf command's RunE so Execute() doesn't try to actually @@ -130,7 +131,7 @@ func TestOptInEnvFlags_CLIOverridesEnv(t *testing.T) { } func TestOptInEnvFlags_EnvSatisfiesRequired(t *testing.T) { - t.Setenv(envPlatformEnabled, "true") + t.Setenv(config.EnvPlatformEnabled, "true") t.Setenv(envDevsyHost, "from-env.example.com") rootCmd, _ := BuildRoot() leaf := resolveCommand(t, rootCmd, "platform workspace list") @@ -149,7 +150,7 @@ func TestOptInEnvFlags_EnvSatisfiesRequired(t *testing.T) { // This catches the regression where a future addition to cmd/platform/ forgets // the inline BindEnv call. func TestOptInEnvFlags_PlatformSubcommandSweep(t *testing.T) { - t.Setenv(envPlatformEnabled, "true") + t.Setenv(config.EnvPlatformEnabled, "true") for _, env := range []string{envDevsyHost, envDevsyProject} { t.Setenv(env, "sweep-"+strings.ToLower(env)) } @@ -175,7 +176,8 @@ func TestOptInEnvFlags_PlatformSubcommandSweep(t *testing.T) { // TestPlatformCommand_HiddenWhenDisabled verifies platform is gated off by default. func TestPlatformCommand_HiddenWhenDisabled(t *testing.T) { - t.Setenv(envPlatformEnabled, "") + t.Setenv(config.EnvPlatformEnabled, "") + t.Setenv(config.EnvPlatformEnabledLegacy, "") rootCmd, _ := BuildRoot() for _, c := range rootCmd.Commands() { use := c.Use @@ -186,7 +188,7 @@ func TestPlatformCommand_HiddenWhenDisabled(t *testing.T) { t, "platform", use, - "platform command should not be registered when DEVSY_PRO_ENABLED is unset", + "platform command should not be registered when DEVSY_PLATFORM_ENABLED is unset", ) } } diff --git a/cmd/root.go b/cmd/root.go index c607e65f4..f93898b6e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,11 +58,6 @@ const ( flagLogOutput = "--" + names.LogOutput flagLogFormat = "--" + names.LogFormat - // envPlatformEnabled gates registration of the `pro` command tree. The pro - // feature is not ready for general use; set DEVSY_PRO_ENABLED=true to - // expose it (e.g. for internal testing). - envPlatformEnabled = "DEVSY_PRO_ENABLED" - internalCommand = "internal" rootLong = "Devsy - standardized development workspaces built on devcontainers, " + @@ -81,8 +76,10 @@ const ( $ devsy provider add docker` ) +// platformEnabled keeps the platform command tree off until DEVSY_PLATFORM_ENABLED=true; +// the feature is not ready for general use. func platformEnabled() bool { - return os.Getenv(envPlatformEnabled) == "true" + return config.IsPlatformEnabled() } // isMachineLogFormat reports whether the configured --log-output mode produces diff --git a/pkg/config/env.go b/pkg/config/env.go index 408552fa4..35a606303 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -47,6 +47,12 @@ const ( // EnvPlatformRunnerLegacy is the pre-rename runner marker still set by existing runner pods. EnvPlatformRunnerLegacy = "DEVSY_PRO_RUNNER" + // EnvPlatformEnabled exposes the platform command tree when set to "true". + EnvPlatformEnabled = "DEVSY_PLATFORM_ENABLED" + + // EnvPlatformEnabledLegacy is the pre-rename switch for the platform command tree. + EnvPlatformEnabledLegacy = "DEVSY_PRO_ENABLED" + // EnvTelemetryDistinctID is set by the desktop app on spawned CLI // processes so their events share the desktop's analytics identity. EnvTelemetryDistinctID = "DEVSY_TELEMETRY_DISTINCT_ID" diff --git a/pkg/config/platform.go b/pkg/config/platform.go index 9f8abb7f2..48b9a855d 100644 --- a/pkg/config/platform.go +++ b/pkg/config/platform.go @@ -4,8 +4,17 @@ import "os" // IsPlatformRunner reports whether the CLI runs inside a Platform runner pod; the new env var wins when set. func IsPlatformRunner() bool { - if value := os.Getenv(EnvPlatformRunner); value != "" { + return boolEnvWithLegacy(EnvPlatformRunner, EnvPlatformRunnerLegacy) +} + +// IsPlatformEnabled reports whether the platform command tree is exposed; the new env var wins when set. +func IsPlatformEnabled() bool { + return boolEnvWithLegacy(EnvPlatformEnabled, EnvPlatformEnabledLegacy) +} + +func boolEnvWithLegacy(name, legacyName string) bool { + if value := os.Getenv(name); value != "" { return value == BoolTrue } - return os.Getenv(EnvPlatformRunnerLegacy) == BoolTrue + return os.Getenv(legacyName) == BoolTrue } diff --git a/pkg/config/platform_test.go b/pkg/config/platform_test.go index fd6d27129..468a997bf 100644 --- a/pkg/config/platform_test.go +++ b/pkg/config/platform_test.go @@ -40,3 +40,30 @@ func TestIsPlatformRunner(t *testing.T) { }) } } + +func TestIsPlatformEnabled(t *testing.T) { + tests := []struct { + name string + newValue string + legacyValue string + want bool + }{ + {name: "neither set", want: false}, + {name: "only new set", newValue: BoolTrue, want: true}, + {name: "only legacy set", legacyValue: BoolTrue, want: true}, + {name: "new false beats legacy true", newValue: BoolFalse, legacyValue: BoolTrue}, + { + name: "new true beats legacy false", + newValue: BoolTrue, + legacyValue: BoolFalse, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Setenv(EnvPlatformEnabled, tt.newValue) + t.Setenv(EnvPlatformEnabledLegacy, tt.legacyValue) + assert.Equal(t, tt.want, IsPlatformEnabled()) + }) + } +}