2024-03-10 18:55:42 +01:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2024-07-08 13:15:04 +02:00
|
|
|
"go.etcd.io/bbolt"
|
2024-07-07 20:08:23 +02:00
|
|
|
"strings"
|
2024-03-10 18:55:42 +01:00
|
|
|
)
|
|
|
|
|
2024-07-07 19:12:21 +02:00
|
|
|
func (d *BoltDb) GetOptions(params db.RetrieveQueryParams) (res map[string]string, err error) {
|
2024-07-07 20:08:23 +02:00
|
|
|
res = make(map[string]string)
|
2024-07-06 19:33:16 +02:00
|
|
|
var options []db.Option
|
2024-07-07 20:08:23 +02:00
|
|
|
err = d.getObjects(0, db.OptionProps, db.RetrieveQueryParams{}, func(i interface{}) bool {
|
|
|
|
|
|
|
|
option := i.(db.Option)
|
|
|
|
if params.Filter == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return option.Key == params.Filter || strings.HasPrefix(option.Key, params.Filter+".")
|
|
|
|
|
|
|
|
}, &options)
|
2024-07-06 19:33:16 +02:00
|
|
|
for _, opt := range options {
|
|
|
|
res[opt.Key] = opt.Value
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-10 18:55:42 +01:00
|
|
|
func (d *BoltDb) SetOption(key string, value string) error {
|
|
|
|
|
|
|
|
opt := db.Option{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
|
2024-03-10 19:00:15 +01:00
|
|
|
_, err := d.getOption(key)
|
2024-03-10 18:55:42 +01:00
|
|
|
|
|
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
|
|
_, err = d.createObject(-1, db.OptionProps, opt)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
err = d.updateObject(-1, db.OptionProps, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-10 19:00:15 +01:00
|
|
|
func (d *BoltDb) getOption(key string) (value string, err error) {
|
|
|
|
var option db.Option
|
|
|
|
err = d.getObject(-1, db.OptionProps, strObjectID(key), &option)
|
|
|
|
value = option.Value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-10 18:55:42 +01:00
|
|
|
func (d *BoltDb) GetOption(key string) (value string, err error) {
|
|
|
|
var option db.Option
|
|
|
|
err = d.getObject(-1, db.OptionProps, strObjectID(key), &option)
|
|
|
|
value = option.Value
|
2024-03-10 19:00:15 +01:00
|
|
|
|
|
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2024-03-10 18:55:42 +01:00
|
|
|
return
|
|
|
|
}
|
2024-07-08 13:15:04 +02:00
|
|
|
|
|
|
|
func (d *BoltDb) DeleteOption(key string) (err error) {
|
|
|
|
err = db.ValidateOptionKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return d.deleteObject(-1, db.OptionProps, strObjectID(key), tx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteOptions(filter string) (err error) {
|
|
|
|
err = db.ValidateOptionKey(filter)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|