| 1 |
// Copyright (c) 2018, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package interp_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"mvdan.cc/sh/v3/expand" |
| 14 |
"mvdan.cc/sh/v3/interp" |
| 15 |
"mvdan.cc/sh/v3/syntax" |
| 16 |
) |
| 17 |
|
| 18 |
func Example() { |
| 19 |
src := ` |
| 20 |
foo=abc |
| 21 |
for i in 1 2 3; do |
| 22 |
foo+=$i |
| 23 |
done |
| 24 |
let bar=(2 + 3) |
| 25 |
echo $foo $bar |
| 26 |
echo $GLOBAL |
| 27 |
` |
| 28 |
file, _ := syntax.NewParser().Parse(strings.NewReader(src), "") |
| 29 |
runner, _ := interp.New( |
| 30 |
// Use [interp.Interactive] to enable interactive shell defaults like expanding aliases. |
| 31 |
interp.Env(expand.ListEnviron("GLOBAL=global_value")), |
| 32 |
interp.StdIO(nil, os.Stdout, os.Stdout), |
| 33 |
) |
| 34 |
runner.Run(context.TODO(), file) |
| 35 |
// Output: |
| 36 |
// abc123 5 |
| 37 |
// global_value |
| 38 |
} |
| 39 |
|
| 40 |
func ExampleExecHandlers() { |
| 41 |
src := "echo foo; join ! foo bar baz; missing-program bar" |
| 42 |
file, _ := syntax.NewParser().Parse(strings.NewReader(src), "") |
| 43 |
|
| 44 |
execJoin := func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc { |
| 45 |
return func(ctx context.Context, args []string) error { |
| 46 |
hc := interp.HandlerCtx(ctx) |
| 47 |
if args[0] == "join" { |
| 48 |
fmt.Fprintln(hc.Stdout, strings.Join(args[2:], args[1])) |
| 49 |
return nil |
| 50 |
} |
| 51 |
return next(ctx, args) |
| 52 |
} |
| 53 |
} |
| 54 |
execNotInstalled := func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc { |
| 55 |
return func(ctx context.Context, args []string) error { |
| 56 |
hc := interp.HandlerCtx(ctx) |
| 57 |
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil { |
| 58 |
fmt.Printf("%s is not installed\n", args[0]) |
| 59 |
return interp.ExitStatus(1) |
| 60 |
} |
| 61 |
return next(ctx, args) |
| 62 |
} |
| 63 |
} |
| 64 |
runner, _ := interp.New( |
| 65 |
interp.StdIO(nil, os.Stdout, os.Stdout), |
| 66 |
interp.ExecHandlers(execJoin, execNotInstalled), |
| 67 |
) |
| 68 |
runner.Run(context.TODO(), file) |
| 69 |
// Output: |
| 70 |
// foo |
| 71 |
// foo!bar!baz |
| 72 |
// missing-program is not installed |
| 73 |
} |
| 74 |
|
| 75 |
type nopWriterCloser struct { |
| 76 |
*strings.Reader |
| 77 |
} |
| 78 |
|
| 79 |
func (nopWriterCloser) Write([]byte) (int, error) { return 0, io.EOF } |
| 80 |
func (nopWriterCloser) Close() error { return nil } |
| 81 |
|
| 82 |
func ExampleOpenHandler() { |
| 83 |
src := "echo $(</etc/hostname); echo bar >/dev/null" |
| 84 |
file, _ := syntax.NewParser().Parse(strings.NewReader(src), "") |
| 85 |
|
| 86 |
open := func(ctx context.Context, path string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) { |
| 87 |
// Hard-code the contents of the hostname file for all platforms. |
| 88 |
if path == "/etc/hostname" { |
| 89 |
return nopWriterCloser{strings.NewReader("mymachine")}, nil |
| 90 |
} |
| 91 |
// DefaultOpenHandler already redirects /dev/null to NUL on Windows. |
| 92 |
return interp.DefaultOpenHandler()(ctx, path, flag, perm) |
| 93 |
} |
| 94 |
runner, _ := interp.New( |
| 95 |
interp.StdIO(nil, os.Stdout, os.Stdout), |
| 96 |
interp.OpenHandler(open), |
| 97 |
) |
| 98 |
runner.Run(context.TODO(), file) |
| 99 |
// Output: |
| 100 |
// mymachine |
| 101 |
} |