aboutsummaryrefslogtreecommitdiff
path: root/sources.go
blob: d092eddc274f7035381d6e66170fbdd7ae24a8b6 (plain)
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
package main

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)
}