/
flat.go
47 lines (39 loc) · 1005 Bytes
/
flat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Copyright © 2019 victor23d <victor6742x@gmail.com>
This file is part of {{ .appName }}.
*/
package cmd
import (
"errors"
"github.com/prometheus/common/log"
"github.com/spf13/cobra"
"github.com/victor23d/etcd-batch/common"
"github.com/victor23d/etcd-batch/flat"
)
// flatCmd represents the flat command
var flatCmd = &cobra.Command{
Use: "flat",
Short: "flat a hierarchical file",
Long: `Example: etcd-batch flat -f foo.json`,
Run: func(cmd *cobra.Command, args []string) {
log.Info(filename)
if filename == "" {
log.Fatal(errors.New("must specify -f"))
}
m, err := common.ReadJSONFromFile(filename)
if err != nil {
log.Fatal(err)
}
fp := make(map[string]interface{})
flat.FlatMap(m, fp, sep, prefix)
sfp := flat.StringFlatedMap(fp)
log.Info(sfp)
sb := flat.TextSFP(sfp)
log.Info("=== Text ===")
log.Info(sb.String())
},
}
func init() {
rootCmd.AddCommand(flatCmd)
flatCmd.Flags().StringVarP(&filename, "filename", "f", "", "the file to apply")
}