github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmg/SubCommand/Json2Yaml.go.bak (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  
     6  	"github.com/bronze1man/kmg/console"
     7  	"github.com/bronze1man/kmg/encoding/kmgYaml"
     8  	"github.com/bronze1man/kmg/kmgFile"
     9  )
    10  
    11  type Json2Yaml struct {
    12  	inputPath  *string
    13  	outputPath *string
    14  }
    15  
    16  func (command *Json2Yaml) GetNameConfig() *console.NameConfig {
    17  	return &console.NameConfig{Name: "Json2Yaml",
    18  		Short: "convert from json to yaml",
    19  	}
    20  }
    21  
    22  func (command *Json2Yaml) ConfigFlagSet(flag *flag.FlagSet) {
    23  	command.inputPath = flag.String("i", "", "input file path")
    24  	command.outputPath = flag.String("o", "", "output file path")
    25  }
    26  func (command *Json2Yaml) Execute(context *console.Context) error {
    27  	inputPath := *command.inputPath
    28  	outputPath := *command.outputPath
    29  	if inputPath == "" || outputPath == "" {
    30  		return kmgYaml.Json2YamlIo(context.Stdin, context.Stdout)
    31  	}
    32  	transform := &kmgFile.DirectoryFileTransform{
    33  		InputExt:  "yml",
    34  		OuputExt:  "json",
    35  		Transform: kmgYaml.Json2YamlIo,
    36  	}
    37  	return transform.Run(inputPath, outputPath)
    38  }
    39