Skip to content

Commit 31db883

Browse files
authored
Merge pull request #11 from copilot-extensions/update-stream-logic
update stream cmd logic and error handling
2 parents 62c0c3c + 81242a4 commit 31db883

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

cmd/stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func agentStream(cmd *cobra.Command, args []string) {
3030

3131
file := args[0]
3232

33-
err := stream.ParseFile(file)
33+
result, err := stream.ParseFile(file)
3434
if err != nil {
3535
fmt.Fprintf(os.Stderr, "Error parsing file: %v\n", err)
3636
os.Exit(1)
3737
}
38+
fmt.Println(result)
3839
}

pkg/stream/parse.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ type Data struct {
1818
Choices []Choice `json:"choices"`
1919
}
2020

21-
func ParseFile(filename string) error {
21+
func ParseFile(filename string) (string, error) {
2222
// Open the file
2323
file, err := os.Open(filename)
2424
if err != nil {
25-
return fmt.Errorf("could not open file: %w", err)
25+
return "", fmt.Errorf("could not open file: %w", err)
2626
}
2727
defer file.Close()
2828

@@ -52,8 +52,7 @@ func ParseFile(filename string) error {
5252
var data Data
5353
err := json.Unmarshal([]byte(line), &data)
5454
if err != nil {
55-
// Skip this line if JSON is incomplete or malformed
56-
continue
55+
return "", fmt.Errorf("error parsing JSON: %w", err)
5756
}
5857

5958
// Extract delta.content and concatenate it
@@ -64,12 +63,10 @@ func ParseFile(filename string) error {
6463

6564
// Check for scanner errors
6665
if err := scanner.Err(); err != nil {
67-
return fmt.Errorf("error reading file: %w", err)
66+
return "", fmt.Errorf("error reading file: %w", err)
6867
}
6968

7069
// Print the final concatenated result
7170
result := contentBuilder.String()
72-
fmt.Println(result)
73-
74-
return nil
71+
return result, nil
7572
}

0 commit comments

Comments
 (0)