示例#1
0
func main() {
	if flag.NArg() != 0 {
		flag.Usage()
	}

	var tmp interface{}
	if err := json.NewDecoder(os.Stdin).Decode(&tmp); err != nil {
		log.Fatalf("Error decoding JSON: %s", err)
	}

	tomlData := translate(tmp)
	if err := toml.NewEncoder(os.Stdout).Encode(tomlData); err != nil {
		log.Fatalf("Error encoding TOML: %s", err)
	}
}
示例#2
0
func render(v *viper.Viper, configOutputType string) ([]byte, error) {

	var b []byte
	var err error
	var conf = make(map[string]interface{})

	m := v.AllSettings()
	b, err = yaml.Marshal(m)
	err = yaml.Unmarshal(b, conf)

	if err == nil {
		switch configOutputType {
		case "json":

			b, err = json.MarshalIndent(conf, "", "    ")

		case "toml":
			var buff bytes.Buffer

			err = toml.NewEncoder(&buff).Encode(conf)
			b = buff.Bytes()

		case "yaml", "yml":

			b, err = yaml.Marshal(conf)

		}
	}

	if err != nil {
		return nil, err
	}

	return b, nil

}