summaryrefslogtreecommitdiffhomepage
path: root/internal/format/format.go
blob: 321f5c10266245e05104d16dd3aef920eadc9f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package format

import (
	"encoding/json"
	"fmt"
)

// OutputFormat represents the format for non-interactive mode output
type OutputFormat string

const (
	// TextFormat is plain text output (default)
	TextFormat OutputFormat = "text"

	// JSONFormat is output wrapped in a JSON object
	JSONFormat OutputFormat = "json"
)

// IsValid checks if the output format is valid
func (f OutputFormat) IsValid() bool {
	return f == TextFormat || f == JSONFormat
}

// String returns the string representation of the output format
func (f OutputFormat) String() string {
	return string(f)
}

// FormatOutput formats the given content according to the specified format
func FormatOutput(content string, format OutputFormat) (string, error) {
	switch format {
	case TextFormat:
		return content, nil
	case JSONFormat:
		jsonData := map[string]string{
			"response": content,
		}
		jsonBytes, err := json.MarshalIndent(jsonData, "", "  ")
		if err != nil {
			return "", fmt.Errorf("failed to marshal JSON: %w", err)
		}
		return string(jsonBytes), nil
	default:
		return "", fmt.Errorf("unsupported output format: %s", format)
	}
}