-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.go
69 lines (53 loc) · 1.47 KB
/
dataset.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Dataset struct {
name string
filters []string
globalFilters *[]string
}
// WriteTmpFilterFile writes filters of Dataset instance to temporary file and return its path.
func (ds *Dataset) WriteTmpFilterFile() string {
// Create temp filter list
var filters []string
// Global filters do not have to be prefixed. Just append them to the beginning of filter list
filters = append(filters, *ds.globalFilters...)
if len(ds.filters) == 0 {
// Default: No filters -> include everything from dataset directory
filters = append(filters, fmt.Sprintf("+ %s/**", ds.name))
} else {
// Prefix dataset-specific filters with dataset name
var flag, filter string
for _, f := range ds.filters {
flag = string(f[0])
filter = f[2:]
filters = append(filters, fmt.Sprintf("%s %s/%s", flag, ds.name, filter))
}
}
// Finally add "exclude rest"
filters = append(filters, "- **")
// Open temporary file for writing
f, err := os.CreateTemp("", "dm_*.txt")
checkError(err)
// Write list of filters to file
w := bufio.NewWriter(f)
nBytes, err := w.WriteString(strings.Join(filters, "\n"))
checkError(err)
err = w.Flush()
checkError(err)
DebugLogger.Printf("%d bytes written to `%s`", nBytes, f.Name())
return f.Name()
}
func getDatasetNames(datasets map[string]*Dataset) []string {
names := make([]string, len(datasets))
i := 0
for n := range datasets {
names[i] = n
i++
}
return names
}