From 9f38d66857e33996489e2b25781047b2cb75b8a1 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 12 Nov 2015 11:50:24 +0000 Subject: [PATCH] misc/sortac: add tool to sort the AUTHORS and CONTRIBUTORS files I keep losing this utility, used as part of other tools to auto-update the AUTHORS and CONTRIBUTORS files. Check it in to the repo so I don't lose it, and so others can use it as well. Updates #12042 Change-Id: Ib5886b85799087aaaddcec4c81169e2726322c05 Reviewed-on: https://go-review.googlesource.com/16824 Reviewed-by: Russ Cox --- misc/sortac/sortac.go | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 misc/sortac/sortac.go diff --git a/misc/sortac/sortac.go b/misc/sortac/sortac.go new file mode 100644 index 0000000000..f61aa9617e --- /dev/null +++ b/misc/sortac/sortac.go @@ -0,0 +1,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() +} -- 2.48.1