chore: initial commit

This commit is contained in:
2022-02-26 14:08:44 +00:00
commit b67da4accb
12 changed files with 1616 additions and 0 deletions

52
render.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import (
"encoding"
"encoding/json"
"io"
"gopkg.in/yaml.v3"
)
func render(w io.Writer, format string, v interface{}) error {
if format == "yaml" {
return renderYAML(w, v)
}
if format == "json" {
return renderJSON(w, v)
}
if wt, ok := v.(io.WriterTo); ok {
_, err := wt.WriteTo(w)
return err
}
if tm, ok := v.(encoding.TextMarshaler); ok {
b, err := tm.MarshalText()
if err != nil {
return err
}
_, err = w.Write(b)
return err
}
return renderYAML(w, v)
}
func renderYAML(w io.Writer, v interface{}) error {
enc := yaml.NewEncoder(w)
enc.SetIndent(2)
return enc.Encode(v)
}
func renderJSON(w io.Writer, v interface{}) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(v)
}