nix-amer/internal/buildfile/reader.go

50 lines
936 B
Go
Raw Normal View History

package buildfile
import (
"bufio"
2018-11-09 21:37:05 +00:00
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
"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
}
// 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-09 21:37:05 +00:00
// add each line as instruction
l, reader := 0, bufio.NewReader(buildfile)
for {
l++
2018-11-09 21:37:05 +00:00
// read line until end
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)
if err != nil {
return nil, LineError{l, err}
}
2018-11-09 21:37:05 +00:00
// add to list
r.Content = append(r.Content, inst)
}
return r, nil
}