MCPcopy Create free account
hub / github.com/diillson/chatcli / ExecuteWithStream

Method ExecuteWithStream

cli/plugins/builtin_tree.go:81–117  ·  view source on GitHub ↗

ExecuteWithStream parses args, converts to engine argv form, runs engine.handleTree. Output streams line-by-line.

(ctx context.Context, args []string, onOutput func(string))

Source from the content-addressed store, hash-verified

79// ExecuteWithStream parses args, converts to engine argv form, runs
80// engine.handleTree. Output streams line-by-line.
81func (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.
120type treeArgs struct {

Callers 1

ExecuteMethod · 0.95

Calls 10

ExecuteMethod · 0.95
FlushMethod · 0.95
NewStreamWriterFunction · 0.92
NewEngineFunction · 0.92
parseTreeArgsFunction · 0.85
buildTreeArgvFunction · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
ErrorfMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected