summaryrefslogtreecommitdiffhomepage
path: root/internal/format/format.go
diff options
context:
space:
mode:
authorJay V <[email protected]>2025-05-21 15:01:25 -0400
committerJay V <[email protected]>2025-05-21 15:01:25 -0400
commit9049295cc961b250be6144585dde322e778534d7 (patch)
treec8a2f09ed6cea54eb9587243eb7dbe298fef1b20 /internal/format/format.go
parent4526b14b17dc49f3ef4f3b1a1d02eff5c6b6b59f (diff)
parentdff8e77eb6d1709fa1ddeb52d0d9c19afd13d385 (diff)
downloadopencode-9049295cc961b250be6144585dde322e778534d7.tar.gz
opencode-9049295cc961b250be6144585dde322e778534d7.zip
Merge branch 'dev' into docs
Diffstat (limited to 'internal/format/format.go')
-rw-r--r--internal/format/format.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/internal/format/format.go b/internal/format/format.go
new file mode 100644
index 000000000..321f5c102
--- /dev/null
+++ b/internal/format/format.go
@@ -0,0 +1,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)
+ }
+}