/
config.go
51 lines (45 loc) · 943 Bytes
/
config.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
48
49
50
51
package config
import (
"errors"
"github.com/zjyl1994/toot_anywhere/model"
"github.com/zjyl1994/toot_anywhere/vars"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func Get(key string) (string, error) {
var m model.Config
err := vars.DB.First(&m, "name = ?", key).Error
if err != nil {
return "", err
}
return m.Data, nil
}
func IsSet(key string) (bool, error) {
if _, err := Get(key); err == nil {
return true, nil
} else {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
} else {
return false, err
}
}
}
func MustGet(key, defautValue string) string {
result, err := Get(key)
if err != nil {
return defautValue
} else {
return result
}
}
func Set(key, value string) error {
m := model.Config{
Name: key,
Data: value,
}
return vars.DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
DoUpdates: clause.AssignmentColumns([]string{"data"}),
}).Create(&m).Error
}