~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to identifier/identifier.go

  • Committer: Guillermo Gonzalez
  • Date: 2014-08-04 20:40:50 UTC
  • mto: (91.148.26 automatic)
  • mto: This revision was merged to the branch mainline in revision 119.
  • Revision ID: guillermo.gonzalez@canonical.com-20140804204050-0tbj2xmmy6cs9a6u
replace whoopsie with /var/lib/dbus/machine-id

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
18
// system id (from /var/lib/dbus/machine-id) used by the Ubuntu
 
19
// push notifications service.
 
20
package identifier
 
21
 
 
22
import (
 
23
        "fmt"
 
24
        "io/ioutil"
 
25
)
 
26
 
 
27
var machineIdPath = "/var/lib/dbus/machine-id"
 
28
 
 
29
// an Id knows how to generate itself, and how to stringify itself.
 
30
type Id interface {
 
31
        String() string
 
32
}
 
33
 
 
34
// Identifier is the default Id implementation.
 
35
type Identifier struct {
 
36
        value string
 
37
}
 
38
 
 
39
func readMachineId() (string, error) {
 
40
        value, err := ioutil.ReadFile(machineIdPath)
 
41
        if err != nil {
 
42
                return "", err
 
43
        }
 
44
        return string(value)[:len(value)-1], nil
 
45
}
 
46
 
 
47
// New creates an Identifier
 
48
func New() (Id, error) {
 
49
        value, err := readMachineId()
 
50
        if err != nil {
 
51
                return &Identifier{value: ""}, fmt.Errorf("Failed to read the machine id: %s", err)
 
52
        }
 
53
        return &Identifier{value: value}, nil
 
54
}
 
55
 
 
56
// String returns the system identifier as a string.
 
57
func (id *Identifier) String() string {
 
58
        return id.value
 
59
}