small pixel drawing of a pufferfish enby

config/config.go

package config

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

// Block is a list of directives.
type Block []*Binary

type Fetch struct {
	Url string
	Every string
}

// Binary is a configuration directive.
type Binary struct {
	Name   string
	Fetch Fetch
	Build string
	Args []string
}

// Load loads a configuration file.
func Load(path string) (Block, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	return Read(f)
}

// Read parses a configuration file from an io.Reader.
func Read(r io.Reader) (Block, error) {
	scanner := bufio.NewScanner(r)

	block, closingBrace, err := readBlock(scanner)
	if err != nil {
		return nil, err
	} else if closingBrace {
		return nil, fmt.Errorf("unexpected '}'")
	}

	return block, scanner.Err()
}

// readBlock reads a block. closingBrace is true if parsing stopped on '}'
// (otherwise, it stopped on Scanner.Scan).
func readBlock(scanner *bufio.Scanner) (block Block, closingBrace bool, err error) {
	for scanner.Scan() {
		l := scanner.Text()
		words := strings.Split(l, " ")
		if err != nil {
			return nil, false, fmt.Errorf("failed to parse configuration file: %v", err)
		}

		if len(words) == 0 {
			continue
		}

		if len(words) == 1 && l == "" {
			continue
		}

		if len(words) == 1 && l[len(l)-1] == '}' {
			closingBrace = true
			break
		}

		if words[0] == "#" {
			continue
		}

		var fetch Fetch
		if strings.Contains(words[0], "fetch") {
			fmt.Println("fetchin")
			fetch.Url = words[1]
			fetch.Every = words[3]
		}

		var d *Binary
		if words[len(words)-1] == "{" && l[len(l)-1] == '{' {
			name := words[0]

			childBlock, childClosingBrace, err := readBlock(scanner)
			if err != nil {
				return nil, false, err
			} else if !childClosingBrace {
				return nil, false, io.ErrUnexpectedEOF
			}

			// Allows callers to tell apart "no block" and "empty block"
			if childBlock == nil {
				childBlock = Block{}
			}

			d = &Binary{Name: name}
		} else {
			d = &Binary{Name: words[0], Fetch: fetch}
			fmt.Printf("d: %v\n", d)
		}
		block = append(block, d)
		fmt.Printf("block: %v\n", block)
	}

	return block, closingBrace, nil
}