summaryrefslogtreecommitdiff
path: root/describe/describe.go
blob: fe168ec8946b59aa7a361dad235a7ecf12c60068 (plain)
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
package describe

import (
	"bufio"
	"os"
	"strings"
)

var DescriptionPrefixes = []string{
	"#",
	"//",
}

func Describe(file string) (string, error) {
	fd, err := os.Open(file)
	if err != nil {
		return "", err
	}
	defer fd.Close()
	scanner := bufio.NewScanner(fd)
	scanner.Split(bufio.ScanLines)
	scanner.Scan()
	text := scanner.Text()
	if strings.HasPrefix(text, "#!") {
		scanner.Scan()
		text = scanner.Text()
	}
	text = strings.TrimSpace(text)
	if !strings.HasPrefix(text, "#") {
		return "", nil
	}
	return text, nil
}