Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/cmd/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func (cmd *AnswerCmd) Run(ctx context.Context) error {
return outfmt.WriteJSON(os.Stdout, result)
}

if outfmt.IsPlain(ctx) {
headers := []string{"ANSWER"}
rows := [][]string{{result.Answer}}

return outfmt.WritePlain(os.Stdout, headers, rows)
}

fmt.Fprintf(os.Stdout, "Answer:\n%s\n", result.Answer)

if len(result.Citations) > 0 {
Expand Down
29 changes: 24 additions & 5 deletions internal/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (cmd *AuthSetKeyCmd) Run(ctx context.Context) error {
})
}

if outfmt.IsPlain(ctx) {
return outfmt.WritePlain(os.Stdout, []string{"STATUS", "MESSAGE"}, [][]string{{"success", "API key stored in keyring"}})
}

fmt.Fprintln(os.Stderr, "API key stored in keyring")

return nil
Expand Down Expand Up @@ -115,20 +119,31 @@ func (cmd *AuthStatusCmd) Run(ctx context.Context) error {
return outfmt.WriteJSON(os.Stdout, status)
}

if outfmt.IsPlain(ctx) {
headers := []string{"HAS_KEY", "ENV_OVERRIDE", "STORAGE"}
rows := [][]string{{
fmt.Sprintf("%t", hasKey),
fmt.Sprintf("%t", envOverride),
"keyring",
}}

return outfmt.WritePlain(os.Stdout, headers, rows)
}

// Human-readable output
fmt.Fprintf(os.Stderr, "Storage: %s\n", status["storage_backend"])
fmt.Fprintf(os.Stdout, "Storage: %s\n", status["storage_backend"])

switch {
case envOverride:
fmt.Fprintln(os.Stderr, "Status: Using EXA_API_KEY environment variable")
fmt.Fprintln(os.Stdout, "Status: Using EXA_API_KEY environment variable")
case hasKey:
fmt.Fprintln(os.Stderr, "Status: Authenticated")
fmt.Fprintln(os.Stdout, "Status: Authenticated")

if redacted, ok := status["key_redacted"].(string); ok {
fmt.Fprintf(os.Stderr, "Key: %s\n", redacted)
fmt.Fprintf(os.Stdout, "Key: %s\n", redacted)
}
default:
fmt.Fprintln(os.Stderr, "Status: Not authenticated")
fmt.Fprintln(os.Stdout, "Status: Not authenticated")
fmt.Fprintln(os.Stderr, "Run: exa-cli auth set-key --stdin")
}

Expand All @@ -154,6 +169,10 @@ func (cmd *AuthRemoveCmd) Run(ctx context.Context) error {
})
}

if outfmt.IsPlain(ctx) {
return outfmt.WritePlain(os.Stdout, []string{"STATUS", "MESSAGE"}, [][]string{{"success", "API key removed"}})
}

fmt.Fprintln(os.Stderr, "API key removed")

return nil
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ func (cmd *ContentsCmd) Run(ctx context.Context) error {
return outfmt.WriteJSON(os.Stdout, result)
}

if outfmt.IsPlain(ctx) {
headers := []string{"TITLE", "URL", "SCORE", "DATE", "SUMMARY"}

var rows [][]string
for _, r := range result.Results {
rows = append(rows, []string{r.Title, r.URL, fmt.Sprintf("%.2f", r.Score), r.PublishedDate, r.Summary})
}

return outfmt.WritePlain(os.Stdout, headers, rows)
}

