forked from riking/AutoDelete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
131 lines (116 loc) · 2.66 KB
/
storage.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package autodelete
import (
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
)
// Interface to the storage systems.
type Storage interface {
ListChannels() ([]string, error)
// Special errors:
// - os.IsNotExist() - no configuration for channel
GetChannel(id string) (ManagedChannelMarshal, error)
SaveChannel(conf ManagedChannelMarshal) error
DeleteChannel(id string) error
IsBanned(guildID string) (bool, error)
AddBan(guildID string) error
}
/******************
* Disk Storage *
******************/
// Stores channel configurations on disk as YAML files.
type DiskStorage struct {
}
const pathChannelConfDir = "./data"
const pathChannelConfig = "./data/%s.yml"
const pathBanList = "./data/bans.yml"
func (s *DiskStorage) ListChannels() ([]string, error) {
files, err := ioutil.ReadDir(pathChannelConfDir)
if err != nil {
return nil, err
}
channelIDs := make([]string, 0, len(files))
for _, v := range files {
n := v.Name()
if !strings.HasSuffix(n, ".yml") {
continue
}
if strings.HasPrefix(n, "bans.yml") {
continue
}
chID := strings.TrimSuffix(n, ".yml")
channelIDs = append(channelIDs, chID)
}
return channelIDs, nil
}
func (s *DiskStorage) GetChannel(channelID string) (ManagedChannelMarshal, error) {
var conf ManagedChannelMarshal
fileName := fmt.Sprintf(pathChannelConfig, channelID)
f, err := os.Open(fileName)
if os.IsNotExist(err) {
return conf, os.ErrNotExist
} else if err != nil {
return conf, err
}
by, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return conf, err
}
err = yaml.Unmarshal(by, &conf)
if err != nil {
return conf, err
}
conf = internalMigrateConfig(conf)
return conf, nil
}
func (s *DiskStorage) SaveChannel(conf ManagedChannelMarshal) error {
conf = internalMigrateConfig(conf)
by, err := yaml.Marshal(conf)
if err != nil {
panic(err)
}
fileName := fmt.Sprintf(pathChannelConfig, conf.ID)
f, err := os.Create(fileName)
if err != nil {
return err
}
f.Write(by)
err = f.Close()
if err != nil {
return err
}
return nil
}
func (s *DiskStorage) DeleteChannel(id string) error {
fileName := fmt.Sprintf(pathChannelConfig, id)
err := os.Remove(fileName)
if err != nil {
return err
}
return nil
}
func (s *DiskStorage) IsBanned(guildID string) (bool, error) {
by, err := ioutil.ReadFile(pathBanList)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
var conf BansFile
err = yaml.Unmarshal(by, &conf)
if err != nil {
return false, err
}
for _, v := range conf.Guilds {
if v == guildID {
return true, nil
}
}
return false, nil
}
func (s *DiskStorage) AddBan(guildID string) error {
return fmt.Errorf("unimplemented!")
}