• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyberShadow / aconfmgr / 470

26 Mar 2024 06:13PM UTC coverage: 80.669% (-13.0%) from 93.672%
470

push

github

CyberShadow-Renovate
Update dependency paru to v20240326042315

0 of 1 new or added line in 1 file covered. (0.0%)

464 existing lines in 23 files now uncovered.

3209 of 3978 relevant lines covered (80.67%)

140.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.09
/src/helpers.bash
1
# helpers.bash
2

3
# This file contains helper functions for the generated configuration scripts.
4

5
#
6
# AddPackage [--foreign] PACKAGE...
7
#
8
# Adds a package to the list of packages to be installed.
9
#
10

11
function AddPackage() {
12
        local fn='packages.txt'
50✔
13
        if [[ "$1" == "--foreign" ]]
50✔
14
        then
15
                shift
1✔
16
                fn='foreign-packages.txt'
1✔
17
        fi
18

19
        printf '%q\n' "$@" >> "$output_dir"/"$fn"
50✔
20
}
21

22
#
23
# AddPackageGroup GROUP
24
#
25
# Adds all packages belonging to a group to the list of packages to be installed.
26
#
27

28
function AddPackageGroup() {
29
        local group=$1
1✔
30

31
        pacman --sync --quiet --groups "$group" >> "$output_dir"/packages.txt
1✔
32
}
33

34
#
35
# RemovePackage [--foreign] PACKAGE...
36
#
37
# Removes an earlier-added package to the list of packages to be installed.
38
#
39
# Emitted by `aconfmgr save` when a package is present in the configuration,
40
# but absent on the system.
41
#
42
# You should refactor out any occurrences of this function from your configuration.
43
#
44

45
function RemovePackage() {
46
        local fn='packages.txt'
6✔
47
        if [[ "$1" == "--foreign" ]]
6✔
48
        then
UNCOV
49
                shift
×
UNCOV
50
                fn='foreign-packages.txt'
×
51
        fi
52

53
        local package
6✔
54
        for package in "$@"
6✔
55
        do
56
                sed -i "$output_dir"/"$fn" -e "/^${package}\$/d"
6✔
57
        done
58
}
59

60
#
61
# IgnorePackage [--foreign] PACKAGE...
62
#
63
# Adds a package to the list of packages to be ignored.
64
#
65

66
function IgnorePackage() {
67
        if [[ "$1" == "--foreign" ]]
52✔
68
        then
69
                shift
24✔
70
                ignore_foreign_packages+=("$@")
24✔
71
        else
72
                ignore_packages+=("$@")
28✔
73
        fi
74
}
75

76
#
77
# CopyFile PATH [MODE [OWNER [GROUP]]]
78
#
79
# Copies a file from the "files" subdirectory to the output.
80
#
81
# The specified path should be relative to the root of the "files" subdirectory.
82
#
83
# If MODE, OWNER and GROUP are blank or unspecified, they default to
84
# "644", "root" and "root" respectively for new files.
85
# Values corresponding to the above defaults must be specified
86
# as an empty string ('').
87
#
88

89
function CopyFile() {
90
        local file="$1"
3✔
91
        local mode="${2:-}"
3✔
92
        local owner="${3:-}"
3✔
93
        local group="${4:-}"
3✔
94

95
        CopyFileTo "$file" "$file" "$mode" "$owner" "$group"
3✔
96
}
97

98
#
99
# CopyFileTo SRC-PATH DST-PATH [MODE [OWNER [GROUP]]]
100
#
101
# Copies a file from the "files" subdirectory to the output,
102
# under a different name or path.
103
#
104
# The source path should be relative to the root of the "files" subdirectory.
105
# The destination path is relative to the root of the output directory.
106
#
107