if len(result.Results) == 0 {
fmt.Fprintln(os.Stderr, "No results found")
return nil
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/find_similar.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func (cmd *FindSimilarCmd) Run(ctx context.Context) error {
return outfmt.WriteJSON(os.Stdout, result)
}

if outfmt.IsPlain(ctx) {
headers := []string{"TITLE", "URL", "SCORE", "DATE", "AUTHOR"}

var rows [][]string
for _, r := range result.Results {
rows = append(rows, []string{r.Title, r.URL, fmt.Sprintf("%.2f", r.Score), r.PublishedDate, r.Author})
}

return outfmt.WritePlain(os.Stdout, headers, rows)
}

if len(result.Results) == 0 {
fmt.Fprintln(os.Stderr, "No similar pages found")
return nil
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func (cmd *SearchCmd) Run(ctx context.Context) error {
return outfmt.WriteJSON(os.Stdout, result)
}

if outfmt.IsPlain(ctx) {
headers := []string{"TITLE", "URL", "SCORE", "DATE", "AUTHOR"}

var rows [][]string
for _, r := range result.Results {
rows = append(rows, []string{r.Title, r.URL, fmt.Sprintf("%.2f", r.Score), r.PublishedDate, r.Author})
}

return outfmt.WritePlain(os.Stdout, headers, rows)
}

if len(result.Results) == 0 {
fmt.Fprintln(os.Stderr, "No results found")
return nil
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (cmd *VersionCmd) Run(ctx context.Context) error {
"os": runtime.GOOS + "/" + runtime.GOARCH,
})
}

if outfmt.IsPlain(ctx) {
headers := []string{"VERSION", "COMMIT", "DATE", "OS"}
rows := [][]string{{VersionString(), commit, date, runtime.GOOS + "/" + runtime.GOARCH}}

return outfmt.WritePlain(os.Stdout, headers, rows)
}

fmt.Printf("exa-cli %s\n", VersionString())
fmt.Printf(" Commit: %s\n", commit)
fmt.Printf(" Built: %s\n", date)
Expand Down
24 changes: 24 additions & 0 deletions internal/outfmt/outfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ func WriteJSON(w io.Writer, v any) error {
return nil
}

// WritePlain writes tab-separated values to the writer.
func WritePlain(w io.Writer, headers []string, rows [][]string) error {
replacer := strings.NewReplacer("\t", " ", "\n", " ", "\r", "")

if len(headers) > 0 {
if _, err := fmt.Fprintln(w, strings.Join(headers, "\t")); err != nil {
return fmt.Errorf("write plain headers: %w", err)
}
}

for _, row := range rows {
cleaned := make([]string, len(row))
for i, cell := range row {
cleaned[i] = replacer.Replace(cell)
}

if _, err := fmt.Fprintln(w, strings.Join(cleaned, "\t")); err != nil {
return fmt.Errorf("write plain row: %w", err)
}
}

return nil
Comment on lines 68 to 88
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TSV output breaks on embedded tabs/newlines

WritePlain joins cell values with \t and writes rows with Fprintln, but does not sanitize the values. API responses for fields like Answer and Summary can contain tab and newline characters, which will corrupt the TSV structure — a newline inside a cell will be interpreted as a row boundary, and a tab will be interpreted as a column boundary.

Consider replacing or stripping \t and \n from cell values before joining. For example:

// WritePlain writes tab-separated values to the writer.
func WritePlain(w io.Writer, headers []string, rows [][]string) error {
	if len(headers) > 0 {
		fmt.Fprintln(w, strings.Join(headers, "\t"))
	}

	replacer := strings.NewReplacer("\t", " ", "\n", " ", "\r", "")
	for _, row := range rows {
		cleaned := make([]string, len(row))
		for i, cell := range row {
			cleaned[i] = replacer.Replace(cell)
		}
		fmt.Fprintln(w, strings.Join(cleaned, "\t"))
	}

	return nil
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/outfmt/outfmt.go
Line: 68:77

Comment:
**TSV output breaks on embedded tabs/newlines**

`WritePlain` joins cell values with `\t` and writes rows with `Fprintln`, but does not sanitize the values. API responses for fields like `Answer` and `Summary` can contain tab and newline characters, which will corrupt the TSV structure — a newline inside a cell will be interpreted as a row boundary, and a tab will be interpreted as a column boundary.

Consider replacing or stripping `\t` and `\n` from cell values before joining. For example:

```go
// WritePlain writes tab-separated values to the writer.
func WritePlain(w io.Writer, headers []string, rows [][]string) error {
	if len(headers) > 0 {
		fmt.Fprintln(w, strings.Join(headers, "\t"))
	}

	replacer := strings.NewReplacer("\t", " ", "\n", " ", "\r", "")
	for _, row := range rows {
		cleaned := make([]string, len(row))
		for i, cell := range row {
			cleaned[i] = replacer.Replace(cell)
		}
		fmt.Fprintln(w, strings.Join(cleaned, "\t"))
	}

	return nil
}
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — WritePlain now sanitizes tabs/newlines with strings.NewReplacer.

Comment on lines 68 to 88
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write errors silently discarded

WritePlain returns error but never returns a non-nil one — fmt.Fprintln write errors are silently ignored. This is minor since stdout write failures are typically unrecoverable, but for consistency with WriteJSON (which wraps encode errors), consider checking at least one write:

Suggested change
func WritePlain(w io.Writer, headers []string, rows [][]string) error {
if len(headers) > 0 {
fmt.Fprintln(w, strings.Join(headers, "\t"))
}
for _, row := range rows {
fmt.Fprintln(w, strings.Join(row, "\t"))
}
return nil
// WritePlain writes tab-separated values to the writer.
func WritePlain(w io.Writer, headers []string, rows [][]string) error {
if len(headers) > 0 {
if _, err := fmt.Fprintln(w, strings.Join(headers, "\t")); err != nil {
return fmt.Errorf("write plain headers: %w", err)
}
}
for _, row := range rows {
if _, err := fmt.Fprintln(w, strings.Join(row, "\t")); err != nil {
return fmt.Errorf("write plain row: %w", err)
}
}
return nil
}

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/outfmt/outfmt.go
Line: 68:77

Comment:
**Write errors silently discarded**

`WritePlain` returns `error` but never returns a non-nil one — `fmt.Fprintln` write errors are silently ignored. This is minor since stdout write failures are typically unrecoverable, but for consistency with `WriteJSON` (which wraps encode errors), consider checking at least one write:

```suggestion
// WritePlain writes tab-separated values to the writer.
func WritePlain(w io.Writer, headers []string, rows [][]string) error {
	if len(headers) > 0 {
		if _, err := fmt.Fprintln(w, strings.Join(headers, "\t")); err != nil {
			return fmt.Errorf("write plain headers: %w", err)
		}
	}

	for _, row := range rows {
		if _, err := fmt.Fprintln(w, strings.Join(row, "\t")); err != nil {
			return fmt.Errorf("write plain row: %w", err)
		}
	}

	return nil
}
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — WritePlain now propagates fmt.Fprintln errors.

}

func KeyValuePayload(key string, value any) map[string]any {
return map[string]any{
"key": key,
Expand Down