-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
114 lines (96 loc) · 2.66 KB
/
config.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
111
112
113
114
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
type Config struct {
ptr *C.Config
Repos map[string]*BaseRepo
ReposEbuild map[string]*EbuildRepo
ReposFake map[string]*FakeRepo
}
// Return a new config for the system.
func NewConfig() *Config {
ptr := C.pkgcraft_config_new()
config := &Config{ptr: ptr}
// force caller to explicitly close Config object, otherwise a panic occurs
_, file, line, _ := runtime.Caller(1)
runtime.SetFinalizer(config, func(self *Config) {
panic(fmt.Sprintf("%s:%d: unclosed config object", file, line))
})
return config
}
// Free a config object's encapsulated C pointer.
func (self *Config) Close() {
C.pkgcraft_config_free(self.ptr)
runtime.SetFinalizer(self, nil)
}
// Add an external repo via its file path.
func (self *Config) AddRepoPath(path string, id string, priority int) error {
path_str := C.CString(path)
defer C.free(unsafe.Pointer(path_str))
id_str := C.CString(id)
defer C.free(unsafe.Pointer(id_str))
ptr := C.pkgcraft_config_add_repo_path(self.ptr, id_str, C.int(priority), path_str, true)
if ptr == nil {
return newPkgcraftError()
}
self.updateRepos()
return nil
}
// Add an external repo.
func (self *Config) AddRepo(repo repoPtr) error {
ptr := C.pkgcraft_config_add_repo(self.ptr, repo.p(), false)
if ptr == nil {
return newPkgcraftError()
}
self.updateRepos()
return nil
}
// Load portage config from a given directory, falling back to default locations.
func (self *Config) LoadPortageConf(path string) error {
var c_path *C.char
if path != "" {
c_path = C.CString(path)
defer C.free(unsafe.Pointer(c_path))
}
ptr := C.pkgcraft_config_load_portage_conf(self.ptr, c_path)
if ptr != nil {
self.updateRepos()
return nil
} else {
return newPkgcraftError()
}
}
// Update the repo maps for a config.
func (self *Config) updateRepos() {
var length C.size_t
c_repos := C.pkgcraft_config_repos(self.ptr, &length)
repos := make(map[string]*BaseRepo)
for _, r := range unsafe.Slice(c_repos, length) {
s := C.pkgcraft_repo_id(r)
id := C.GoString(s)
defer C.pkgcraft_str_free(s)
repos[id] = repoFromPtr(r)
}
C.pkgcraft_array_free((*unsafe.Pointer)(unsafe.Pointer(c_repos)), length)
repos_ebuild := make(map[string]*EbuildRepo)
repos_fake := make(map[string]*FakeRepo)
for id, r := range repos {
switch format := r.format; format {
case RepoFormatEbuild:
repos_ebuild[id] = &EbuildRepo{r, nil}
case RepoFormatFake:
repos_fake[id] = &FakeRepo{r}
default:
panic(fmt.Sprintf("unknown repo format: %d", format))
}
}
self.Repos = repos
self.ReposEbuild = repos_ebuild
self.ReposFake = repos_fake
}