-
Notifications
You must be signed in to change notification settings - Fork 113
/
parse.go
97 lines (77 loc) · 2.07 KB
/
parse.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
/*
Copyright 2020 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package libmount
import (
"errors"
"strings"
)
var (
ErrInvalidToken error = errors.New("invalid token error")
)
type Parser interface {
Parse(string) (*Filesystem, error)
}
type mountInfoParser struct{}
type mountsParser struct{}
func NewMountsParser() Parser {
return mountsParser{}
}
func NewMountInfoParser() Parser {
return mountInfoParser{}
}
func NewParser(format MountTabFormat) Parser {
switch format {
case MntFmtMountInfo:
return NewMountInfoParser()
default:
return NewMountsParser()
}
}
// parse one line of mountinfo file
func (p mountInfoParser) Parse(line string) (*Filesystem, error) {
return nil, errors.New("not implemented")
}
// parse one line of mounts/{fs, m}tab file
func (p mountsParser) Parse(line string) (*Filesystem, error) {
fs := NewFilesystem()
tokens := strings.Fields(line)
// [1] source
p.parseSourceToken(fs, tokens[0])
// [2] target
fs.SetTarget(tokens[1])
// [3] FS type
fs.SetFsType(tokens[2])
// TODO: parse further columns
return fs, nil
}
func (p mountsParser) parseSourceToken(fs *Filesystem, token string) {
tag, val, err := parseTagString(token)
if err != nil && isValidTagName(tag) {
fs.SetTag(tag, val)
}
fs.SetSource(token)
}
func parseTagString(token string) (string, string, error) {
tv := strings.SplitN(token, "=", 2)
if len(tv) < 2 {
return "", "", ErrInvalidToken
}
return tv[0], tv[1], nil
}
func isValidTagName(tag string) bool {
return (tag == "ID" ||
tag == "UUID" ||
tag == "LABEL" ||
tag == "PARTUUID" ||
tag == "PARTLABEL")
}