func (c *Client) Set(ft *models.Feature) error { defer c.Store.Close() var existing *models.Feature kvb, err := c.Store.Get(ft.ScopedKey()) if err != nil { return err } if kvb != nil { err = json.Unmarshal(kvb.Bytes, &existing) if err != nil { return err } } if err != nil { return err } if existing != nil { if ft.Comment == "" { ft.Comment = existing.Comment } if ft.Value == nil { ft.Value = existing.Value } if ft.FeatureType != existing.FeatureType && ft.FeatureType != "" { return ErrTypeChange } if ft.FeatureType == "" { ft.FeatureType = existing.FeatureType } } else { if ft.Value == nil { return ErrNilValue } } bts, err := ft.ToJSON() if err != nil { return err } err = c.Store.Set(ft.ScopedKey(), bts) if err != nil { return err } err = c.SendStatEvent(ft, false) return nil }
func (c *Client) Commit(ft *models.Feature, deleted bool) error { if !c.Repo.Exists() { err := c.Repo.Clone() if err != nil { return err } } kvb, err := c.Store.List(fmt.Sprintf("%s/features", c.Namespace())) if err != nil { return err } fm, err := c.KVsToFeatureMap(kvb) if err != nil { return err } bts, err := json.MarshalIndent(fm, "", " ") if err != nil { return err } var msg string if deleted { msg = fmt.Sprintf("%s deleted %s", ft.UpdatedBy, ft.ScopedKey()) } else { msg = fmt.Sprintf("%s set %s to %v", ft.UpdatedBy, ft.ScopedKey(), ft.Value) } err = c.Repo.Commit(bts, msg) if err != nil { return err } return nil }
func (c *Client) SendStatEvent(f *models.Feature, delete bool) error { if c.Stats == nil { return nil } var text string title := "Decider Change" if delete { text = fmt.Sprintf("deleted %s", f.ScopedKey()) } else { text = fmt.Sprintf("set %s: %v", f.ScopedKey(), f.Value) } optionals := make(map[string]string) optionals["alert_type"] = "info" optionals["source_type_name"] = "dcdr" tags := []string{"source_type:dcdr"} return c.Stats.Event(title, text, optionals, tags) }