generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
52 lines (46 loc) · 989 Bytes
/
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
package main // import "moul.io/moulsay"
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/peterbourgon/ff/v3/ffcli"
"moul.io/moulsay/moulsay"
)
func main() {
var (
fs = flag.NewFlagSet("app", flag.ExitOnError)
maxWidth = fs.Int("max-width", 72, "set max width")
)
root := &ffcli.Command{
Name: "moul say",
ShortUsage: "moulsay [OPTS] words...",
ShortHelp: "moulsay -h",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
return run(ctx, args, *maxWidth)
},
}
if err := root.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context, args []string, maxWidth int) error {
message := strings.Join(args, " ")
if message == "" {
in, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
message = string(in)
}
out, err := moulsay.Say(message, maxWidth)
if err != nil {
return err
}
fmt.Println(out)
return nil
}