feat: add cli/format package

This commit is contained in:
2023-12-15 20:01:37 +01:00
parent e40a8515cf
commit 4a8add1d3d
9 changed files with 386 additions and 0 deletions

38
cli/format/json/writer.go Normal file
View File

@ -0,0 +1,38 @@
package json
import (
"encoding/json"
"io"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cli/format"
)
const Format format.Format = "json"
func init() {
format.Register(Format, NewWriter())
}
type Writer struct{}
// Format implements format.Writer.
func (*Writer) Write(writer io.Writer, hints format.Hints, data ...any) error {
encoder := json.NewEncoder(writer)
if hints.OutputMode == format.OutputModeWide {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(data); err != nil {
return errors.WithStack(err)
}
return nil
}
func NewWriter() *Writer {
return &Writer{}
}
var _ format.Writer = &Writer{}