-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrapper.go
110 lines (85 loc) · 2.16 KB
/
wrapper.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
107
108
109
110
// Copyright 2018-2022 VirtualTam.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package ccache
import (
"errors"
"regexp"
"github.com/Masterminds/semver/v3"
)
var (
ErrVersionMissing error = errors.New("command: missing version")
)
var (
versionRegex = regexp.MustCompile("ccache version (.+)")
useLegacyParserForVersionsBelow = semver.MustParse("3.7")
)
// Wrapper provides an abstraction for ccache commands.
type Wrapper struct {
command Command
version semver.Version
}
// NewWrapper initializes and returns a new Wrapper.
func NewWrapper(c Command) *Wrapper {
w := &Wrapper{
command: c,
}
v, err := w.Version()
if err != nil {
panic(err)
}
w.version = *v
return w
}
// Configuration returns the current ccache configuration.
func (w *Wrapper) Configuration() (*Configuration, error) {
var out string
var err error
if w.version.LessThan(useLegacyParserForVersionsBelow) {
out, err = w.command.PrintConfig()
} else {
out, err = w.command.ShowConfig()
}
if err != nil {
return &Configuration{}, err
}
return ParseConfiguration(out)
}
// Statistics returns the current ccache statistics.
func (w *Wrapper) Statistics() (*Statistics, error) {
if w.version.LessThan(useLegacyParserForVersionsBelow) {
return w.legacyStatistics()
}
return w.tsvStatistics()
}
func (w *Wrapper) legacyStatistics() (*Statistics, error) {
out, err := w.command.ShowStats()
if err != nil {
return &Statistics{}, err
}
stats, err := ParsePre37Statistics(out)
return stats, err
}
func (w *Wrapper) tsvStatistics() (*Statistics, error) {
out, err := w.command.PrintStats()
if err != nil {
return &Statistics{}, err
}
return ParseTSVStatistics(out)
}
// Version returns the semantic version for ccache.
func (w *Wrapper) Version() (*semver.Version, error) {
out, err := w.command.Version()
if err != nil {
return &semver.Version{}, err
}
matches := versionRegex.FindStringSubmatch(out)
if len(matches) != 2 {
return &semver.Version{}, ErrVersionMissing
}
version, err := semver.NewVersion(matches[1])
if err != nil {
return &semver.Version{}, err
}
return version, nil
}