108
function CopyFileTo() {
109
        local src_file="$1"
4✔
110
        local dst_file="$2"
4✔
111
        local mode="${3:-}"
4✔
112
        local owner="${4:-}"
4✔
113
        local group="${5:-}"
4✔
114

115
        if [[ "$src_file" != /* ]]
4✔
116
        then
117
                ConfigWarning 'Source file path %s is not absolute.\n' \
2✔
118
                                          "$(Color C "%q" "$src_file")"
2✔
119
        fi
120

121
        if [[ "$dst_file" != /* && "$dst_file" != "$src_file" ]]
5✔
122
        then
123
                ConfigWarning 'Target file path %s is not absolute.\n' \
2✔
124
                              "$(Color C "%q" "$dst_file")"
2✔
125
        fi
126

127
        mkdir --parents "$(dirname "$output_dir"/files/"$dst_file")"
8✔
128

UNCOV
129
        cp --no-dereference\
×
130
           "$config_dir"/files/"$src_file"\
4✔
131
           "$output_dir"/files/"$dst_file"
4✔
132

133
        SetFileProperty "$dst_file" mode  "$mode"
4✔
134
        SetFileProperty "$dst_file" owner "$owner"
4✔
135
        SetFileProperty "$dst_file" group "$group"
4✔
136

137
        used_files["$src_file"]=y
4✔
138
}
139

140
#
141
# CreateFile [--no-clobber] PATH [MODE [OWNER [GROUP]]]
142
#
143
# Creates an empty file, to be included in the output.
144
# Prints its absolute path to standard output.
145
#
146
# Avoids overwriting any pre-existing output file if
147
# --no-clobber is provided.
148
#
149

150
function CreateFile() {
151
        local keep=false
33✔
152
        if [[ "$1" == "--no-clobber" ]]
33✔
153
        then
154
                keep=true
×
155
                shift
×
156
        fi
157

158
        local file="$1"
33✔
159
        local mode="${2:-}"
33✔
160
        local owner="${3:-}"
33✔
161
        local group="${4:-}"
33✔
162

163
        local output_file="$output_dir"/files/"$file"
33✔
164

165
        mkdir --parents "$(dirname "$output_file")"
66✔
166

167
        if [[ -h "$output_file" || -e "$output_file" ]]
66✔
168
        then
169
                if $keep
×
170
                then
171
                        printf '%s' "$output_file"
×
172
                        return
×
173
                else
174
                        ConfigWarning 'Overwriting %s, which was created earlier in the configuration. Use %s to keep old contents, or silence this warning by calling %s first.\n' \
×
175
                                                  "$(Color C "%q" "$file")" \
×
176
                                                  "$(Color Y "CreateFile --no-clobber")" \
×
177
                                                  "$(Color Y "RemoveFile")"
×
178
                fi
179
        fi
180

181
        truncate --size 0 "$output_file"
33✔
182

183
        SetFileProperty "$file" mode  "$mode"
33✔
184
        SetFileProperty "$file" owner "$owner"
33✔
185
        SetFileProperty "$file" group "$group"
33✔
186

187
        printf '%s' "$output_file"
33✔
188
}
189

190
#
191
# GetPackageOriginalFile [--no-clobber] PACKAGE PATH
192
#
193
# Extracts the original file from a package's archive for inclusion in the output.
194
# Prints its absolute path to standard output.
195
#
196
# As in the case of CreateFile, the file can be further modified after extraction.
197
#
198
# Avoids overwriting any pre-existing output file if
199
# --no-clobber is provided.
200
#
201

202
function GetPackageOriginalFile() {
203
        local keep=false
5✔
204
        if [[ "$1" == "--no-clobber" ]]
5✔
205
        then
206
                keep=true
2✔
207
                shift
2✔
208
        fi
209

210
        local package="$1" # Package to extract the file from
5✔
211
        local file="$2" # Absolute path to file in package
5✔
212

213
        local output_file="$output_dir"/files/"$file"
5✔
214

215
        mkdir --parents "$(dirname "$output_file")"
10✔
216

217
        if [[ -h "$output_file" || -e "$output_file" ]]
10✔
218
        then
219
                if $keep
2✔
220
                then
221
                        printf '%s' "$output_file"
1✔
222
                        return
1✔
223
                else
224
                        ConfigWarning 'Overwriting %s, which was created earlier in the configuration. Use %s to keep old contents, or silence this warning by calling %s first.\n' \
4✔
225
                                                  "$(Color C "%q" "$file")" \
4✔
226
                                                  "$(Color Y "GetPackageOriginalFile --no-clobber")" \
4✔
227
                                                  "$(Color Y "RemoveFile")"
4✔
228
                fi
229
        fi
230

231
        AconfGetPackageOriginalFile        "$package" "$file" > "$output_file"
4✔
232

233
        printf '%s' "$output_file"
4✔
234
}
235

236
#
237
# CreateLink PATH TARGET [OWNER [GROUP]]
238
#
239
# Creates a symbolic link with the specified target.
240
#
241

242
function CreateLink() {
243
        local file="$1"
15✔
244
        local target="$2"
15✔
245
        local owner="${3:-}"
15✔
246
        local group="${4:-}"
15✔
247

248
        mkdir --parents "$(dirname "$output_dir"/files/"$file")"
30✔
249

250
        ln --symbolic -- "$target" "$output_dir"/files/"$file"
15✔
251

252
        SetFileProperty "$file" owner "$owner"
15✔
253
        SetFileProperty "$file" group "$group"
15✔
254
}
255

256
#
257
# CreateDir PATH [MODE [OWNER [GROUP]]]
258
#
259
# Creates an empty directory at the specified path.
260
#
261
# Normally calling this function is not necessary, as creating files
262
# will implicitly create all parent directories. Use this function
263
# only when you need to create an empty directory without any files in
264
# it.
265
#
266

267
function CreateDir() {
268
        local file="$1"
11✔
269
        local mode="${2:-}"
11✔
270
        local owner="${3:-}"
11✔
271
        local group="${4:-}"
11✔
272

273
        mkdir --parents "$output_dir"/files/"$file"
11✔
274

275
        SetFileProperty "$file" mode  "$mode"
11✔
276
        SetFileProperty "$file" owner "$owner"
11✔
277
        SetFileProperty "$file" group "$group"
11✔
278
}
279

280
#
281
# RemoveFile PATH
282
#
283
# Removes an earlier-added file.
284
#
285
# Emitted by `aconfmgr save` when a file is present in the configuration,
286
# but absent (or, in case of files owned by packages, unmodified) on the system.
287
#
288
# You should refactor out any occurrences of this function from your configuration.
289
#
290
# If you want to delete a file owned by a package, instead use:
291
# SetFileProperty /path/to/file deleted y
292
#
293

294
function RemoveFile() {
295
        local file="$1"
9✔
296

297
        rm --dir "$output_dir"/files/"$file"
9✔
298
}
299

300
#
301
# SetFileProperty PATH TYPE VALUE
302
#
303
# Sets a file property.
304
# TYPE can be "owner", "group" "mode", or "deleted".
305
#
306
# Set "deleted" to "y" to mark a file owned by some package for deletion.
307
#
308
# To reset a file property to its default value,
309
# specify an empty string ('') for the VALUE parameter.
310
#
311

312
function SetFileProperty() {
313
        local file="$1"
192✔
314
        local type="$2"
192✔
315
        local value="$3"
192✔
316

317
        printf '%s\t%s\t%q\n' "$type" "$value" "$file" >> "$output_dir"/file-props.txt
192✔
318
}
319

320
#
321
# IgnorePath PATH
322
#
323
# Adds the specified path to the list of ignored paths.
324
#
325
# The argument should be a shell pattern, e.g. '/etc/foo/*'.
326
#
327

328
function IgnorePath() {
329
        ignore_paths+=("$@")
12✔
330
}
331

332
#
333
# AddFileContentFilter PATTERN FUNCTION
334
#
335
# Adds a filter for paths matching the given PATTERN (bash syntax).
336
#
337
# The function is called with the file name as the only parameter, the
338
# file contents on its stdin, and is expected to provide the filtered
339
# contents on its stdout.
340
#
341
# Only one function can be configured per unique PATTERN.  The most
342
# recently added PATTERN takes precedence over any matching patterns
343
# preceding it.
344
#
345

346
function AddFileContentFilter() {
347
        local pattern=$1
3✔
348
        local function=$2
3✔
349

350
        file_content_filters[$pattern]=$function
3✔
351
}
352

353
: # include in coverage
88✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc