2018-11-07 21:13:47 +00:00
|
|
|
package buildfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-11-09 21:37:05 +00:00
|
|
|
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
|
2018-11-07 21:13:47 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reader is the buildfile reader
|
|
|
|
type Reader struct {
|
|
|
|
// linux distribution to run on
|
|
|
|
distribution string
|
2018-11-09 21:37:05 +00:00
|
|
|
Content []instruction.T
|
2018-11-07 21:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2018-11-09 21:37:05 +00:00
|
|
|
Content: make([]instruction.T, 0),
|
2018-11-07 21:13:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 21:37:05 +00:00
|
|
|
// add each line as instruction
|
2018-11-07 21:13:47 +00:00
|
|
|
l, reader := 0, bufio.NewReader(buildfile)
|
|
|
|
for {
|
|
|
|
l++
|
|
|
|
|
2018-11-09 21:37:05 +00:00
|
|
|
// read line until end
|
2018-11-07 21:13:47 +00:00
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, LineError{l, err}
|
|
|
|
}
|
|
|
|
|
2018-11-09 21:37:05 +00:00
|
|
|
// turn into instruction
|
|
|
|
inst, err := instruction.Parse(line)
|
2018-11-07 21:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, LineError{l, err}
|
2018-11-07 21:13:47 +00:00
|
|
|
}
|
2018-11-09 21:37:05 +00:00
|
|
|
|
|
|
|
// add to list
|
2018-11-07 21:13:47 +00:00
|
|
|
r.Content = append(r.Content, inst)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|