Skip to content

Conversation

@hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Jan 28, 2026

πŸ“‘ Description

Support run string command directly: facades.Process().Run("ls -a") instead of facades.Process().Run("/bin/sh", "-c", "ls -a").

βœ… Checks

  • Added test cases for my code

}

func (r *Process) Run(name string, args ...string) contractsprocess.Result {
name, args = formatCommand(name, args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we make this explicit by adding a RunShell method. The auto-detection is ambiguous. For instance, on Windows, running a binary inside c:\Program Files\App.exe would fail because the code treats the space as a shell separator.

It also creates inconsistent behavior. For instance, facades.Process().Run("ls | grep foo", "-la") will fail. Because a second argument is present, the logic skips the shell detection and tries to find an executable literally named 'ls | grep foo'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered adding a new function like RunShell, but I'm thinking if it's necessary to add a single function to support running string commands. I just want to omit /bin/sh -c, it's a bit heavy to add such a function.

c:\Program Files\App.exe is a good case, space may be contained in the route.

facades.Process().Run("ls | grep foo", "-la") doesn't trigger the formatCommand, so it's not related to this PR.

We can optimize the condition to if len(args) == 0 && str.Of(name).Contains("&", "|"), except space. Then it's simpler to run such commands:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you are concerned about the verbosity of manually writing /bin/sh -c but the default Run method should remain raw and strict to avoid 'magic' inside it.

Go's standard library actually warns against this implicit shell invocation:

Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells. The package behaves more like C's "exec" family of functions. To expand glob patterns, either call the shell directly, taking care to escape any dangerous input, or use the path/filepath package's Glob function. To expand environment variables, use package os's ExpandEnv.

https://pkg.go.dev/os/exec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants