123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package main
-
- import (
- "encoding/json"
- "io"
- "io/ioutil"
- "os"
- )
-
- type SongsFile struct {
- Songs []*SongEntry
- }
-
- type SongEntry struct {
- Week int
- Title string
- Artist string
- URL string
- Sync bool
- }
-
- func loadDb() (SongsFile, error) {
-
- var v SongsFile
- f, err := os.Open("songs.json")
- if err != nil {
- return v, err
- }
- defer f.Close()
-
- if err != nil {
- return v, err
- }
- dec := json.NewDecoder(f)
- dec.UseNumber()
- for {
- if err := dec.Decode(&v); err == io.EOF {
- break
- } else if err != nil {
- return v, err
- }
- }
- return v, nil
- }
-
- func saveDb(v SongsFile) error {
- jsonValue, err := json.Marshal(v)
- if err != nil {
- return err
- }
- err = ioutil.WriteFile("songs.json", jsonValue, 0644)
- return err
- }
|