/
utils.go
106 lines (94 loc) · 2.54 KB
/
utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path"
"regexp"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
// getDistro returns the host OS' distro ID (e.g. 'fedora', 'arch')
func getDistro() (distro string, err error) {
file, err := os.Open("/etc/os-release")
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
// Look for pretty name
re := regexp.MustCompile(`ID="?([^"\s]+)"?`)
for scanner.Scan() {
m := re.FindSubmatch(scanner.Bytes())
if len(m) > 1 {
distro = string(m[1])
return
}
}
return distro, fmt.Errorf("cannot get distro ID")
}
// userHomeCacheDir returns user cache directory with subfolder for this program
//
// Will fail if this directory cannot be created, typically `$HOME/.cache/go-check-updates`
func userHomeCacheDir() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
ret := path.Join(dir, packageName)
// Create if missing
err = os.MkdirAll(ret, 0700)
return ret, err
}
// getHomeCache returns a file pointer to the cache file in the user's cache directory
func getHomeCache() (string, error) {
dir, err := userHomeCacheDir()
if err != nil {
return "", err
}
return path.Join(dir, "cache.json"), nil
}
// getCachePath returns path to cache file
//
// By default it is at `defaultCache` but it may be `$HOME/.cache/go-check-updates/cache.json` if default is not writable.
func getCachePath() (string, error) {
dir := os.TempDir()
tmpCache := path.Join(dir, packageName+".json")
if checkFileExists(tmpCache) && !checkFileWrite(tmpCache) {
return getHomeCache()
}
return tmpCache, nil
}
// checkFileWrite returns nil if the file can be written to OK
// It is considered invalid if the current user is not its owner
func checkFileWrite(path string) bool {
return unix.Access(path, unix.W_OK|unix.R_OK) == nil
}
// checkFileRead returns true if the file is readable
func checkFileRead(path string) bool {
return unix.Access(path, unix.R_OK) == nil
}
// checkFileExists returns true if the file exists
func checkFileExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func runCmd(name string, args ...string) (string, error) {
log.Debugf("runCmd %s %s", name, args)
var buf bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Stdout = &buf
err := cmd.Run()
return buf.String(), err
}
// checkCmd returns true if '<name> --help' ran successfully
func checkCmd(name string) bool {
cmd := exec.Command(name, "--help")
err := cmd.Run()
return err == nil
}