-
Notifications
You must be signed in to change notification settings - Fork 2
/
specfile.go
70 lines (63 loc) · 1.59 KB
/
specfile.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
package specfile
import (
"errors"
"reflect"
)
// Specfile specfile struct
type Specfile struct {
Subpackages []Specfile
Tags []Tag
Macros Macros
Sections []Section
Dependencies []Dependency
}
// FindDepedencies find a kind of dependency in specfile
func (s Specfile) FindDependencies(typ string) []string {
var dependencies []string
for _, d := range s.Dependencies {
if d.Name == typ {
dependencies = append(dependencies, d.Value)
}
}
return dependencies
}
// FindTag find a tag in specfile
func (s Specfile) FindTag(name string) (Tag, error) {
for _, t := range s.Tags {
if t.Name == name {
return t, nil
}
}
return Tag{}, errors.New("tag not found")
}
// FindSection find a section in specfile
func (s Specfile) FindSection(name string) (Section, error) {
for _, section := range s.Sections {
if section.Name == name {
return section, nil
}
}
return Section{}, errors.New("section not found")
}
// FindSubpackage find a subpackage in specfile
func (s Specfile) FindSubpackage(name string) (Specfile, error) {
for _, spec := range s.Subpackages {
if tag, err := spec.FindTag("Name"); err == nil {
if tag.Value == name {
return spec, nil
}
}
}
return Specfile{}, errors.New("specfile not found")
}
// append append value to fields
func (s *Specfile) append(field string, val interface{}) {
sv := reflect.ValueOf(s).Elem().FieldByName(field)
if sv.Len() > 0 {
sv.Set(reflect.Append(sv, reflect.ValueOf(val)))
} else {
nv := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(val)), 1, 1)
nv.Index(0).Set(reflect.ValueOf(val))
sv.Set(nv)
}
}