-
Notifications
You must be signed in to change notification settings - Fork 1
/
hcat.go
48 lines (37 loc) · 858 Bytes
/
hcat.go
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
package util
import (
"log"
"path"
"strings"
)
var plainTextHeading = "##"
var heading = "^" + plainTextHeading
func ensureNewline(s string) string {
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
return s
}
// given a list of text files cats them together into a single stream of text
// separated by headings
// given file1.md that contains "hello"
// and given file2.md that contains "ok"
// output:
// "## file1.md
// hello
// ## file2.md
// ok"
// # ls $somedir | sort | hcat > $somefile
// Hcat ...
func Hcat(filenames []string, dir string) string {
var text string
for _, filename := range filenames {
content, err := readFile(path.Join(dir, filename))
if err != nil {
log.Fatalf("failed to Hcat: %s", err)
}
text += ensureNewline(plainTextHeading + " " + filename)
text += ensureNewline(content)
}
return text
}