package buildfile import ( "bufio" "errors" "git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction" "io" "strings" ) var ErrNullContext = errors.New("null context") // Reader is the buildfile reader type Reader struct { // Context is the linux distribution-specified execution context (package manager, service manager, etc) Context *instruction.ExecutionContext // Content is the instruction list Content []instruction.T } // NewReader creates a new reader for the specified build file and linux distribution func NewReader(ctx *instruction.ExecutionContext, buildfile io.Reader) (*Reader, error) { // fail on null context if ctx == nil { return nil, ErrNullContext } r := &Reader{ Context: ctx, Content: make([]instruction.T, 0), } // add each line as instruction l, reader := 0, bufio.NewReader(buildfile) for { l++ // read line until end line, err := reader.ReadString('\n') if err == io.EOF { break } else if err != nil { return nil, LineError{l, err} } line = strings.Trim(line, " \t\r\n") // ignore newline & comments if len(line) < 1 || line[0] == '[' { continue } // turn into instruction inst, err := instruction.Parse(line) if err != nil { return nil, LineError{l, err} } // add to list r.Content = append(r.Content, inst) } return r, nil }