Skip to content

Commit f844d44

Browse files
Dhanalakshmi DDhanalakshmi-D04
authored andcommitted
Add Shell method for running commands via the system shell
Adds a new Shell method (and package-level Shell function) that runs a command line through the native OS shell (sh -c on Unix, cmd /C on Windows), rather than parsing arguments manually like Exec does. This allows correct variable expansion when used with WithEnv, fixing the issue described in #239, while leaving Exec unchanged for backward compatibility.
1 parent cf62146 commit f844d44

6 files changed

Lines changed: 216 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ These are functions that create a pipe with a given contents:
313313
| [`IfExists`](https://pkg.go.dev/github.com/bitfield/script#IfExists) | do something only if some file exists |
314314
| [`ListFiles`](https://pkg.go.dev/github.com/bitfield/script#ListFiles) | file listing (including wildcards) |
315315
| [`Post`](https://pkg.go.dev/github.com/bitfield/script#Post) | HTTP response |
316+
| [`Shell`](https://pkg.go.dev/github.com/bitfield/script#Shell) | command output, run via the system shell |
316317
| [`Slice`](https://pkg.go.dev/github.com/bitfield/script#Slice) | slice elements, one per line |
317318
| [`Stdin`](https://pkg.go.dev/github.com/bitfield/script#Stdin) | standard input |
318319

@@ -362,6 +363,7 @@ Filters are methods on an existing pipe that also return a pipe, allowing you to
362363
| [`RejectRegexp`](https://pkg.go.dev/github.com/bitfield/script#Pipe.RejectRegexp) | lines not matching given regexp |
363364
| [`Replace`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Replace) | matching text replaced with given string |
364365
| [`ReplaceRegexp`](https://pkg.go.dev/github.com/bitfield/script#Pipe.ReplaceRegexp) | matching text replaced with given string |
366+
| [`Shell`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Shell) | filtered through the system shell |
365367
| [`Tee`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Tee) | input copied to supplied writers |
366368

367369
Note that filters run concurrently, rather than producing nothing until each stage has fully read its input. This is convenient for executing long-running commands, for example. If you do need to wait for the pipeline to complete, call [`Wait`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Wait).

‎script.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ func Post(url string) *Pipe {
184184
return NewPipe().Post(url)
185185
}
186186

187+
// Shell creates a pipe that runs cmdLine as a command via the system shell,
188+
// and produces its combined output. See [Pipe.Shell] for details.
189+
func Shell(cmdLine string) *Pipe {
190+
return NewPipe().Shell(cmdLine)
191+
}
192+
187193
// Slice creates a pipe containing each element of s, one per line. If s is
188194
// empty or nil, then the pipe is empty.
189195
func Slice(s []string) *Pipe {
@@ -888,6 +894,43 @@ func (p *Pipe) SHA256Sums() *Pipe {
888894
return p.HashSums(sha256.New())
889895
}
890896

897+
// Shell runs cmdLine as a command via the operating system's native shell
898+
// ("sh -c" on Unix-like systems, "cmd /C" on Windows), sending it the
899+
// contents of the pipe as input, and produces the command's combined
900+
// output. Unlike [Pipe.Exec], Shell performs no argument parsing of its
901+
// own; the entire command line is passed unmodified to the shell, which
902+
// handles all parsing, quoting, and variable expansion exactly as it
903+
// would on an interactive command line.
904+
//
905+
// Note that variable syntax differs by platform: Unix shells expand
906+
// variables written as $VAR, while cmd.exe on Windows expands variables
907+
// written as %VAR%.
908+
//
909+
// See [Pipe.Exec] for details on error handling and environment variables
910+
// set via [Pipe.WithEnv].
911+
func (p *Pipe) Shell(cmdLine string) *Pipe {
912+
return p.Filter(func(r io.Reader, w io.Writer) error {
913+
cmd := shellCommand(cmdLine)
914+
cmd.Stdin = r
915+
cmd.Stdout = w
916+
cmd.Stderr = w
917+
pipeStderr := p.stdErr()
918+
if pipeStderr != nil {
919+
cmd.Stderr = pipeStderr
920+
}
921+
pipeEnv := p.environment()
922+
if pipeEnv != nil {
923+
cmd.Env = pipeEnv
924+
}
925+
err := cmd.Start()
926+
if err != nil {
927+
fmt.Fprintln(cmd.Stderr, err)
928+
return err
929+
}
930+
return cmd.Wait()
931+
})
932+
}
933+
891934
// Slice returns the pipe's contents as a slice of strings, one element per
892935
// line, or an error.
893936
//

‎script_unix_test.go‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"bytes"
11+
"errors"
12+
"strings"
13+
1014
"github.com/bitfield/script"
1115
"github.com/google/go-cmp/cmp"
1216
)
@@ -222,3 +226,110 @@ func ExamplePipe_ExecForEach() {
222226
// b
223227
// c
224228
}
229+
230+
func TestShellRunsShWithEchoHelloAndGetsOutputHello(t *testing.T) {
231+
t.Parallel()
232+
p := script.Shell("echo hello")
233+
if p.Error() != nil {
234+
t.Fatal(p.Error())
235+
}
236+
want := "hello\n"
237+
got, err := p.String()
238+
if err != nil {
239+
t.Fatal(err)
240+
}
241+
if want != got {
242+
t.Error(cmp.Diff(want, got))
243+
}
244+
}
245+
246+
func TestShell_ExpandsEnvironmentVariablesSetViaWithEnv(t *testing.T) {
247+
t.Parallel()
248+
env := []string{"ENV1=test1", "ENV2=test2"}
249+
got, err := script.NewPipe().WithEnv(env).Shell("echo ENV1=$ENV1 ENV2=$ENV2").String()
250+
if err != nil {
251+
t.Fatal(err)
252+
}
253+
want := "ENV1=test1 ENV2=test2\n"
254+
if want != got {
255+
t.Error(cmp.Diff(want, got))
256+
}
257+
}
258+
259+
func TestShellExpandsHomeVariableWithoutWithEnv(t *testing.T) {
260+
t.Parallel()
261+
p := script.Shell("echo $HOME")
262+
if p.Error() != nil {
263+
t.Fatal(p.Error())
264+
}
265+
got, err := p.String()
266+
if err != nil {
267+
t.Fatal(err)
268+
}
269+
if strings.TrimSpace(got) == "" {
270+
t.Error("want non-empty $HOME expansion, got empty string")
271+
}
272+
}
273+
274+
func TestShellPipesDataToExternalCommandAndGetsExpectedOutput(t *testing.T) {
275+
t.Parallel()
276+
p := script.File("testdata/hello.txt").Shell("cat")
277+
want := "hello world"
278+
got, err := p.String()
279+
if err != nil {
280+
t.Fatal(err)
281+
}
282+
if want != got {
283+
t.Error(cmp.Diff(want, got))
284+
}
285+
}
286+
287+
func TestShellErrorsRunningCommandThatDoesNotExist(t *testing.T) {
288+
t.Parallel()
289+
p := script.Shell("doesntexist_command_xyz")
290+
p.Wait()
291+
if p.Error() == nil {
292+
t.Error("want error running non-existent command")
293+
}
294+
}
295+
296+
func TestShellSendsStderrOutputToPipeStderr(t *testing.T) {
297+
t.Parallel()
298+
buf := new(bytes.Buffer)
299+
out, err := script.NewPipe().WithStderr(buf).Shell("go").String()
300+
if err == nil {
301+
t.Fatal("want error when command returns a non-zero exit status")
302+
}
303+
if out != "" {
304+
t.Fatalf("unexpected output: %q", out)
305+
}
306+
if !strings.Contains(buf.String(), "Usage") {
307+
t.Errorf("want stderr output containing the word 'Usage', got %q", buf.String())
308+
}
309+
}
310+
311+
func TestShellOnEmptyPipeProducesNoOutputAndNoError(t *testing.T) {
312+
t.Parallel()
313+
got, err := script.NewPipe().Shell("cat").String()
314+
if err != nil {
315+
t.Fatal(err)
316+
}
317+
if got != "" {
318+
t.Errorf("want empty output, got %q", got)
319+
}
320+
}
321+
322+
func TestShellOnPipeWithExistingErrorIsNoOp(t *testing.T) {
323+
t.Parallel()
324+
fakeErr := errors.New("existing error")
325+
p := script.NewPipe().WithError(fakeErr).Shell("echo hello")
326+
if p.Error() != fakeErr {
327+
t.Errorf("want existing error %v preserved, got %v", fakeErr, p.Error())
328+
}
329+
}
330+
331+
func ExamplePipe_Shell() {
332+
script.Echo("Hello, world!").Shell("tr a-z A-Z").Stdout()
333+
// Output:
334+
// HELLO, WORLD!
335+
}

‎script_windows_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,41 @@ func ExamplePipe_Dirname() {
9191
// ./src
9292
// C:\
9393
}
94+
95+
func TestShellRunsCmdWithEchoHelloAndGetsOutputHello(t *testing.T) {
96+
t.Parallel()
97+
p := script.Shell("echo hello")
98+
if p.Error() != nil {
99+
t.Fatal(p.Error())
100+
}
101+
want := "hello\r\n"
102+
got, err := p.String()
103+
if err != nil {
104+
t.Fatal(err)
105+
}
106+
if want != got {
107+
t.Errorf("want %q, got %q", want, got)
108+
}
109+
}
110+
111+
func TestShell_ExpandsEnvironmentVariablesSetViaWithEnvOnWindows(t *testing.T) {
112+
t.Parallel()
113+
env := []string{"ENV1=test1"}
114+
got, err := script.NewPipe().WithEnv(env).Shell("echo ENV1=%ENV1%").String()
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
want := "ENV1=test1\r\n"
119+
if want != got {
120+
t.Errorf("want %q, got %q", want, got)
121+
}
122+
}
123+
124+
func TestShellErrorsRunningCommandThatDoesNotExistOnWindows(t *testing.T) {
125+
t.Parallel()
126+
p := script.Shell("doesntexist_command_xyz")
127+
p.Wait()
128+
if p.Error() == nil {
129+
t.Error("want error running non-existent command")
130+
}
131+
}

‎shell_unix.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !windows
2+
3+
package script
4+
5+
import "os/exec"
6+
7+
// shellCommand returns the command used to run cmdLine via the system
8+
// shell on Unix-like systems.
9+
func shellCommand(cmdLine string) *exec.Cmd {
10+
return exec.Command("sh", "-c", cmdLine)
11+
}

‎shell_windows.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build windows
2+
3+
package script
4+
5+
import "os/exec"
6+
7+
// shellCommand returns the command used to run cmdLine via the system
8+
// shell on Windows.
9+
func shellCommand(cmdLine string) *exec.Cmd {
10+
return exec.Command("cmd", "/C", cmdLine)
11+
}

0 commit comments

Comments
 (0)