First commit
This commit is contained in:
61
data/data.go
Normal file
61
data/data.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type (
|
||||
Service struct {
|
||||
Softwares []Software
|
||||
}
|
||||
|
||||
Software struct {
|
||||
UUID string
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Version string `json:"version"`
|
||||
IconURL string `json:"icon_url"`
|
||||
ScreenshotURLs []string `json:"screenshot_urls"`
|
||||
DownloadLinks []DownloadLink `json:"download_links"`
|
||||
}
|
||||
|
||||
DownloadLink struct {
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
document struct {
|
||||
Softwares []Software `json:"softwares"`
|
||||
}
|
||||
)
|
||||
|
||||
func Load(path string) Service {
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0744)
|
||||
if err != nil {
|
||||
panic("failed to open data file: " + err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var s document
|
||||
d := json.NewDecoder(f)
|
||||
err = d.Decode(&s)
|
||||
if err != nil {
|
||||
panic("failed to parse data file: " + err.Error())
|
||||
}
|
||||
s = generateUUID(s)
|
||||
return Service{
|
||||
Softwares: s.Softwares,
|
||||
}
|
||||
}
|
||||
|
||||
func generateUUID(d document) document {
|
||||
for i, s := range d.Softwares {
|
||||
s.UUID = uuid.New().String()
|
||||
d.Softwares[i] = s
|
||||
}
|
||||
return d
|
||||
}
|
||||
Reference in New Issue
Block a user