forked from tomnomnom/anew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.45 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
)
func main() {
var quietMode bool
var dryRun bool
var trim bool
flag.BoolVar(&quietMode, "q", false, "quiet mode (no output at all)")
flag.BoolVar(&dryRun, "d", false, "don't append anything to the file, just print the new lines to stdout")
flag.BoolVar(&trim, "t", false, "trim leading and trailing whitespace before comparison")
flag.Parse()
fn := flag.Arg(0)
lines := make(map[string]bool)
var f io.WriteCloser
if fn != "" {
// read the whole file into a map if it exists
r, err := os.Open(fn)
if err == nil {
sc := bufio.NewScanner(r)
for sc.Scan() {
if trim {
lines[strings.TrimSpace(sc.Text())] = true
} else {
lines[sc.Text()] = true
}
}
r.Close()
}
if !dryRun {
// re-open the file for appending new stuff
f, err = os.OpenFile(fn, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open file for writing: %s\n", err)
return
}
defer f.Close()
}
}
// read the lines, append and output them if they're new
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
line := sc.Text()
if trim {
line = strings.TrimSpace(line)
}
if lines[line] {
continue
}
// add the line to the map so we don't get any duplicates from stdin
lines[line] = true
if !quietMode {
fmt.Println(line)
}
if !dryRun {
if fn != "" {
fmt.Fprintf(f, "%s\n", line)
}
}
}
}