File size: 1,282 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

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

func isAlphaNum(r byte) bool {
	switch {
	case r >= 'a' && r <= 'z':
	case r >= 'A' && r <= 'Z':
	case r >= '0' && r <= '9':
	case r == '_':
	default:
		return false
	}
	return true
}

func Parse(r io.Reader) ([]Task, error) {
	s := bufio.NewScanner(r)
	var tasks []Task
	var t Task
	var line int
	for s.Scan() {
		line++
		str := strings.TrimSpace(s.Text())
		if str == "" {
			t = Task{}
			continue
		}
		if str[0] == '@' {
			// parameter
			parts := strings.SplitN(str[1:], "=", 2)
			switch strings.TrimSpace(parts[0]) {
			case "oneshot":
				t.OneShot = true
			case "watch-file":
				if len(parts) != 2 {
					return nil, fmt.Errorf("line %d: missing file path for watch-file", line)
				}
				t.WatchFiles = append(t.WatchFiles, parts[1])
			default:
				return nil, fmt.Errorf("line %d: invalid option '%s'", line, parts[0])
			}
		}
		if !isAlphaNum(str[0]) {
			// comment/ignored
			continue
		}

		parts := strings.SplitN(str, ":", 2)
		if len(parts) != 2 {
			return nil, fmt.Errorf("line %d: invalid proc definition '%s' (missing ':')", line, parts[0])
		}
		t.Name = strings.TrimSpace(parts[0])
		t.Command = strings.TrimSpace(parts[1])
		tasks = append(tasks, t)
		t = Task{}
	}
	return tasks, s.Err()
}