Skip to content

Commit

Permalink
cmd/xgettext-go: add command line wrapper around internal/xgettext
Browse files Browse the repository at this point in the history
  • Loading branch information
jhenstridge committed Feb 7, 2024
1 parent 939253a commit 328f752
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
107 changes: 107 additions & 0 deletions cmd/xgettext-go/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 328f752

Please sign in to comment.