Skip to content

Commit

Permalink
Support Custom Command Format for pwsh in the shell Field (#4595)
Browse files Browse the repository at this point in the history
* Update strategy

* fix ci error

fix ci error

* fix ci error

---------

Co-authored-by: Victor Vazquez <[email protected]>
Co-authored-by: Menghua Chen (WICRESOFT NORTH AMERICA LTD) <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2025
1 parent 3e57d4d commit 835fc6e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cli/azd/pkg/ext/hooks_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (h *HooksRunner) GetScript(hookConfig *HookConfig, envVars []string) (tools
return nil, err
}

switch hookConfig.Shell {
switch ShellType(strings.Split(string(hookConfig.Shell), " ")[0]) {
case ShellTypeBash:
return bash.NewBashScript(h.commandRunner, h.cwd, envVars), nil
case ShellTypePowershell:
Expand Down Expand Up @@ -190,6 +190,7 @@ func (h *HooksRunner) execHook(ctx context.Context, hookConfig *HookConfig, opti
options.StdOut = previewer
defer h.console.StopPreviewer(ctx, false)
}
options.UserPwsh = string(hookConfig.Shell)

log.Printf("Executing script '%s'\n", hookConfig.path)
res, err := script.Execute(ctx, hookConfig.path, *options)
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/ext/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,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{
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/tools/powershell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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)
Expand Down
6 changes: 4 additions & 2 deletions cli/azd/pkg/tools/powershell/powershell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ 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
}).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)
Expand All @@ -41,7 +43,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)
Expand Down
1 change: 1 addition & 0 deletions cli/azd/pkg/tools/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type ExecOptions struct {
Interactive *bool
StdOut io.Writer
UserPwsh string
}

// Utility to easily execute a bash script across platforms
Expand Down

0 comments on commit 835fc6e

Please sign in to comment.