diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 21 |
1 files changed, 17 insertions, 4 deletions
@@ -1,7 +1,9 @@ package main import ( + "encoding/json" "flag" + "fmt" "os" "strings" @@ -11,15 +13,15 @@ import ( var ( input = stringsArray{} output = "" - indent = 2 replaceArrays = false + outType = "yaml" ) func main() { flag.Var(&input, "i", "input files") flag.StringVar(&output, "o", "out.yaml", "output file") - flag.IntVar(&indent, "indent", 2, "changes the used indentation used when encoding") flag.BoolVar(&replaceArrays, "replace_arrays", false, "replace arrays with same keys. Merge otherwise.") + flag.StringVar(&outType, "out_type", "yaml", "output type, 'yaml' (default) or 'json'") flag.Parse() result := map[string]any{} @@ -42,8 +44,15 @@ func main() { } defer fp.Close() - enc := yaml.NewEncoder(fp) - enc.SetIndent(indent) + var enc Encoder + switch outType { + case "yaml": + enc = yaml.NewEncoder(fp) + case "json": + enc = json.NewEncoder(fp) + default: + panic(fmt.Errorf("unknown output type: %s", outType)) + } if err := enc.Encode(result); err != nil { panic(err) } @@ -86,3 +95,7 @@ func (i *stringsArray) Set(value string) error { func (i *stringsArray) String() string { return strings.Join(*i, ",") } + +type Encoder interface { + Encode(any) error +} |