-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagfile.go
171 lines (158 loc) · 4.29 KB
/
tagfile.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
171
package bago
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"unicode/utf8"
)
type TagSet map[string][]string
type TagFile struct {
tags TagSet
labels []string
}
type bagitValues struct {
encoding string
version [2]int
}
// DefaultBagitTxt returns a new TagFile for bagit.txt
func DefaultBagitTxt() *TagFile {
tagFile := &TagFile{}
tagFile.Append(`BagIt-Version`, defaultVersion)
tagFile.Append(`Tag-File-Character-Encoding`, `UTF-8`)
return tagFile
}
func (tf *TagFile) init() {
if tf.tags == nil {
tf.tags = TagSet{}
}
}
func (tf *TagFile) Append(label string, value string) []string {
tf.init()
if _, ok := tf.tags[label]; !ok {
tf.tags[label] = []string{value}
tf.labels = append(tf.labels, label)
} else {
tf.tags[label] = append(tf.tags[label], value)
}
return tf.tags[label]
}
func (tf *TagFile) Set(label string, value string) {
tf.init()
if _, ok := tf.tags[label]; !ok {
tf.labels = append(tf.labels, label)
}
tf.tags[label] = []string{value}
}
func ParseTagFileLine(line string) (ret [2]string, err error) {
lineRe := regexp.MustCompile(`^([^\s:][^:]*):(.*)`)
match := lineRe.FindStringSubmatch(line)
if len(match) < 3 {
err = fmt.Errorf("tags should be set as 'tag-name: value'")
return
}
ret[0] = strings.Trim(match[1], ` `)
ret[1] = strings.Trim(match[2], ` `)
return
}
func (tf *TagFile) parse(reader io.Reader) error {
tf.tags, tf.labels = nil, nil
lineNum := 0
emptyLineRE := regexp.MustCompile(`^\s*$`)
contLineRE := regexp.MustCompile(`^\s+\S+`)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
lineNum++
line := scanner.Text()
if emptyLineRE.MatchString(line) {
continue // ignore empty lines
} else if contLineRE.MatchString(line) {
// continuation of previous label
l := len(tf.labels)
if l == 0 {
return fmt.Errorf("Syntax error at line: %d", lineNum)
}
prevLabel := tf.labels[l-1]
valIndx := len(tf.tags[prevLabel]) - 1
tf.tags[prevLabel][valIndx] += " " + strings.Trim(line, ` `)
} else {
// must be start of a new label/value pair.
keyVal, err := ParseTagFileLine(line)
if err != nil {
return fmt.Errorf("Syntax error on line %d: %s", lineNum, err.Error())
}
tf.Append(keyVal[0], keyVal[1])
}
}
return nil
}
// bagitTxtValues validates structure of TagFile from bagit.txt and returns
// tag values.
func (tf *TagFile) bagitTxtValues() (ret bagitValues, err error) {
labels := [...]string{`BagIt-Version`, `Tag-File-Character-Encoding`}
patterns := [...]*regexp.Regexp{
regexp.MustCompile(`(\d+)\.(\d+)`),
regexp.MustCompile(`^(\S+)`),
}
tmpVals := []string{}
if len(tf.labels) != len(labels) {
err = fmt.Errorf(`%s should have %s and %s`, bagitTxt, labels[0], labels[1])
return ret, err
}
for i, label := range tf.labels {
if label != labels[i] {
err = fmt.Errorf(`Expected %s in line %d of %s to`, label, i, bagitTxt)
return ret, err
}
vals, ok := tf.tags[label]
if !ok || len(vals) != 1 {
err = fmt.Errorf(`Expected 1 entry for %s in %s to`, label, bagitTxt)
return ret, err
}
matches := patterns[i].FindStringSubmatch(vals[0])
if len(matches) == 0 {
err = fmt.Errorf(`Bad value for %s in %s: %s`, label, bagitTxt, vals[0])
return ret, err
}
tmpVals = append(tmpVals, matches[1:]...)
}
if len(tmpVals) != 3 {
return ret, fmt.Errorf(`unexpected values parsing %s`, bagitTxt)
}
ret.version[0], _ = strconv.Atoi(tmpVals[0])
ret.version[1], _ = strconv.Atoi(tmpVals[1])
ret.encoding = tmpVals[2]
return ret, nil
}
func (tf *TagFile) Write(writer io.Writer) error {
for _, label := range tf.labels {
for _, val := range tf.tags[label] {
if _, err := fmt.Fprintf(writer, "%s:", label); err != nil {
return err
}
runesOnLine := utf8.RuneCountInString(label) + 1
scanner := bufio.NewScanner(strings.NewReader(val))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Text()
len := utf8.RuneCountInString(word)
prefix := ``
if (runesOnLine + len) < 79 {
runesOnLine += (len + 1) // continue on same line
} else {
prefix = "\n " // new line: "\n word"
runesOnLine = len + 2 //
}
if _, err := fmt.Fprintf(writer, "%s %s", prefix, word); err != nil {
return err
}
}
if _, err := io.WriteString(writer, "\n"); err != nil {
return err
}
}
}
return nil
}