-
Notifications
You must be signed in to change notification settings - Fork 3
/
configuration.go
77 lines (63 loc) · 2.06 KB
/
configuration.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
// 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 (
"bufio"
"path/filepath"
"strings"
"unicode"
"github.com/alecthomas/units"
)
// Configuration represents information about ccache configuration.
type Configuration struct {
CacheDirectory string `json:"cache_directory"`
PrimaryConfig string `json:"primary_config"`
MaxCacheSize string `json:"max_cache_size"`
MaxCacheSizeBytes units.MetricBytes `json:"max_cache_size_bytes"`
}
func splitConfigurationField(c rune) bool {
return unicode.IsSpace(c) || c == '=' || c == '(' || c == ')'
}
// ParseConfiguration parses ccache configuration returned by the
// `--show-config` (ccache >= 3.7) or the `--print-config` (ccache < 3.7)
// command.
//
// See https://ccache.dev/releasenotes.html#_ccache_3_7
func ParseConfiguration(text string) (*Configuration, error) {
reader := strings.NewReader(text)
scanner := bufio.NewScanner(reader)
configuration := &Configuration{}
var err error
for scanner.Scan() {
// split each configuration line into 3 fields:
//
// (<configuration source>) <key> = <value>
fields := strings.FieldsFunc(scanner.Text(), splitConfigurationField)
if len(fields) < 3 {
continue
}
switch fields[1] {
case "cache_dir":
configuration.CacheDirectory = fields[2]
configuration.PrimaryConfig = filepath.Join(fields[2], "ccache.conf")
case "max_size":
var sanitizedMaxCacheSize string
if len(fields) == 4 {
sanitizedMaxCacheSize = fields[2] + fields[3]
} else {
sanitizedMaxCacheSize = fields[2]
}
sanitizedMaxCacheSize = strings.Replace(strings.ToUpper(sanitizedMaxCacheSize), " ", "", -1)
if !strings.HasSuffix(sanitizedMaxCacheSize, "B") {
sanitizedMaxCacheSize += "B"
}
configuration.MaxCacheSize = sanitizedMaxCacheSize
configuration.MaxCacheSizeBytes, err = units.ParseMetricBytes(sanitizedMaxCacheSize)
if err != nil {
return &Configuration{}, err
}
}
}
return configuration, nil
}