2023-01-31 13:47:44 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-06-22 09:31:41 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2023-01-31 13:47:44 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/configitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) GetConfigItem(key string) (*string, error) {
|
|
|
|
result, err := c.Ent.ConfigItem.Query().Where(configitem.NameEQ(key)).First(c.CTX)
|
|
|
|
if err != nil && ent.IsNotFound(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(QueryFail, "select config item: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result.Value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetConfigItem(key string, value string) error {
|
|
|
|
|
|
|
|
nbUpdated, err := c.Ent.ConfigItem.Update().SetValue(value).Where(configitem.NameEQ(key)).Save(c.CTX)
|
|
|
|
if (err != nil && ent.IsNotFound(err)) || nbUpdated == 0 { //not found, create
|
|
|
|
err := c.Ent.ConfigItem.Create().SetName(key).SetValue(value).Exec(c.CTX)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(QueryFail, "insert config item: %s", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return errors.Wrapf(QueryFail, "update config item: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|