-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.go
51 lines (48 loc) · 1.34 KB
/
title.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
package ma
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
)
func CommandTitle() *cli.Command {
return &cli.Command{
Name: "title",
HelpName: "title",
Usage: "Create a title following the specified convention",
Description: "Create a title following the specified convention",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "caser",
Aliases: []string{"c"},
Usage: "The case algorithm to use, one of 'upper', 'lower', or 'title'",
Value: "title",
Required: false,
},
},
Action: func(c *cli.Context) error {
enc := runtime(c).Encoder
tag := runtime(c).Language
var caser cases.Caser
switch c.String("caser") {
case "lower":
caser = cases.Lower(tag)
case "upper":
caser = cases.Upper(tag)
case "title":
caser = cases.Title(tag, cases.NoLower)
default:
return fmt.Errorf("unknown caser: %s", c.String("caser"))
}
for i := 0; i < c.NArg(); i++ {
title := c.Args().Get(i)
runtime(c).Metrics.IncrCounter([]string{c.Command.Name, c.String("caser")}, 1)
log.Info().Str("title", title).Str("caser", c.String("caser")).Str("lang", tag.String()).Msg(c.Command.Name)
if err := enc.Encode(map[string]string{"Title": caser.String(title)}); err != nil {
return err
}
}
return nil
},
}
}