Cosmos Nicolaou | 90610bd | 2014-12-02 22:31:04 -0800 | [diff] [blame] | 1 | package modules_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | |
| 8 | "veyron.io/veyron/veyron/lib/modules" |
| 9 | ) |
| 10 | |
| 11 | func init() { |
| 12 | modules.RegisterChild("echo", "<args>...", echo) |
| 13 | } |
| 14 | |
| 15 | func echo(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 16 | for i, a := range args { |
| 17 | fmt.Fprintf(stdout, "%d: %s\n", i, a) |
| 18 | } |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | func ExampleDispatch() { |
| 23 | if modules.IsModulesProcess() { |
| 24 | // Child process. Dispatch will invoke the 'echo' command |
| 25 | if err := modules.Dispatch(); err != nil { |
| 26 | panic(fmt.Sprintf("unexpected error: %s", err)) |
| 27 | } |
| 28 | return |
| 29 | } |
| 30 | // Parent process. |
| 31 | sh, _ := modules.NewShell(nil) |
| 32 | defer sh.Cleanup(nil, nil) |
| 33 | h, _ := sh.Start("echo", nil, "a", "b") |
| 34 | h.Shutdown(os.Stdout, os.Stderr) |
| 35 | // Output: |
| 36 | // 0: echo |
| 37 | // 1: a |
| 38 | // 2: b |
| 39 | } |
| 40 | |
| 41 | func ExampleDispatchAndExit() { |
| 42 | // DispatchAndExit will call os.Exit(0) when executed within the child. |
| 43 | modules.DispatchAndExit() |
| 44 | sh, _ := modules.NewShell(nil) |
| 45 | defer sh.Cleanup(nil, nil) |
| 46 | h, _ := sh.Start("echo", nil, "c", "d") |
| 47 | h.Shutdown(os.Stdout, os.Stderr) |
| 48 | // Output: |
| 49 | // 0: echo |
| 50 | // 1: c |
| 51 | // 2: d |
| 52 | } |