diff --git a/goblin.go b/goblin.go index 608f540..d022f8a 100644 --- a/goblin.go +++ b/goblin.go @@ -12,6 +12,7 @@ import( "github.com/jienfak/goblin/sort" "github.com/jienfak/goblin/tac" "github.com/jienfak/goblin/ls" + "github.com/jienfak/goblin/yes" ) func main() { @@ -30,6 +31,7 @@ func main() { "sort" : sort.Run, "tac" : tac.Run, "ls" : ls.Run, + "yes" : yes.Run, } if binBase := path.Base(os.Args[0]) ; binBase != "goblin" { diff --git a/sort/sort.go b/sort/sort.go index 7cd8b26..02f5b76 100644 --- a/sort/sort.go +++ b/sort/sort.go @@ -9,7 +9,7 @@ import ( "flag" ) -func readLines() []string { +func ReadLines() []string { r := bufio.NewReader(os.Stdin) a := make([]string, 0) @@ -28,7 +28,7 @@ func Run(args []string) int { flagSet.Parse(args[1:]) status := 0 - lines := readLines() + lines := ReadLines() sort.Strings(lines) for _, l := range lines { fmt.Print(l) diff --git a/yes/yes.go b/yes/yes.go new file mode 100644 index 0000000..5bb827f --- /dev/null +++ b/yes/yes.go @@ -0,0 +1,56 @@ +/* Yes program implementation. */ +package yes + +import( + "os" + "fmt" + "flag" + "strings" + "github.com/jienfak/goblin/sort" +) + +func yes(s string){ + for{ + fmt.Print(s) + } +} + +func Run(args []string) int { + var( + stdinFlag bool + nFlag bool + s string + ) + status := 0 + arg0 := args[0] + args = args[1:] + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) + flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.") + flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.") + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0) + flagSet.PrintDefaults() + + } + flagSet.Parse(args) + args = flagSet.Args() + + if stdinFlag { + in := sort.ReadLines() + s = strings.Join(in, "") + } else { + if len(args)>0 { + s = strings.Join(args, " ") + } else { + s = "y" + } + } + + if !nFlag { + s += "\n" + } + + yes(s) + + return status +}