Debian Codegraph @

identifier/identifier.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Package identifier is the source of an anonymous and stable system id (from
18 // /var/lib/dbus/machine-id) used by the push notifications service.
19 package identifier
20
21 import (
22 "fmt"
23 "io/ioutil"
24 )
25
26 var machineIdPath = "/var/lib/dbus/machine-id"
27
28 // an Id knows how to generate itself, and how to stringify itself.
29 type Id interface {
30 String() string
31 }
32
33 // Identifier is the default Id implementation.
34 type Identifier struct {
35 value string
36 }
37
38 func readMachineId() (string, error) {
39 value, err := ioutil.ReadFile(machineIdPath)
40 if err != nil {
41 return "", err
42 }
43 return string(value)[:len(value)-1], nil
44 }
45
46 // New creates an Identifier
47 func New() (Id, error) {
48 value, err := readMachineId()
49 if err != nil {
50 return &Identifier{value: ""}, fmt.Errorf("failed to read the machine id: %s", err)
51 }
52 return &Identifier{value: value}, nil
53 }
54
55 // String returns the system identifier as a string.
56 func (id *Identifier) String() string {
57 return id.value
58 }