xgotext: added option to exclude directories

This commit is contained in:
Benjamin Böhmke
2020-02-27 19:59:03 +01:00
parent df996c3ae1
commit 5b0ba55f37
2 changed files with 15 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"flag"
"log"
"strings"
"github.com/leonelquinteros/gotext/cli/xgotext/parser"
)
@@ -11,6 +12,7 @@ var (
dirName = flag.String("in", "", "input dir: /path/to/go/pkg")
outputDir = flag.String("out", "", "output dir: /path/to/i18n/files")
defaultDomain = flag.String("default", "default", "Name of default domain")
excludeDirs = flag.String("exclude", ".git", "Comma separated list of directories to exclude")
)
func main() {
@@ -30,7 +32,7 @@ func main() {
Default: *defaultDomain,
}
err := parser.ParseDirRec(*dirName, data)
err := parser.ParseDirRec(*dirName, strings.Split(*excludeDirs, ","), data)
if err != nil {
log.Fatal(err)
}

View File

@@ -1,8 +1,10 @@
package parser
import (
"log"
"os"
"path/filepath"
"strings"
)
// ParseDirFunc parses one directory
@@ -34,7 +36,7 @@ func ParseDir(dirPath, basePath string, data *DomainMap) error {
}
// ParseDirRec calls all known parser for each directory
func ParseDirRec(dirPath string, data *DomainMap) error {
func ParseDirRec(dirPath string, exclude []string, data *DomainMap) error {
dirPath, _ = filepath.Abs(dirPath)
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
@@ -43,6 +45,15 @@ func ParseDirRec(dirPath string, data *DomainMap) error {
}
if info.IsDir() {
// skip directory if in exclude list
subDir, _ := filepath.Rel(dirPath, path)
for _, d := range exclude {
if strings.HasPrefix(subDir, d) {
return nil
}
}
log.Print(path)
err := ParseDir(path, dirPath, data)
if err != nil {
return err