2018-11-07 21:13:47 +00:00
|
|
|
package buildfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reader is the buildfile reader
|
|
|
|
type Reader struct {
|
|
|
|
// linux distribution to run on
|
|
|
|
distribution string
|
|
|
|
Content []*instruction
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader creates a new reader for the specified build file and linux distribution
|
|
|
|
func NewReader(distro string, buildfile io.Reader) (*Reader, error) {
|
|
|
|
|
|
|
|
r := &Reader{
|
|
|
|
distribution: distro,
|
|
|
|
Content: make([]*instruction, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
// read each line
|
|
|
|
l, reader := 0, bufio.NewReader(buildfile)
|
|
|
|
for {
|
|
|
|
l++
|
|
|
|
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, LineError{l, err}
|
|
|
|
}
|
|
|
|
line = strings.Trim(line, " \t\n")
|
|
|
|
|
2018-11-07 21:54:02 +00:00
|
|
|
// get and add instruction to list
|
|
|
|
inst, err := createInstruction(line)
|
|
|
|
if err != nil {
|
|
|
|
return nil, LineError{l, err}
|
2018-11-07 21:13:47 +00:00
|
|
|
}
|
|
|
|
r.Content = append(r.Content, inst)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|