Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
51 lines
1.8 KiB
Go
51 lines
1.8 KiB
Go
package framework
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
cliservice "github.com/tutti-os/tutti/services/tuttid/service/cli"
|
|
)
|
|
|
|
func FormatOutput(spec OutputSpec, mode cliservice.OutputMode, result any) (cliservice.CommandOutput, error) {
|
|
if mode == "" {
|
|
mode = spec.DefaultMode
|
|
}
|
|
switch mode {
|
|
case cliservice.OutputModeJSON:
|
|
formatter := jsonFormatter(spec)
|
|
if formatter == nil {
|
|
return cliservice.CommandOutput{}, fmt.Errorf("%w: json output is not supported", cliservice.ErrInvalidCommand)
|
|
}
|
|
return cliservice.CommandOutput{Kind: cliservice.OutputModeJSON, Value: formatter(result)}, nil
|
|
case cliservice.OutputModeTable:
|
|
if spec.Table == nil || spec.Table.Rows == nil {
|
|
return cliservice.CommandOutput{}, fmt.Errorf("%w: table output is not supported", cliservice.ErrInvalidCommand)
|
|
}
|
|
return cliservice.CommandOutput{
|
|
Kind: cliservice.OutputModeTable,
|
|
Columns: spec.Table.Columns,
|
|
Rows: spec.Table.Rows(result),
|
|
}, nil
|
|
case cliservice.OutputModePlain:
|
|
if spec.PlainText == nil {
|
|
return cliservice.CommandOutput{}, fmt.Errorf("%w: plain output is not supported", cliservice.ErrInvalidCommand)
|
|
}
|
|
return cliservice.CommandOutput{Kind: cliservice.OutputModePlain, Text: spec.PlainText(result)}, nil
|
|
case cliservice.OutputModeMarkdown:
|
|
if spec.Markdown == nil {
|
|
return cliservice.CommandOutput{}, fmt.Errorf("%w: markdown output is not supported", cliservice.ErrInvalidCommand)
|
|
}
|
|
return cliservice.CommandOutput{Kind: cliservice.OutputModeMarkdown, Text: spec.Markdown(result)}, nil
|
|
default:
|
|
return cliservice.CommandOutput{}, fmt.Errorf("%w: unsupported output mode %q", cliservice.ErrInvalidCommand, strings.TrimSpace(string(mode)))
|
|
}
|
|
}
|
|
|
|
func jsonFormatter(spec OutputSpec) func(any) map[string]any {
|
|
if spec.DefaultView != "" {
|
|
return spec.JSONViews[spec.DefaultView]
|
|
}
|
|
return spec.JSONValue
|
|
}
|