...
1 package metriton
2
3 import (
4 "errors"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8
9 "github.com/google/uuid"
10 )
11
12
13
14 func StaticInstallID(id string) func(*Reporter) (string, error) {
15 return func(*Reporter) (string, error) {
16 return id, nil
17 }
18 }
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 func userConfigDir() (string, error) {
35 var dir string
36 dir = os.Getenv("XDG_CONFIG_HOME")
37 if dir == "" {
38 dir = os.Getenv("HOME")
39 if dir == "" {
40 return "", errors.New("neither $XDG_CONFIG_HOME nor $HOME are defined")
41 }
42 dir += "/.config"
43 }
44 return dir, nil
45 }
46
47
48
49 func InstallIDFromFilesystem(reporter *Reporter) (string, error) {
50 dir, err := userConfigDir()
51 if err != nil {
52 return "", err
53 }
54
55 idFilename := filepath.Join(dir, reporter.Application, "id")
56 if idBytes, err := ioutil.ReadFile(idFilename); err == nil {
57 reporter.BaseMetadata["new_install"] = false
58 return string(idBytes), nil
59 } else if !os.IsNotExist(err) {
60 return "", err
61 }
62
63 id := uuid.New().String()
64 reporter.BaseMetadata["new_install"] = true
65
66 if err := os.MkdirAll(filepath.Dir(idFilename), 0755); err != nil {
67 return "", err
68 }
69 if err := ioutil.WriteFile(idFilename, []byte(id), 0644); err != nil {
70 return "", err
71 }
72 return id, nil
73 }
74
View as plain text