A powerful Go library for flexible output formatting in command-line applications. OutFormat provides multiple output formats with intelligent data parsing, syntax highlighting, and table generation capabilities.
- Multiple Output Formats: JSON, YAML, TOML, CSV, and formatted tables
- Intelligent Data Parsing: Automatically detects and formats structured data
- Syntax Highlighting: Color-coded output with customizable highlighting rules
- Table Generation: Beautiful table output with row/column orientation options
- Error Handling: Graceful fallback to JSON when format conversion fails
- Machine-Readable Output: Support for machine parsing with structured formats
go get binrc.com/pkg/outformatpackage main
import (
"binrc.com/pkg/outformat"
)
func main() {
data := map[string]interface{}{
"id": 1,
"name": "example",
"status": "active",
}
// Output as formatted JSON
outformat.SuccessOutput(data, "", "json")
// Output as table
outformat.SuccessOutput(data, "", "table")
// Output as YAML
outformat.SuccessOutput(data, "", "yaml")
}json: Pretty-printed JSON with indentationjson-line: Compact JSON on single line
yaml: YAML format outputtoml: TOML format outputcsv: CSV format (for tabular data)
table: Default table formattable.row: Row-oriented table layouttable.col: Column-oriented table layout
Main function for outputting successful results:
func SuccessOutput(result interface{}, override string, outputFormat string, failflag ...string)Parameters:
result: Data to output (any type)override: Override message (optional)outputFormat: Desired output formatfailflag: Error flag for error styling
Output success message with standard structure:
func SuccessOutputl(message string, override string, outputFormat string)Output error message with standard structure:
func ErrorOutputl(errResult error, override string, outputFormat string)// Simple map output
data := map[string]string{
"status": "success",
"message": "Operation completed",
}
outformat.SuccessOutput(data, "", "json")// Array of objects for table output
users := []map[string]interface{}{
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"},
{"id": 3, "name": "Charlie", "role": "user"},
}
outformat.SuccessOutput(users, "", "table")if err != nil {
outformat.ErrorOutputl(err, "", "json")
return
}// Check if machine-readable output is requested
if outformat.HasMachineOutputFlag() {
outformat.SuccessOutput(data, "", "json")
} else {
outformat.SuccessOutput(data, "", "table")
}The library automatically applies color coding to common patterns:
- Timestamps: Light blue (
2006-01-02 15:04:05) - IP Addresses: Light blue (IPv4 and IPv6)
- MAC Addresses: Light blue
- Status Keywords:
- Success: Green (
OK,succeeded,SUCCESS) - Warning: Light yellow (
WAR,DBG) - Error: Light red (
ERR,failed,FAILED,ERROR)
- Success: Green (
- Network Terms: Light blue (
cloud,local)
The library automatically detects and handles:
- Objects: Key-value pairs displayed as tables
- Arrays: Multiple objects displayed as rows
- Nested Structures: Recursively processed for table generation
- Mixed Types: Automatic conversion to string representation
When using table formats, the library:
- Extracts keys from the first object as column headers
- Sorts columns with priority (ID first, message last)
- Generates rows from data values
- Applies appropriate styling and colors
- Row-oriented: Each object becomes a row
- Column-oriented: Each object becomes a column (useful for comparing few items)
The library implements robust error handling:
- Format Conversion Failures: Falls back to JSON output
- Invalid Data: Gracefully handles malformed input
- Exit Codes: Proper exit codes for success (0) and errors (2)
- Logging: Comprehensive logging for debugging
github.com/TylerBrock/colorjson: JSON colorizationgithub.com/bitly/go-simplejson: JSON manipulationgithub.com/fatih/color: Terminal colorsgithub.com/pelletier/go-toml/v2: TOML supportgithub.com/pterm/pterm: Table renderinggithub.com/rs/zerolog/log: Logginggopkg.in/yaml.v3: YAML support
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
This project is licensed under the MIT License - see the LICENSE file for details.

