From cda23f29f46b7573e0246d6b994437def0248164 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie Date: Thu, 27 Jun 2019 10:32:55 +0800 Subject: [PATCH] Add format function --- app/cmd/common.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/cmd/common.go diff --git a/app/cmd/common.go b/app/cmd/common.go new file mode 100644 index 0000000..ea4803d --- /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) +} -- GitLab