forked from rafecolton/docker-builder
/
parser.go
69 lines (60 loc) · 1.82 KB
/
parser.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 parser
import (
"github.com/modcloth/docker-builder/parser/uuid"
)
import (
"path/filepath"
"github.com/Sirupsen/logrus"
)
/*
Parser is a struct that contains a Builderfile and knows how to parse it both
as raw text and to convert toml to a Builderfile struct. It also knows how to
tell if the Builderfile is valid (openable) or nat.
*/
type Parser struct {
filename string
*logrus.Logger
uuidGenerator uuid.UUIDGenerator
top string
}
/*
NewParser returns an initialized Parser. Not currently necessary, as no
default values are assigned to a new Parser, but useful to have in case we need
to change this.
*/
func NewParser(filename string, logger *logrus.Logger) (*Parser, error) {
return &Parser{
Logger: logger,
filename: filename,
uuidGenerator: uuid.NewUUIDGenerator(true),
top: filepath.Dir(filename),
}, nil
}
/*
NextUUID returns the next UUID generated by the parser's uuid generator. This
will either be a random uuid (normal behavior) or the same uuid every time if
the generator is "seeded" (used for tests)
*/
func (parser *Parser) NextUUID() (string, error) {
ret, err := parser.uuidGenerator.NextUUID()
if err != nil {
return "", err
}
return ret, nil
}
/*
SeedUUIDGenerator turns this parser's uuidGenerator into a seeded generator.
All calls to NextUUID() will produce the same uuid after this function is
called and until RandomizeUUIDGenerator() is called.
*/
func (parser *Parser) SeedUUIDGenerator() {
parser.uuidGenerator = uuid.NewUUIDGenerator(false)
}
/*
RandomizeUUIDGenerator turns this parser's uuidGenerator into a random
generator. All calls to NextUUID() will produce a random uuid after this
function is called and until SeedUUIDGenerator() is called.
*/
func (parser *Parser) RandomizeUUIDGenerator() {
parser.uuidGenerator = uuid.NewUUIDGenerator(true)
}