diff --git a/app/cmd/common.go b/app/cmd/common.go new file mode 100644 index 0000000000000000000000000000000000000000..ea4803d56aed87c68c3bdbc6803f7b6016de561e --- /dev/null +++ b/app/cmd/common.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "encoding/json" + "fmt" + + "gopkg.in/yaml.v2" +) + +type OutputOption struct { + Format string +} + +const ( + JsonOutputFormat string = "json" + YAMLOutputFormat string = "yaml" +) + +func Format(obj interface{}, format string) (data []byte, err error) { + if format == JsonOutputFormat { + return json.MarshalIndent(obj, "", " ") + } else if format == YAMLOutputFormat { + return yaml.Marshal(obj) + } + + return nil, fmt.Errorf("not support format %s", format) +}