From 328f752b8feaee06987040994de1238309e3dd06 Mon Sep 17 00:00:00 2001 From: James Henstridge Date: Fri, 27 Nov 2020 09:16:03 +0800 Subject: [PATCH] cmd/xgettext-go: add command line wrapper around internal/xgettext --- cmd/xgettext-go/main.go | 107 ++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +- go.sum | 2 + 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 cmd/xgettext-go/main.go diff --git a/cmd/xgettext-go/main.go b/cmd/xgettext-go/main.go new file mode 100644 index 0000000..6485133 --- /dev/null +++ b/cmd/xgettext-go/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "bytes" + "io/ioutil" + "log" + "os" + "strings" + "time" + + "github.com/jessevdk/go-flags" + + "github.com/snapcore/go-gettext/internal/xgettext" +) + +var formatTime = func() string { + return time.Now().Format("2006-01-02 15:04-0700") +} + +type options struct { + FilesFrom string `short:"f" long:"files-from" value-name:"FILE" description:"get list of input files from FILE"` + + Directories []string `short:"D" long:"directory" value-name:"DIRECTORY" description:"add DIRECTORY to list for input files search"` + + Output string `short:"o" long:"output" value-name:"FILE" description:"output to specified file"` + + CommentTags []string `short:"c" long:"add-comments" optional:"true" optional-value:"" value-name:"TAG" description:"place all comment blocks preceding keyword lines in output file"` + + Keywords []string `short:"k" long:"keyword" optional:"true" optional-value:"" value-name:"WORD" description:"look for WORD as the keyword for singular strings"` + + NoLocation bool `long:"no-location" description:"do not write '#: filename:line' lines"` + + SortOutput bool `short:"s" long:"sort-output" description:"generate sorted output"` + + PackageName string `long:"package-name" value-name:"PACKAGE" description:"set package name in output"` + + MsgidBugsAddress string `long:"msgid-bugs-address" default:"EMAIL" value-name:"ADDRESS" description:"set report address for msgid bugs"` +} + +func main() { + // parse args + var opts options + args, err := flags.ParseArgs(&opts, os.Args) + if err != nil { + if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { + os.Exit(0) + } + os.Exit(1) + } + + var files []string + if opts.FilesFrom != "" { + content, err := ioutil.ReadFile(opts.FilesFrom) + if err != nil { + log.Fatalf("cannot read file %v: %v", opts.FilesFrom, err) + } + content = bytes.TrimSpace(content) + files = strings.Split(string(content), "\n") + } else { + files = args[1:] + } + + extractor := xgettext.Extractor{ + Directories: opts.Directories, + CommentTags: opts.CommentTags, + SortOutput: opts.SortOutput, + NoLocation: opts.NoLocation, + PackageName: opts.PackageName, + MsgidBugsAddress: opts.MsgidBugsAddress, + CreationDate: formatTime(), + } + log.Printf("keywords: %#v", opts.Keywords) + addDefaultKeywords := true + for _, spec := range opts.Keywords { + if spec == "" { + // a bare "-k" option disables the default keywords + addDefaultKeywords = false + continue + } + kw, err := xgettext.ParseKeyword(spec) + if err != nil { + log.Fatalf("cannot parse keyword %s: %s", spec, err) + } + extractor.Keywords = append(extractor.Keywords, kw) + } + if addDefaultKeywords { + extractor.AddDefaultKeywords() + } + + for _, filename := range files { + if err := extractor.ParseFile(filename); err != nil { + log.Fatalf("cannot parse file %s: %s", filename, err) + } + } + + out := os.Stdout + if opts.Output != "" { + var err error + out, err = os.Create(opts.Output) + if err != nil { + log.Fatalf("failed to create %s: %s", opts.Output, err) + } + } + if err := extractor.Write(out); err != nil { + log.Fatalf("failed to write po template: %s", err) + } +} diff --git a/go.mod b/go.mod index 96c6722..be1242b 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/snapcore/go-gettext go 1.18 -require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c +require ( + github.com/jessevdk/go-flags v1.4.0 + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c +) require ( github.com/kr/pretty v0.3.1 // indirect diff --git a/go.sum b/go.sum index c17af7e..e1e80f1 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=