/
sortac.go
79 lines (72 loc) · 1.41 KB
/
sortac.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
79
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Sortac sorts the AUTHORS and CONTRIBUTORS files.
//
// Usage:
//
// sortac [file...]
//
// Sortac sorts the named files in place.
// If given no arguments, it sorts standard input to standard output.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
func main() {
log.SetFlags(0)
log.SetPrefix("sortac: ")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
os.Stdout.Write(sortAC(os.Stdin))
} else {
for _, arg := range args {
f, err := os.Open(arg)
if err != nil {
log.Fatal(err)
}
sorted := sortAC(f)
f.Close()
if err := ioutil.WriteFile(arg, sorted, 0644); err != nil {
log.Fatal(err)
}
}
}
}
func sortAC(r io.Reader) []byte {
bs := bufio.NewScanner(r)
var header []string
var lines []string
for bs.Scan() {
t := bs.Text()
lines = append(lines, t)
if t == "# Please keep the list sorted." {
header = lines
lines = nil
continue
}
}
if err := bs.Err(); err != nil {
log.Fatal(err)
}
var out bytes.Buffer
c := collate.New(language.Und, collate.Loose)
c.SortStrings(lines)
for _, l := range header {
fmt.Fprintln(&out, l)
}
for _, l := range lines {
fmt.Fprintln(&out, l)
}
return out.Bytes()
}