github.com/liquid-dev/text@v0.3.3-liquid/cmd/gotext/rewrite.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"io"
     9  	"os"
    10  
    11  	"github.com/liquid-dev/text/message/pipeline"
    12  )
    13  
    14  const printerType = "github.com/liquid-dev/text/message.Printer"
    15  
    16  // TODO:
    17  // - merge information into existing files
    18  // - handle different file formats (PO, XLIFF)
    19  // - handle features (gender, plural)
    20  // - message rewriting
    21  
    22  func init() {
    23  	overwrite = cmdRewrite.Flag.Bool("w", false, "write files in place")
    24  }
    25  
    26  var (
    27  	overwrite *bool
    28  )
    29  
    30  var cmdRewrite = &Command{
    31  	Run:       runRewrite,
    32  	UsageLine: "rewrite <package>",
    33  	Short:     "rewrites fmt functions to use a message Printer",
    34  	Long: `
    35  rewrite is typically done once for a project. It rewrites all usages of
    36  fmt to use x/text's message package whenever a message.Printer is in scope.
    37  It rewrites Print and Println calls with constant strings to the equivalent
    38  using Printf to allow translators to reorder arguments.
    39  `,
    40  }
    41  
    42  func runRewrite(cmd *Command, _ *pipeline.Config, args []string) error {
    43  	var w io.Writer
    44  	if !*overwrite {
    45  		w = os.Stdout
    46  	}
    47  	pkg := "."
    48  	switch len(args) {
    49  	case 0:
    50  	case 1:
    51  		pkg = args[0]
    52  	default:
    53  		return errorf("can only specify at most one package")
    54  	}
    55  	return pipeline.Rewrite(w, pkg)
    56  }