Commit e38fe06d authored by Kirill Unitsaev's avatar Kirill Unitsaev

config, ui: add JSON output format support

- Add config/format.go with FormatFlag and IsJSON helper - Add ui/json.go with JSONItem struct and PrintJSON utility
parent 661d0a49
package config
import "github.com/urfave/cli/v3"
const (
FormatText = "text"
FormatJSON = "json"
)
var FormatFlag = &cli.StringFlag{
Name: "format",
Usage: "output format (text, json)",
Value: FormatText,
Aliases: []string{"o"},
}
func IsJSON(cmd *cli.Command) bool {
return cmd.String("format") == FormatJSON
}
package ui
import (
"encoding/json"
"fmt"
)
type JSONItem struct {
Name string `json:"name"`
Status string `json:"status,omitempty"`
Description string `json:"description,omitempty"`
Value string `json:"value,omitempty"`
}
func TreeItemsToJSON(items []TreeItem) []JSONItem {
result := make([]JSONItem, len(items))
for i, item := range items {
result[i] = JSONItem{
Name: item.Name,
Status: item.Status.Label,
Description: item.Description,
}
}
return result
}
func PrintJSON(data interface{}) error {
output, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment