-
Notifications
You must be signed in to change notification settings - Fork 33
/
catalog.go
126 lines (110 loc) · 2.96 KB
/
catalog.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
package catalog
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"time"
)
// Entry contains the properties of a project which are recorded as part of the catalog
type Entry struct {
ProjectName string `json:"project_name"`
URL string `json:"project_url,omitempty"`
Author string `json:"author,omitempty"`
Homepage string `json:"home_page,omitempty"`
Description string `json:"description"`
Discussion string `json:"discussion_url,omitempty"`
Documentation string `json:"documentation_url,omitempty"`
Tags []string `json:"tags,omitempty"`
Version string `json:"version,omitempty"`
Origin string `json:"origin,omitempty"`
}
// catalogContent represents the contents of a catalog file
type catalogContent struct {
Header Header `json:"file_info"`
Name string `json:"catalog_name"`
Date time.Time `json:"date,omitempty"`
Entries []Entry `json:"entries"`
}
// Catalog is collected information about
type Catalog struct {
content *catalogContent
}
var (
catalogDefaultHeader Header
)
func init() {
catalogDefaultHeader = Header{
Version: 1,
Kind: "script_catalog",
}
}
// New creates a new empty catalog
func New(name string) *Catalog {
return &Catalog{
content: &catalogContent{
Name: name,
Header: catalogDefaultHeader,
Entries: make([]Entry, 0),
},
}
}
// Load loads an existing catalog in json form from the given reader
func Load(r io.Reader) (*Catalog, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
// TODO: deal with upgrading the
var content catalogContent
err = json.Unmarshal(data, &content)
if err != nil {
return nil, err
}
return &Catalog{content: &content}, nil
}
// Store writes the catalog in json form to the given writer
func (c *Catalog) Store(w io.Writer) error {
if c.content.Header != catalogDefaultHeader {
return errors.New("catalog header version/kind does not match")
}
data, err := json.MarshalIndent(c.content, "", " ")
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
// Insert places the given entry in the catalog potentially replacing an entry
// with the name name
func (c *Catalog) Insert(entry *Entry) {
for i, e := range c.content.Entries {
if e.ProjectName == entry.ProjectName {
c.content.Entries[i] = *entry
return
}
}
// else we haven't found a matching entry, append
c.content.Entries = append(c.content.Entries, *entry)
}
// Get returns the first matching entry found in the catalog or nil
func (c *Catalog) Get(name string) *Entry {
for _, e := range c.content.Entries {
if e.ProjectName == name {
return &e
}
}
return nil
}
// Entries returns all the entries in the catalog
func (c *Catalog) Entries() []Entry {
return c.content.Entries
}
// Name returns the name of the catalog
func (c *Catalog) Name() string {
return c.content.Name
}
// Updated returns the update time for catalog
func (c *Catalog) Updated() time.Time {
return c.content.Date
}