Skip to content

Commit

Permalink
update stream cmd logic and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmvrk committed Nov 12, 2024
1 parent 62c0c3c commit 81242a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ func agentStream(cmd *cobra.Command, args []string) {

file := args[0]

err := stream.ParseFile(file)
result, err := stream.ParseFile(file)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing file: %v\n", err)
os.Exit(1)
}
fmt.Println(result)
}
13 changes: 5 additions & 8 deletions pkg/stream/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type Data struct {
Choices []Choice `json:"choices"`
}

func ParseFile(filename string) error {
func ParseFile(filename string) (string, error) {
// Open the file
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("could not open file: %w", err)
return "", fmt.Errorf("could not open file: %w", err)
}
defer file.Close()

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

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

// Check for scanner errors
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file: %w", err)
return "", fmt.Errorf("error reading file: %w", err)
}

// Print the final concatenated result
result := contentBuilder.String()
fmt.Println(result)

return nil
return result, nil
}

0 comments on commit 81242a4

Please sign in to comment.