-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
101 lines (94 loc) · 2.68 KB
/
print.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
//This file is part of xtagger. ©2023 Jörg Walter.
//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 3 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 <https://www.gnu.org/licenses/>.
package program
import (
"fmt"
"github.com/jwdev42/xtagger/internal/cli"
"github.com/jwdev42/xtagger/internal/record"
"github.com/jwdev42/xtagger/internal/softerrors"
"io/fs"
"os"
"path/filepath"
)
func printFile(parent string, info fs.FileInfo) error {
print := func(attr record.Attribute, path string) error {
if commandLine.FlagPrint0() {
_, err := printMe.Print0(path)
return err
}
if commandLine.FlagPrintRecords() {
_, err := attr.FprintRecordsWithPath(os.Stdout, path)
return err
}
_, err := fmt.Printf("%s\n", path)
return err
}
path := filepath.Join(parent, info.Name())
constraint := commandLine.PrintConstraint()
//Open file
f, err := os.Open(path)
if err != nil {
return softerrors.Consume(err)
}
defer f.Close()
//Load Attributes
attr, err := record.FLoadAttribute(f)
if err != nil {
return softerrors.Consume(err)
}
//Filter Attributes by name
if names := commandLine.Names(); names != nil {
attr = attr.FilterByName(names...)
}
if len(attr) < 1 {
switch constraint {
case cli.PrintConstraintUntagged:
//Print recordless file if PrintConstraintUntagged is set
return softerrors.Consume(print(attr, path))
}
//Skip file otherwise
return nil
}
switch constraint {
case cli.PrintConstraintNone:
return softerrors.Consume(print(attr, path)) //Print tagged file if no constraint is set
case cli.PrintConstraintUntagged:
return nil //Skip tagged file
}
//Iterate through Attributes to check for invalid and valid records
var hasInvalidEntry bool
var hasValidEntry bool
for _, rec := range attr {
if !rec.Valid {
hasInvalidEntry = true
} else {
hasValidEntry = true
}
}
switch constraint {
case cli.PrintConstraintInvalid:
//Print if all records are invalid
if !hasValidEntry {
return softerrors.Consume(print(attr, path))
}
case cli.PrintConstraintValid:
//Print if all records are valid
if !hasInvalidEntry {
return softerrors.Consume(print(attr, path))
}
default:
panic("You're not supposed to be here")
}
return nil
}