nix-amer/internal/buildfile/reader.go

48 lines
876 B
Go
Raw Normal View History

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")
// get and add instruction to list
inst, err := createInstruction(line)
if err != nil {
return nil, LineError{l, err}
}
r.Content = append(r.Content, inst)
}
return r, nil
}