/
subsets.go
170 lines (137 loc) · 4.33 KB
/
subsets.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright © 2021 Daniele Rondina <geaaru@sabayonlinux.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/>.
package artifact
import (
"fmt"
. "github.com/geaaru/luet/pkg/config"
"github.com/geaaru/luet/pkg/helpers"
. "github.com/geaaru/luet/pkg/logger"
pkg "github.com/geaaru/luet/pkg/package"
tarf_specs "github.com/geaaru/tar-formers/pkg/specs"
)
func (a *PackageArtifact) GetTarFormersSpec(enableSubsets bool) *tarf_specs.SpecFile {
spec := tarf_specs.NewSpecFile()
spec.SameOwner = LuetCfg.GetGeneral().SameOwner
spec.EnableMutex = LuetCfg.GetTarFlows().Mutex4Dirs
spec.MaxOpenFiles = LuetCfg.GetTarFlows().MaxOpenFiles
spec.BufferSize = LuetCfg.GetTarFlows().CopyBufferSize
spec.OverwritePerms = LuetCfg.GetGeneral().OverwriteDirPerms
spec.IgnoreRegexes = []string{
// prevent 'operation not permitted'
//"^/dev",
}
spec.IgnoreFiles = []string{}
if enableSubsets {
def := a.GetSubsets()
for k, v := range def.Definitions {
if !helpers.ContainsElem(&LuetCfg.Subsets.Enabled, k) {
// POST: the selected subset is not enabled.
// I add the rules as IgnoreRegexes.
for _, r := range v.Rules {
spec.IgnoreRegexes = append(spec.IgnoreRegexes, r)
}
Debug(fmt.Sprintf("[%s] Adding ignore regexes %s",
a.Runtime.HumanReadableString(), v.Rules))
}
}
}
return spec
}
func (a *PackageArtifact) GetSubsets() *LuetSubsetsDefinition {
ans := &LuetSubsetsDefinition{
Definitions: make(map[string]*LuetSubsetDefinition, 0),
}
if a.Runtime != nil {
// Get global/user category subsets defined
catSubsets := LuetCfg.SubsetsCatDefMap[a.Runtime.GetCategory()]
// Get global/user package subsets defined
pkgSubsets := LuetCfg.SubsetsPkgsDefMap[a.Runtime.PackageName()]
// Check if there is subsets definition on annotations
if a.Runtime.HasAnnotation(string(pkg.SubsetsAnnotation)) {
ans = a.unmarshalSubsets()
}
// Respect this order on override the subsets:
// If there are subsets on package annotation I set the
// initial definitions.
// If there is a pkg subsets defined locally i use them
// to add new subsets or override existing with the same keys.
// If there isn't a pkg subsets and there are subsets
// defined for the category i override the subsets at the
// same way.
if pkgSubsets != nil {
for k, v := range pkgSubsets.Definitions {
ans.Definitions[k] = v
}
} else if catSubsets != nil {
for k, v := range catSubsets.Definitions {
ans.Definitions[k] = v
}
}
}
return ans
}
func (a *PackageArtifact) unmarshalSubsets() *LuetSubsetsDefinition {
ans := &LuetSubsetsDefinition{
Definitions: make(map[string]*LuetSubsetDefinition, 0),
}
subsets := a.Runtime.GetAnnotationByKey(
string(pkg.SubsetsAnnotation),
)
obj, ok := subsets.(map[interface{}]interface{})
if !ok {
Warning(fmt.Sprintf("[%s] Wrong format on %s annotation.",
a.Runtime.HumanReadableString(), pkg.SubsetsAnnotation,
))
return ans
}
for k, v := range obj {
krules, kok := k.(string)
if !kok {
Warning(fmt.Sprintf("[%s] Invalid key on subset %s.",
a.Runtime.HumanReadableString(), k,
))
continue
}
if krules != "rules" {
continue
}
mrules, ok := v.(map[interface{}]interface{})
if !ok {
Warning(fmt.Sprintf("[%s] Wrong rules on subset %s.",
a.Runtime.HumanReadableString(), k,
))
continue
}
for mk, vrules := range mrules {
kk := mk.(string)
rules := []string{}
irules, ok := vrules.([]interface{})
if !ok {
Warning(fmt.Sprintf("[%s] For subset %s value is not an array.",
a.Runtime.HumanReadableString(), kk,
))
continue
}
for _, r := range irules {
rules = append(rules, r.(string))
}
ans.Definitions[kk] = &LuetSubsetDefinition{
Name: kk,
Rules: rules,
}
}
}
return ans
}