Skip to content

Commit f3e69ed

Browse files
ilharpgopherbot
authored andcommitted
cmd/gotext: fix misbehaviors
The existing implementation has misbehaviors described in golang/go#56842: - `gotext extract` ignores `lang` flag - `gotext generate` ignores `out` flag - Misbehavior of `gotext generate` when no flag specified This commit fixes these bugs: - Update `Command.UsageLine` for `cmdGenerate` - Fix flag misbehaviors by encapsuling flag definition statements into individual `Command.Init()` functions of commands - Fix `gotext generate` misbehavior by executing `pipeline.State.Merge()` before `pipeline.State.Generate()` Fixes golang/go#56842 Change-Id: Id5e324b573b2b389bec22181482a97445230d0cc Reviewed-on: https://go-review.googlesource.com/c/text/+/452115 Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]>
1 parent ab07ad1 commit f3e69ed

File tree

7 files changed

+42
-28
lines changed

7 files changed

+42
-28
lines changed

cmd/gotext/doc.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gotext/extract.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ import (
1414
// - handle features (gender, plural)
1515
// - message rewriting
1616

17-
func init() {
18-
lang = cmdExtract.Flag.String("lang", "en-US", "comma-separated list of languages to process")
19-
}
20-
2117
var cmdExtract = &Command{
18+
Init: initExtract,
2219
Run: runExtract,
2320
UsageLine: "extract <package>*",
2421
Short: "extracts strings to be translated from code",
2522
}
2623

24+
func initExtract(cmd *Command) {
25+
lang = cmd.Flag.String("lang", "en-US", "comma-separated list of languages to process")
26+
}
27+
2728
func runExtract(cmd *Command, config *pipeline.Config, args []string) error {
2829
config.Packages = args
2930
state, err := pipeline.Extract(config)

cmd/gotext/generate.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import (
88
"golang.org/x/text/message/pipeline"
99
)
1010

11-
func init() {
12-
out = cmdGenerate.Flag.String("out", "", "output file to write to")
13-
}
14-
1511
var cmdGenerate = &Command{
12+
Init: initGenerate,
1613
Run: runGenerate,
17-
UsageLine: "generate <package>",
14+
UsageLine: "generate <package> [-out <gofile>]",
1815
Short: "generates code to insert translated messages",
1916
}
2017

18+
func initGenerate(cmd *Command) {
19+
out = cmd.Flag.String("out", "", "output file to write to")
20+
}
21+
2122
func runGenerate(cmd *Command, config *pipeline.Config, args []string) error {
2223
config.Packages = args
2324
s, err := pipeline.Extract(config)
@@ -27,5 +28,8 @@ func runGenerate(cmd *Command, config *pipeline.Config, args []string) error {
2728
if err := s.Import(); err != nil {
2829
return wrap(err, "import failed")
2930
}
31+
if err := s.Merge(); err != nil {
32+
return wrap(err, "merge failed")
33+
}
3034
return wrap(s.Generate(), "generation failed")
3135
}

cmd/gotext/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func init() {
3535
}
3636

3737
var (
38+
lang *string
39+
out *string
40+
overwrite *bool
41+
3842
srcLang = flag.String("srclang", "en-US", "the source-code language")
3943
dir = flag.String("dir", "locales", "default subdirectory to store translation files")
4044
)
@@ -57,6 +61,9 @@ func config() (*pipeline.Config, error) {
5761
// A Command is an implementation of a go command
5862
// like go build or go fix.
5963
type Command struct {
64+
// Init initializes the flag set of the command.
65+
Init func(cmd *Command)
66+
6067
// Run runs the command.
6168
// The args are the arguments after the command name.
6269
Run func(cmd *Command, c *pipeline.Config, args []string) error
@@ -139,6 +146,7 @@ func main() {
139146

140147
for _, cmd := range commands {
141148
if cmd.Name() == args[0] && cmd.Runnable() {
149+
cmd.Init(cmd)
142150
cmd.Flag.Usage = func() { cmd.Usage() }
143151
cmd.Flag.Parse(args[1:])
144152
args = cmd.Flag.Args()
@@ -331,6 +339,9 @@ func help(args []string) {
331339
}
332340

333341
func getLangs() (tags []language.Tag) {
342+
if lang == nil {
343+
return []language.Tag{language.AmericanEnglish}
344+
}
334345
for _, t := range strings.Split(*lang, ",") {
335346
if t == "" {
336347
continue

cmd/gotext/rewrite.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@ const printerType = "golang.org/x/text/message.Printer"
1919
// - handle features (gender, plural)
2020
// - message rewriting
2121

22-
func init() {
23-
overwrite = cmdRewrite.Flag.Bool("w", false, "write files in place")
24-
}
25-
26-
var (
27-
overwrite *bool
28-
)
29-
3022
var cmdRewrite = &Command{
23+
Init: initRewrite,
3124
Run: runRewrite,
3225
UsageLine: "rewrite <package>",
3326
Short: "rewrites fmt functions to use a message Printer",
@@ -39,6 +32,10 @@ using Printf to allow translators to reorder arguments.
3932
`,
4033
}
4134

35+
func initRewrite(cmd *Command) {
36+
overwrite = cmd.Flag.Bool("w", false, "write files in place")
37+
}
38+
4239
func runRewrite(cmd *Command, _ *pipeline.Config, args []string) error {
4340
var w io.Writer
4441
if !*overwrite {

cmd/gotext/update.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,18 @@ import (
1414
// - handle features (gender, plural)
1515
// - message rewriting
1616

17-
var (
18-
lang *string
19-
out *string
20-
)
21-
22-
func init() {
23-
lang = cmdUpdate.Flag.String("lang", "en-US", "comma-separated list of languages to process")
24-
out = cmdUpdate.Flag.String("out", "", "output file to write to")
25-
}
26-
2717
var cmdUpdate = &Command{
18+
Init: initUpdate,
2819
Run: runUpdate,
2920
UsageLine: "update <package>* [-out <gofile>]",
3021
Short: "merge translations and generate catalog",
3122
}
3223

24+
func initUpdate(cmd *Command) {
25+
lang = cmd.Flag.String("lang", "en-US", "comma-separated list of languages to process")
26+
out = cmd.Flag.String("out", "", "output file to write to")
27+
}
28+
3329
func runUpdate(cmd *Command, config *pipeline.Config, args []string) error {
3430
config.Packages = args
3531
state, err := pipeline.Extract(config)

message/pipeline/generate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"go/build"
1010
"io"
11+
"os"
1112
"path/filepath"
1213
"regexp"
1314
"sort"
@@ -52,6 +53,10 @@ func (s *State) Generate() error {
5253
gopath := filepath.SplitList(build.Default.GOPATH)[0]
5354
path = filepath.Join(gopath, "src", filepath.FromSlash(pkgs[0].Pkg.Path()))
5455
}
56+
if len(s.Config.GenFile) == 0 {
57+
cw.WriteGo(os.Stdout, pkg, "")
58+
return nil
59+
}
5560
if filepath.IsAbs(s.Config.GenFile) {
5661
path = s.Config.GenFile
5762
} else {

0 commit comments

Comments
 (0)