diff --git a/cmd/project/executor.go b/cmd/project/executor.go index d09fdd7d..8adfd1ce 100644 --- a/cmd/project/executor.go +++ b/cmd/project/executor.go @@ -7,6 +7,24 @@ import ( "github.com/shopware/shopware-cli/internal/shop" ) +// readConfigWithEnvironment loads the project config with the -e/--env +// environment applied, so Admin API commands target the selected environment. +// Without -e the config is returned as-is: Admin API commands historically +// used the base url/admin_api, and an existing environments.local entry must +// not silently retarget them. +func readConfigWithEnvironment(cmd *cobra.Command, allowFallback bool) (*shop.Config, error) { + cfg, err := shop.ReadConfig(cmd.Context(), projectConfigPath, allowFallback) + if err != nil { + return nil, err + } + + if environmentName == "" { + return cfg, nil + } + + return cfg.WithEnvironment(environmentName) +} + // resolveExecutor returns the Executor for the current environment. func resolveExecutor(cmd *cobra.Command, projectRoot string) (executor.Executor, error) { cfg, err := shop.ReadConfig(cmd.Context(), projectConfigPath, true) diff --git a/cmd/project/project_admin_api.go b/cmd/project/project_admin_api.go index 92f833a1..7a0a2032 100644 --- a/cmd/project/project_admin_api.go +++ b/cmd/project/project_admin_api.go @@ -21,7 +21,7 @@ var projectAdminApiCmd = &cobra.Command{ var cfg *shop.Config var err error - if cfg, err = shop.ReadConfig(cobraCmd.Context(), projectConfigPath, false); err != nil { + if cfg, err = readConfigWithEnvironment(cobraCmd, false); err != nil { return err } diff --git a/cmd/project/project_clear_cache.go b/cmd/project/project_clear_cache.go index b92adec3..3dcee342 100644 --- a/cmd/project/project_clear_cache.go +++ b/cmd/project/project_clear_cache.go @@ -18,7 +18,7 @@ var projectClearCacheCmd = &cobra.Command{ var cfg *shop.Config var err error - if cfg, err = shop.ReadConfig(cmd.Context(), projectConfigPath, false); err != nil { + if cfg, err = readConfigWithEnvironment(cmd, false); err != nil { return err } diff --git a/cmd/project/project_env_resolution_test.go b/cmd/project/project_env_resolution_test.go new file mode 100644 index 00000000..9efa5714 --- /dev/null +++ b/cmd/project/project_env_resolution_test.go @@ -0,0 +1,145 @@ +package project + +import ( + "os" + "path/filepath" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// writeMinimalPlugin creates a minimal platform plugin the upload command can +// load, so its RunE reaches the environment-resolution step. +func writeMinimalPlugin(t *testing.T) string { + t.Helper() + + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "composer.json"), []byte(`{ + "name": "frosh/frosh-test", + "type": "shopware-platform-plugin", + "license": "MIT", + "version": "1.0.0", + "require": { "shopware/core": "~6.6.0" }, + "autoload": { "psr-4": { "FroshTest\\": "src/" } }, + "extra": { + "shopware-plugin-class": "FroshTest\\FroshTest", + "label": { "de-DE": "Test", "en-GB": "Test" } + } + }`), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(dir, ".shopware-extension.yml"), []byte( + "build:\n zip:\n composer:\n enabled: false\n assets:\n enabled: false\n", + ), 0o644)) + require.NoError(t, os.MkdirAll(filepath.Join(dir, "src"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "src", "FroshTest.php"), + []byte("