ExecuteWithStream parses args, converts to engine argv form, runs engine.handleTree. Output streams line-by-line.
(ctx context.Context, args []string, onOutput func(string))
| 79 | // ExecuteWithStream parses args, converts to engine argv form, runs |
| 80 | // engine.handleTree. Output streams line-by-line. |
| 81 | func (p *BuiltinTreePlugin) ExecuteWithStream(ctx context.Context, args []string, onOutput func(string)) (string, error) { |
| 82 | parsed, err := parseTreeArgs(args) |
| 83 | if err != nil { |
| 84 | return "", err |
| 85 | } |
| 86 | |
| 87 | argv := buildTreeArgv(parsed) |
| 88 | |
| 89 | var fullOutput strings.Builder |
| 90 | var mu sync.Mutex |
| 91 | emit := func(line string, isErr bool) { |
| 92 | mu.Lock() |
| 93 | defer mu.Unlock() |
| 94 | if onOutput != nil { |
| 95 | prefix := "" |
| 96 | if isErr { |
| 97 | prefix = "ERR: " |
| 98 | } |
| 99 | onOutput(prefix + line) |
| 100 | } |
| 101 | fullOutput.WriteString(line) |
| 102 | fullOutput.WriteString("\n") |
| 103 | } |
| 104 | outWriter := engine.NewStreamWriter(func(line string) { emit(line, false) }) |
| 105 | errWriter := engine.NewStreamWriter(func(line string) { emit(line, true) }) |
| 106 | |
| 107 | eng := engine.NewEngine(outWriter, errWriter, "") |
| 108 | execErr := eng.Execute(ctx, "tree", argv) |
| 109 | |
| 110 | outWriter.Flush() |
| 111 | errWriter.Flush() |
| 112 | |
| 113 | if execErr != nil { |
| 114 | return fullOutput.String(), fmt.Errorf("@tree failed: %w", execErr) |
| 115 | } |
| 116 | return fullOutput.String(), nil |
| 117 | } |
| 118 | |
| 119 | // treeArgs is the typed view of @tree's JSON input. |
| 120 | type treeArgs struct { |
no test coverage detected