-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_base.go
105 lines (88 loc) · 2.62 KB
/
repo_base.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
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"runtime"
"unsafe"
)
type BaseRepo struct {
ptr *C.Repo
format RepoFormat
}
func (self *BaseRepo) p() *C.Repo {
return self.ptr
}
// Return a repo's id.
func (self *BaseRepo) Id() string {
s := C.pkgcraft_repo_id(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
// Return a repo's path.
func (self *BaseRepo) Path() string {
s := C.pkgcraft_repo_path(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
// Return if a repo is empty.
func (self *BaseRepo) IsEmpty() bool {
return bool(C.pkgcraft_repo_is_empty(self.ptr))
}
// Return the number of packages in a repo.
func (self *BaseRepo) Len() int {
return int(C.pkgcraft_repo_len(self.ptr))
}
func (self *BaseRepo) String() string {
return self.Id()
}
// Compare a repo with another repo returning -1, 0, or 1 if the first is less
// than, equal to, or greater than the second, respectively.
func (self *BaseRepo) Cmp(other repoPtr) int {
return int(C.pkgcraft_repo_cmp(self.ptr, other.p()))
}
func (self *BaseRepo) createPkg(ptr *C.Pkg) *BasePkg {
format := PkgFormat(C.pkgcraft_pkg_format(ptr))
pkg := &BasePkg{ptr: ptr, format: format}
runtime.SetFinalizer(pkg, func(self *BasePkg) { C.pkgcraft_pkg_free(self.ptr) })
return pkg
}
// Return an iterator over the packages of a repo.
func (self *BaseRepo) Iter() *repoIter[*BasePkg] {
return newRepoIter[*BasePkg](self)
}
// Return a channel iterating over the packages of a repo.
func (self *BaseRepo) Pkgs() <-chan *BasePkg {
return repoPkgs[*BasePkg](self)
}
// Return an iterator over the restricted packages of a repo.
func (self *BaseRepo) IterRestrict(restrict *Restrict) *repoIterRestrict[*BasePkg] {
return newRepoIterRestrict[*BasePkg](self, restrict)
}
// Return a channel iterating over the restricted packages of a repo.
func (self *BaseRepo) RestrictPkgs(restrict *Restrict) <-chan *BasePkg {
return repoRestrictPkgs[*BasePkg](self, restrict)
}
// Return true if a repo contains a given object, false otherwise.
func (self *BaseRepo) Contains(obj interface{}) bool {
switch obj := obj.(type) {
case string:
c_str := C.CString(obj)
defer C.free(unsafe.Pointer(c_str))
return bool(C.pkgcraft_repo_contains_path(self.ptr, c_str))
case *Restrict:
pkgs := self.RestrictPkgs(obj)
_, ok := <-pkgs
return ok
default:
if restrict, _ := NewRestrict(obj); restrict != nil {
return self.Contains(restrict)
}
return false
}
}
// Return a new repo from a given pointer.
func repoFromPtr(ptr *C.Repo) *BaseRepo {
format := RepoFormat(C.pkgcraft_repo_format(ptr))
return &BaseRepo{ptr, format}
}