-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No working directory for sh.Exec #213
Comments
I think this is a valid concern and I think there is a way to address it. But the linked PR was not the right solution. I think we can come up with something better and more intuitive. |
ok, so i'll propose another PR. |
If have added the PR #246, which should solve this issue. I need to run commands with mage/sh in the working directory, so I pass the variable |
Great ! |
I currently use this function as a workaround:
|
This could cause some commands that run in parallel to fail |
I think sh.Exec should have a way to pass the directory. For example, in Coraza we run |
This PR is an attempt to solve magefile#213 Added `sh.Command` struct to mirror `exec.Cmd` and allow configuring `sh.Exec` options, instead of adding a new function that can change the working directory. A single configuration struct was chosen instead of options since the struct aggregates all configuration options together. Added current `sh.Exec` parameters to `sh.Command` as fields, and mimicked current behavior. Moved `sh.run` functionality to `sh.(*Command).run`, and updated `sh.Exec` to use `sh.Command.Exec`. Added `WorkingDir` field to change the command's working directory. Signed-off-by: Hamza El-Saawy <[email protected]>
This PR is an attempt to solve magefile#213 Added `sh.Command` struct to mirror `exec.Cmd` and allow configuring `sh.Exec` options, instead of adding a new function that can change the working directory. A single configuration struct was chosen instead of options since the struct aggregates all configuration options together. Added current `sh.Exec` parameters to `sh.Command` as fields, and mimicked current behavior. Moved `sh.run` functionality to `sh.(*Command).run`, and updated `sh.Exec` to use `sh.Command.Exec`. Added `WorkingDir` field to change the command's working directory. Signed-off-by: Hamza El-Saawy <[email protected]>
This PR is an attempt to solve magefile#213 Added `sh.Command` struct to mirror `exec.Cmd` and allow configuring `sh.Exec` options, instead of adding a new function that can change the working directory. A single configuration struct was chosen instead of options since the struct aggregates all configuration options together. Added current `sh.Exec` parameters to `sh.Command` as fields, and mimicked current behavior. Moved `sh.run` functionality to `sh.(*Command).run`, and updated `sh.Exec` to use `sh.Command.Exec`. Added `WorkingDir` field to change the command's working directory. Signed-off-by: Hamza El-Saawy <[email protected]>
This is a great workaround, I added a mutex call like this in front of the function and everything works fine except now everything is sequential in execution: var mu sync.Mutex
func runInFolder(cwd string, command string, args ...string) error {
mu.Lock()
defer mu.Unlock()
fmt.Printf("Running %q with %#v in %q\n", command, args, cwd)
wd, err := os.Getwd()
if err != nil {
return err
}
err = os.Chdir(cwd)
if err != nil {
return err
}
output, err := sh.Output(command, args...)
if err != nil {
return err
}
fmt.Println(output)
err = os.Chdir(wd)
if err != nil {
return err
}
return nil
} |
Hello,
os/exec.Command
allow a working directory, but sometimes, we need it insh.Exec
too.The easy way is to use the
exec.Command
function, but i don't like it.I propose to pass a special variable in env parameter, like this:
The text was updated successfully, but these errors were encountered: