aboutsummaryrefslogtreecommitdiff
path: root/internal/yae/sources.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/yae/sources.go')
-rw-r--r--internal/yae/sources.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/yae/sources.go b/internal/yae/sources.go
new file mode 100644
index 0000000..f8cb2b0
--- /dev/null
+++ b/internal/yae/sources.go
@@ -0,0 +1,53 @@
+package yae
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+)
+
+type Sources map[string]Source
+
+func (s *Sources) Add(name string, d Source) error {
+ if s.Exists(name) {
+ return fmt.Errorf("source already exists")
+ }
+
+ (*s)[name] = d
+
+ return nil
+}
+
+func (s *Sources) Exists(name string) bool {
+ _, ok := (*s)[name]
+
+ return ok
+}
+
+func (s *Sources) Drop(url string) {
+ delete((*s), url)
+}
+
+func (s *Sources) Save(path string) error {
+ file, err := os.Create(path)
+
+ if err != nil {
+ return err
+ }
+
+ encoder := json.NewEncoder(file)
+
+ encoder.SetIndent("", " ")
+
+ return encoder.Encode(s)
+}
+
+func (s *Sources) Load(path string) error {
+ file, err := os.Open(path)
+
+ if err != nil {
+ return err
+ }
+
+ return json.NewDecoder(file).Decode(s)
+}