Skip to content
Merged
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
16 changes: 9 additions & 7 deletions cmd/env_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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))
}
Expand All @@ -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
Expand All @@ -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",
)
}
}
Expand Down
9 changes: 3 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " +
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 11 additions & 2 deletions pkg/config/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions pkg/config/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
}
Loading