From bbc5835eec3b07359073b8b37b98fa5c77a52b24 Mon Sep 17 00:00:00 2001 From: Yionse Date: Thu, 9 Jan 2025 14:52:26 +0800 Subject: [PATCH 1/3] Update strategy --- cli/azd/pkg/ext/hooks_runner.go | 1 + cli/azd/pkg/ext/models.go | 5 ++++- cli/azd/pkg/tools/powershell/powershell.go | 2 +- cli/azd/pkg/tools/script.go | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index 2d87378568b..15c7855f539 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -158,6 +158,7 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti options.StdOut = previewer defer h.console.StopPreviewer(ctx, false) } + options.UserPwsh = hookConfig.UserPwsh log.Printf("Executing script '%s'\n", hookConfig.path) res, err := script.Execute(ctx, hookConfig.path, *options) diff --git a/cli/azd/pkg/ext/models.go b/cli/azd/pkg/ext/models.go index 1100c6a5ee6..e2a9f28e088 100644 --- a/cli/azd/pkg/ext/models.go +++ b/cli/azd/pkg/ext/models.go @@ -70,6 +70,8 @@ type HookConfig struct { Windows *HookConfig `yaml:"windows,omitempty"` // When running on linux/macos use this override config Posix *HookConfig `yaml:"posix,omitempty"` + // In order to support pwsh parameter passing, this parameter is required to store the shell value passed in by the user + UserPwsh string } // Validates and normalizes the hook configuration @@ -77,7 +79,8 @@ func (hc *HookConfig) validate() error { if hc.validated { return nil } - + hc.UserPwsh = string(hc.Shell) + hc.Shell = ShellType(strings.Split(string(hc.Shell), " ")[0]) if hc.Run == "" { return ErrRunRequired } diff --git a/cli/azd/pkg/tools/powershell/powershell.go b/cli/azd/pkg/tools/powershell/powershell.go index ca9fad66290..78bbcc98700 100644 --- a/cli/azd/pkg/tools/powershell/powershell.go +++ b/cli/azd/pkg/tools/powershell/powershell.go @@ -25,7 +25,7 @@ type powershellScript struct { // Executes the specified powershell script // When interactive is true will attach to stdin, stdout & stderr func (bs *powershellScript) Execute(ctx context.Context, path string, options tools.ExecOptions) (exec.RunResult, error) { - runArgs := exec.NewRunArgs("pwsh", path). + runArgs := exec.NewRunArgs(options.UserPwsh, path). WithCwd(bs.cwd). WithEnv(bs.envVars). WithShell(true) diff --git a/cli/azd/pkg/tools/script.go b/cli/azd/pkg/tools/script.go index f111db99400..28216b14fc1 100644 --- a/cli/azd/pkg/tools/script.go +++ b/cli/azd/pkg/tools/script.go @@ -11,6 +11,7 @@ import ( type ExecOptions struct { Interactive *bool StdOut io.Writer + UserPwsh string } // Utility to easily execute a bash script across platforms From 8a37f2b2b7ec5c10eff1cdf126f908de048cca51 Mon Sep 17 00:00:00 2001 From: "Menghua Chen (WICRESOFT NORTH AMERICA LTD)" Date: Fri, 24 Jan 2025 16:10:12 +0800 Subject: [PATCH 2/3] fix ci error fix ci error --- cli/azd/pkg/ext/hooks_runner.go | 4 ++-- cli/azd/pkg/ext/models.go | 7 ++----- cli/azd/pkg/tools/powershell/powershell_test.go | 5 +++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/cli/azd/pkg/ext/hooks_runner.go b/cli/azd/pkg/ext/hooks_runner.go index 15c7855f539..0acce4eb0ff 100644 --- a/cli/azd/pkg/ext/hooks_runner.go +++ b/cli/azd/pkg/ext/hooks_runner.go @@ -116,7 +116,7 @@ func (h *HooksRunner) GetScript(hookConfig *HookConfig) (tools.Script, error) { return nil, err } - switch hookConfig.Shell { + switch ShellType(strings.Split(string(hookConfig.Shell), " ")[0]) { case ShellTypeBash: return bash.NewBashScript(h.commandRunner, h.cwd, h.env.Environ()), nil case ShellTypePowershell: @@ -158,7 +158,7 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti options.StdOut = previewer defer h.console.StopPreviewer(ctx, false) } - options.UserPwsh = hookConfig.UserPwsh + options.UserPwsh = string(hookConfig.Shell) log.Printf("Executing script '%s'\n", hookConfig.path) res, err := script.Execute(ctx, hookConfig.path, *options) diff --git a/cli/azd/pkg/ext/models.go b/cli/azd/pkg/ext/models.go index e2a9f28e088..c3439bdafe8 100644 --- a/cli/azd/pkg/ext/models.go +++ b/cli/azd/pkg/ext/models.go @@ -70,8 +70,6 @@ type HookConfig struct { Windows *HookConfig `yaml:"windows,omitempty"` // When running on linux/macos use this override config Posix *HookConfig `yaml:"posix,omitempty"` - // In order to support pwsh parameter passing, this parameter is required to store the shell value passed in by the user - UserPwsh string } // Validates and normalizes the hook configuration @@ -79,8 +77,7 @@ func (hc *HookConfig) validate() error { if hc.validated { return nil } - hc.UserPwsh = string(hc.Shell) - hc.Shell = ShellType(strings.Split(string(hc.Shell), " ")[0]) + if hc.Run == "" { return ErrRunRequired } @@ -169,7 +166,7 @@ func createTempScript(hookConfig *HookConfig) (string, error) { scriptHeader := []string{} scriptFooter := []string{} - switch hookConfig.Shell { + switch ShellType(strings.Split(string(hookConfig.Shell), " ")[0]) { case ShellTypeBash: ext = "sh" scriptHeader = []string{ diff --git a/cli/azd/pkg/tools/powershell/powershell_test.go b/cli/azd/pkg/tools/powershell/powershell_test.go index 2a41c707913..55b3b96d5f6 100644 --- a/cli/azd/pkg/tools/powershell/powershell_test.go +++ b/cli/azd/pkg/tools/powershell/powershell_test.go @@ -23,10 +23,11 @@ func Test_Powershell_Execute(t *testing.T) { t.Run("Success", func(t *testing.T) { mockContext := mocks.NewMockContext(context.Background()) + userPwsh := "pwsh -NoProfile" mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool { return true }).RespondFn(func(args exec.RunArgs) (exec.RunResult, error) { - require.Equal(t, "pwsh", args.Cmd) + require.Equal(t, userPwsh, args.Cmd) require.Equal(t, workingDir, args.Cwd) require.Equal(t, scriptPath, args.Args[0]) require.Equal(t, env, args.Env) @@ -38,7 +39,7 @@ func Test_Powershell_Execute(t *testing.T) { runResult, err := PowershellScript.Execute( *mockContext.Context, scriptPath, - tools.ExecOptions{Interactive: to.Ptr(true)}, + tools.ExecOptions{UserPwsh: userPwsh, Interactive: to.Ptr(true)}, ) require.NotNil(t, runResult) From 213a7ef142fb05e378c60a87b5d42d2d3f28c0b8 Mon Sep 17 00:00:00 2001 From: "Menghua Chen (WICRESOFT NORTH AMERICA LTD)" Date: Fri, 24 Jan 2025 16:23:05 +0800 Subject: [PATCH 3/3] fix ci error --- cli/azd/pkg/tools/powershell/powershell_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/azd/pkg/tools/powershell/powershell_test.go b/cli/azd/pkg/tools/powershell/powershell_test.go index 55b3b96d5f6..ec8c9fd1633 100644 --- a/cli/azd/pkg/tools/powershell/powershell_test.go +++ b/cli/azd/pkg/tools/powershell/powershell_test.go @@ -23,6 +23,7 @@ func Test_Powershell_Execute(t *testing.T) { t.Run("Success", func(t *testing.T) { mockContext := mocks.NewMockContext(context.Background()) + // #nosec G101 userPwsh := "pwsh -NoProfile" mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool { return true