diff --git a/goblin.go b/goblin.go index c380f3e..71ee4fb 100644 --- a/goblin.go +++ b/goblin.go @@ -9,6 +9,7 @@ import( "github.com/jienfak/goblin/mkdir" "github.com/jienfak/goblin/gtrue" "github.com/jienfak/goblin/gfalse" + "github.com/jienfak/goblin/sort" ) func main() { @@ -24,6 +25,7 @@ func main() { "echo" : echo.Run, "true" : gtrue.Run, "false" : gfalse.Run, + "sort" : sort.Run, } if binBase := path.Base(os.Args[0]) ; binBase != "goblin" { diff --git a/sort/sort.go b/sort/sort.go new file mode 100644 index 0000000..7cd8b26 --- /dev/null +++ b/sort/sort.go @@ -0,0 +1,37 @@ +package sort + +import ( + "bufio" + "fmt" + "os" + "sort" + "io" + "flag" +) + +func readLines() []string { + + r := bufio.NewReader(os.Stdin) + a := make([]string, 0) + for { + l, e := r.ReadString('\n') + if e==io.EOF { + break + } + a = append(a, l) + } + return a +} + +func Run(args []string) int { + flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) + flagSet.Parse(args[1:]) + status := 0 + + lines := readLines() + sort.Strings(lines) + for _, l := range lines { + fmt.Print(l) + } + return status +}