-
Notifications
You must be signed in to change notification settings - Fork 0
/
configLoader.go
97 lines (80 loc) · 2.17 KB
/
configLoader.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os/exec"
"strings"
)
func getClangFormatVersion(clangCmd string) string {
out, err := exec.Command(clangCmd, "-version").Output()
if err != nil {
log.Fatal(err)
}
pos := strings.Index(string(out), "version ")
if pos == -1 {
return ""
}
version := string(out)[pos+8:]
pos = strings.Index(version, " ")
if pos == -1 {
return ""
}
version = version[:pos]
return version
}
func loadConfig(clangPath string) (map[string]*ConfigEntry, error) {
var entries map[string]*ConfigEntry
//Parse clang version
version := getClangFormatVersion(clangPath)
if len(version) == 0 {
return nil, errors.New("Failed to fetch clang version")
}
fmt.Println("Using clang version: " + version)
configsData, err := ioutil.ReadFile(configs_file)
if err != nil {
return nil, errors.New("Failed to load " + configs_file)
}
//Read clang configs : load as raw json (just extract keys and store internal value for later parsing)
var data map[string]json.RawMessage
err = json.Unmarshal(configsData, &data)
if err != nil {
return nil, errors.New("Failed to parse " + configs_file)
}
//Parse Values for version key
var availableVersions []string
err = json.Unmarshal(data["versions"], &availableVersions)
if err != nil {
return nil, errors.New("Failed to parse " + configs_file)
}
//Match with current local version
configVersionEntry := ""
for _, v := range availableVersions {
if strings.Index(version, v) == 0 {
configVersionEntry = v
break
}
}
if len(configVersionEntry) == 0 {
fmt.Println("Failed to match version " + version + " with known versions, will use HEAD instead")
configVersionEntry = "HEAD"
}
fmt.Println("Will be using configuration settings for version " + configVersionEntry)
//Now parse keys for given version
configVersionitems := data[configVersionEntry]
err = json.Unmarshal(configVersionitems, &entries)
if err != nil {
return nil, errors.New("Failed to parse " + configs_file)
}
for k, v := range entries {
fmt.Print(k + ": " + v.Type)
if len(v.Options) != 0 {
fmt.Print(" Options:")
fmt.Print(v.Options)
}
fmt.Println("")
}
return entries, nil
}