-
Notifications
You must be signed in to change notification settings - Fork 19
/
profile.go
78 lines (65 loc) · 1.72 KB
/
profile.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
package config
import (
"encoding/json"
"fmt"
"os"
fp "path/filepath"
)
// Profile is struct containing path to qmake, moc, rcc, C compiler and C++ compiler
type Profile struct {
OS string
Arch string
Static bool
Qmake string
Moc string
Rcc string
Gcc string
Gxx string
Windres string
Objdump string
}
// LoadProfiles load all profiles inside config file in the specified configPath.
func LoadProfiles(configPath string) (map[string]Profile, error) {
// If config file doesn't exist, return empty map
if !fileExists(configPath) {
return map[string]Profile{}, nil
}
// Open file
configFile, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer configFile.Close()
// Decode JSON
profiles := map[string]Profile{}
err = json.NewDecoder(configFile).Decode(&profiles)
return profiles, err
}
// LoadProfile loads profile with specified name from config file.
func LoadProfile(configPath string, name string) (Profile, error) {
profiles, err := LoadProfiles(configPath)
if err != nil {
return Profile{}, err
}
if prof, ok := profiles[name]; ok {
return prof, nil
}
return Profile{}, fmt.Errorf("profile %s doesn't exist", name)
}
// SaveProfiles saves the profile as JSON in the specified configPath.
func SaveProfiles(configPath string, profiles map[string]Profile) error {
// Make sure config dir is exists
os.MkdirAll(fp.Dir(configPath), os.ModePerm)
// Create file
configFile, err := os.Create(configPath)
if err != nil {
return err
}
defer configFile.Close()
// Encode profiles to JSON
return json.NewEncoder(configFile).Encode(&profiles)
}
func fileExists(filePath string) bool {
info, err := os.Stat(filePath)
return !os.IsNotExist(err) && !info.IsDir()
}