func KvPatchCommand() cli.Command { return cli.Command{ Name: "patch", Usage: "Patch json values in consul kv", Flags: []cli.Flag{ cli.StringFlag{Name: "key"}, cli.StringFlag{Name: "value"}, cli.BoolFlag{Name: "dry-run"}, }, Action: func(c *cli.Context) error { consul, _ := api.NewClient(api.DefaultConfig()) list, _, err := consul.KV().List(c.String("key"), nil) if err != nil { return err } for _, kv := range list { consulValue := make(map[string]interface{}) if err := json.Unmarshal(kv.Value, &consulValue); err != nil { return err } patchValue := make(map[string]interface{}) if err := json.Unmarshal([]byte(c.String("value")), &patchValue); err != nil { return err } merged, err := mergemap.Merge(consulValue, patchValue) if err != nil { return err } mergedJson, _ := json.MarshalIndent(merged, "", " ") println(kv.Key) println(string(mergedJson) + "\n") if c.Bool("dry-run") { continue } _, putErr := consul.KV().Put(&api.KVPair{Key: kv.Key, Value: mergedJson}, nil) if putErr != nil { return putErr } } return nil }, } }
func includeFile(file string, tree *gabs.Container) error { loaded, err := loader.LoadFile(file) if err != nil { return err } log.Println("include:", file) merged, err := mergemap.Merge(loaded.Data().(map[string]interface{}), tree.Data().(map[string]interface{})) if err != nil { return err } _, err = tree.Set(merged) return err }