-
Notifications
You must be signed in to change notification settings - Fork 30
/
reports.go
72 lines (61 loc) · 1.29 KB
/
reports.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package utils
import (
"os"
"os/user"
"path/filepath"
"github.com/pkg/errors"
)
const (
defaultCacheDir = ".cache"
reportDir = "ubuntu-report"
)
var (
// ErrFormat used to print debug messages.
// Only for log.() msg, not errors.() error wrapping!
ErrFormat = "%v"
)
// ReportPath of last saved report
func ReportPath(distro, version string, cacheP string) (string, error) {
if cacheP == "" {
var err error
if cacheP, err = cacheDir(); err != nil {
return "", err
}
}
return filepath.Join(cacheP, reportDir, distro+"."+version), nil
}
// PendingReportPath of last saved pending report
func PendingReportPath(cacheP string) (string, error) {
if cacheP == "" {
var err error
if cacheP, err = cacheDir(); err != nil {
return "", err
}
}
return filepath.Join(cacheP, reportDir, "pending"), nil
}
func cacheDir() (string, error) {
d := os.Getenv("XDG_CACHE_HOME")
if filepath.IsAbs(d) {
return d, nil
}
if d == "" {
d = defaultCacheDir
}
h, err := getHomeDir()
if err != nil {
return "", err
}
return filepath.Join(h, d), nil
}
func getHomeDir() (string, error) {
d := os.Getenv("HOME")
if d == "" {
usr, err := user.Current()
if err != nil {
return "", errors.Wrapf(err, "couldn't get user home directory")
}
d = usr.HomeDir
}
return d, nil
}