From 2e16c6732137d7be443326a04d15b26aa54a3713 Mon Sep 17 00:00:00 2001 From: bdoerfchen Date: Sun, 20 Jul 2025 15:28:10 +0200 Subject: [PATCH] feat: yaml output formatter --- .vscode/launch.json | 2 +- core/output/formatter.go | 2 +- core/output/yaml.go | 19 +++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 core/output/yaml.go diff --git a/.vscode/launch.json b/.vscode/launch.json index bf3097e..a49da6a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "mode": "debug", "program": "${workspaceFolder}/main.go", "args": [ - "get", "menu" + "get", "menu", "-o", "yaml", "--print-config", "true" ] } ] diff --git a/core/output/formatter.go b/core/output/formatter.go index c6b3159..2a9db20 100644 --- a/core/output/formatter.go +++ b/core/output/formatter.go @@ -6,7 +6,7 @@ import ( var Formatters = map[string]interfaces.Formatter{ "json": &JsonFormatter{}, - "yaml": nil, + "yaml": &YamlFormatter{}, "go": &GoFormatter{}, "table": nil, } diff --git a/core/output/yaml.go b/core/output/yaml.go new file mode 100644 index 0000000..020dedb --- /dev/null +++ b/core/output/yaml.go @@ -0,0 +1,19 @@ +package output + +import ( + "bytes" + "io" + + "github.com/goccy/go-yaml" +) + +type YamlFormatter struct{} + +func (f *YamlFormatter) Format(object any) (io.Reader, error) { + buffer, err := yaml.Marshal(object) + if err != nil { + return nil, err + } + + return bytes.NewBuffer(buffer), nil +} diff --git a/go.mod b/go.mod index 3f24465..17ffee2 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.0 require github.com/spf13/cobra v1.9.1 require ( + github.com/goccy/go-yaml v1.18.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.7 // indirect ) diff --git a/go.sum b/go.sum index 4aae07f..3c2a4cc 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= +github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=