diff options
author | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-05-12 18:17:33 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-05-12 18:17:33 +0300 |
commit | d255ca1955b3658aaf12aba84ded8693d2be482f (patch) | |
tree | 99248b627073d82b0bbeccea2236357cc7a2f7a0 /main.go | |
parent | fd22b325e5c1fd29f7d8e91a6b78be2cc482f0ef (diff) |
Возможность сохранения json
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 +} |