forked from janimo/textsecure
-
Notifications
You must be signed in to change notification settings - Fork 4
/
contacts.go
46 lines (38 loc) · 896 Bytes
/
contacts.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
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"io/ioutil"
"gopkg.in/yaml.v2"
)
// Contact contains information about a contact.
type Contact struct {
Name string
Tel string
Photo string
}
type yamlContacts struct {
Contacts []Contact
}
// ReadContacts reads a YAML contacts file
func ReadContacts(fileName string) ([]Contact, error) {
b, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
contacts := &yamlContacts{}
err = yaml.Unmarshal(b, contacts)
if err != nil {
return nil, err
}
return contacts.Contacts, nil
}
// WriteContacts saves a list of contacts to a file
func WriteContacts(filename string, contacts []Contact) error {
c := &yamlContacts{contacts}
b, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, 0600)
}