2023-03-21 14:43:48 +00:00
|
|
|
// translations downloads translations, uploads translations, prints summary
|
|
|
|
// for translations, prints unused strings.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-04-03 16:22:15 +01:00
|
|
|
"bufio"
|
2023-03-21 14:43:48 +00:00
|
|
|
"bytes"
|
2024-04-04 13:52:39 +01:00
|
|
|
"cmp"
|
2024-09-02 16:03:37 +01:00
|
|
|
"context"
|
2023-03-21 14:43:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-09-02 16:03:37 +01:00
|
|
|
"log/slog"
|
2024-11-08 14:18:16 +00:00
|
|
|
"maps"
|
2023-03-21 14:43:48 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2023-04-03 16:22:15 +01:00
|
|
|
"os/exec"
|
2023-03-21 14:43:48 +00:00
|
|
|
"path/filepath"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2023-03-21 14:43:48 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-04-03 16:22:15 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2023-03-21 14:43:48 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-09-02 16:03:37 +01:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2023-03-21 14:43:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
twoskyConfFile = "./.twosky.json"
|
|
|
|
localesDir = "./client/src/__locales"
|
|
|
|
defaultBaseFile = "en.json"
|
|
|
|
defaultProjectID = "home"
|
|
|
|
srcDir = "./client/src"
|
|
|
|
twoskyURI = "https://twosky.int.agrd.dev/api/v1"
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
readLimit = 1 * 1024 * 1024
|
2023-09-08 13:04:25 +01:00
|
|
|
uploadTimeout = 20 * time.Second
|
2023-03-21 14:43:48 +00:00
|
|
|
)
|
|
|
|
|
2023-07-20 17:52:14 +01:00
|
|
|
// blockerLangCodes is the codes of languages which need to be fully translated.
|
|
|
|
var blockerLangCodes = []langCode{
|
|
|
|
"de",
|
|
|
|
"en",
|
|
|
|
"es",
|
|
|
|
"fr",
|
|
|
|
"it",
|
|
|
|
"ja",
|
|
|
|
"ko",
|
|
|
|
"pt-br",
|
|
|
|
"pt-pt",
|
|
|
|
"ru",
|
|
|
|
"zh-cn",
|
|
|
|
"zh-tw",
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:43:48 +00:00
|
|
|
// langCode is a language code.
|
|
|
|
type langCode string
|
|
|
|
|
|
|
|
// languages is a map, where key is language code and value is display name.
|
|
|
|
type languages map[langCode]string
|
|
|
|
|
|
|
|
// textlabel is a text label of localization.
|
|
|
|
type textLabel string
|
|
|
|
|
|
|
|
// locales is a map, where key is text label and value is translation.
|
|
|
|
type locales map[textLabel]string
|
|
|
|
|
|
|
|
func main() {
|
2024-09-02 16:03:37 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
l := slogutil.New(nil)
|
|
|
|
|
2023-03-21 14:43:48 +00:00
|
|
|
if len(os.Args) == 1 {
|
|
|
|
usage("need a command")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Args[1] == "help" {
|
|
|
|
usage("")
|
|
|
|
}
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
conf := errors.Must(readTwoskyConfig())
|
2023-03-21 14:43:48 +00:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
var cli *twoskyClient
|
2023-03-21 14:43:48 +00:00
|
|
|
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "summary":
|
2024-11-08 14:18:16 +00:00
|
|
|
errors.Check(summary(conf.Languages))
|
2023-03-21 14:43:48 +00:00
|
|
|
case "download":
|
2024-11-08 14:18:16 +00:00
|
|
|
cli = errors.Must(conf.toClient())
|
2023-07-12 14:06:17 +01:00
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
errors.Check(cli.download(ctx, l))
|
2023-03-21 14:43:48 +00:00
|
|
|
case "unused":
|
2024-11-08 14:18:16 +00:00
|
|
|
err := unused(ctx, l, conf.LocalizableFiles[0])
|
|
|
|
errors.Check(err)
|
2023-03-21 14:43:48 +00:00
|
|
|
case "upload":
|
2024-11-08 14:18:16 +00:00
|
|
|
cli = errors.Must(conf.toClient())
|
2023-07-12 14:06:17 +01:00
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
errors.Check(cli.upload())
|
2023-04-03 16:22:15 +01:00
|
|
|
case "auto-add":
|
2024-11-08 14:18:16 +00:00
|
|
|
err := autoAdd(conf.LocalizableFiles[0])
|
|
|
|
errors.Check(err)
|
2023-03-21 14:43:48 +00:00
|
|
|
default:
|
|
|
|
usage("unknown command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// usage prints usage. If addStr is not empty print addStr and exit with code
|
|
|
|
// 1, otherwise exit with code 0.
|
|
|
|
func usage(addStr string) {
|
|
|
|
const usageStr = `Usage: go run main.go <command> [<args>]
|
|
|
|
Commands:
|
|
|
|
help
|
|
|
|
Print usage.
|
|
|
|
summary
|
|
|
|
Print summary.
|
|
|
|
download [-n <count>]
|
2023-04-03 16:22:15 +01:00
|
|
|
Download translations. count is a number of concurrent downloads.
|
2023-03-21 14:43:48 +00:00
|
|
|
unused
|
|
|
|
Print unused strings.
|
|
|
|
upload
|
2023-04-03 16:22:15 +01:00
|
|
|
Upload translations.
|
|
|
|
auto-add
|
|
|
|
Add locales with additions to the git and restore locales with
|
|
|
|
deletions.`
|
2023-03-21 14:43:48 +00:00
|
|
|
|
|
|
|
if addStr != "" {
|
|
|
|
fmt.Printf("%s\n%s\n", addStr, usageStr)
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(usageStr)
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// twoskyConfig is the configuration structure for localization.
|
|
|
|
type twoskyConfig struct {
|
2023-03-21 14:43:48 +00:00
|
|
|
Languages languages `json:"languages"`
|
|
|
|
ProjectID string `json:"project_id"`
|
|
|
|
BaseLangcode langCode `json:"base_locale"`
|
|
|
|
LocalizableFiles []string `json:"localizable_files"`
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// readTwoskyConfig returns twosky configuration.
|
|
|
|
func readTwoskyConfig() (t *twoskyConfig, err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "parsing twosky config: %w") }()
|
2023-04-03 16:22:15 +01:00
|
|
|
|
2023-03-21 14:43:48 +00:00
|
|
|
b, err := os.ReadFile(twoskyConfFile)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
2023-07-12 14:06:17 +01:00
|
|
|
return nil, err
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
var tsc []twoskyConfig
|
2023-03-21 14:43:48 +00:00
|
|
|
err = json.Unmarshal(b, &tsc)
|
|
|
|
if err != nil {
|
2024-11-08 14:18:16 +00:00
|
|
|
return nil, fmt.Errorf("unmarshalling %q: %w", twoskyConfFile, err)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tsc) == 0 {
|
2024-11-08 14:18:16 +00:00
|
|
|
return nil, fmt.Errorf("%q is empty", twoskyConfFile)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf := tsc[0]
|
|
|
|
|
|
|
|
for _, lang := range conf.Languages {
|
|
|
|
if lang == "" {
|
2023-07-12 14:06:17 +01:00
|
|
|
return nil, errors.Error("language is empty")
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:22:15 +01:00
|
|
|
if len(conf.LocalizableFiles) == 0 {
|
2023-07-12 14:06:17 +01:00
|
|
|
return nil, errors.Error("no localizable files specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// twoskyClient is the twosky client with methods for download and upload
|
|
|
|
// translations.
|
|
|
|
type twoskyClient struct {
|
|
|
|
// uri is the base URL.
|
|
|
|
uri *url.URL
|
|
|
|
|
|
|
|
// projectID is the name of the project.
|
|
|
|
projectID string
|
|
|
|
|
|
|
|
// baseLang is the base language code.
|
|
|
|
baseLang langCode
|
2023-07-20 17:52:14 +01:00
|
|
|
|
|
|
|
// langs is the list of codes of languages to download.
|
|
|
|
langs []langCode
|
2023-07-12 14:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// toClient reads values from environment variables or defaults, validates
|
|
|
|
// them, and returns the twosky client.
|
|
|
|
func (t *twoskyConfig) toClient() (cli *twoskyClient, err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "filling config: %w") }()
|
|
|
|
|
2024-04-04 13:52:39 +01:00
|
|
|
uriStr := cmp.Or(os.Getenv("TWOSKY_URI"), twoskyURI)
|
2023-07-12 14:06:17 +01:00
|
|
|
uri, err := url.Parse(uriStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-04 13:52:39 +01:00
|
|
|
projectID := cmp.Or(os.Getenv("TWOSKY_PROJECT_ID"), defaultProjectID)
|
2023-07-12 14:06:17 +01:00
|
|
|
|
|
|
|
baseLang := t.BaseLangcode
|
|
|
|
uLangStr := os.Getenv("UPLOAD_LANGUAGE")
|
|
|
|
if uLangStr != "" {
|
|
|
|
baseLang = langCode(uLangStr)
|
|
|
|
}
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
langs := slices.Sorted(maps.Keys(t.Languages))
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
dlLangStr := os.Getenv("DOWNLOAD_LANGUAGES")
|
2023-07-20 17:52:14 +01:00
|
|
|
if dlLangStr == "blocker" {
|
|
|
|
langs = blockerLangCodes
|
|
|
|
} else if dlLangStr != "" {
|
|
|
|
var dlLangs []langCode
|
|
|
|
dlLangs, err = validateLanguageStr(dlLangStr, t.Languages)
|
2023-07-12 14:06:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
langs = dlLangs
|
2023-04-03 16:22:15 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
return &twoskyClient{
|
|
|
|
uri: uri,
|
|
|
|
projectID: projectID,
|
|
|
|
baseLang: baseLang,
|
|
|
|
langs: langs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateLanguageStr validates languages codes that contain in the str and
|
2023-07-20 17:52:14 +01:00
|
|
|
// returns them or error.
|
|
|
|
func validateLanguageStr(str string, all languages) (langs []langCode, err error) {
|
2023-07-12 14:06:17 +01:00
|
|
|
codes := strings.Fields(str)
|
2023-07-20 17:52:14 +01:00
|
|
|
langs = make([]langCode, 0, len(codes))
|
2023-07-12 14:06:17 +01:00
|
|
|
|
|
|
|
for _, k := range codes {
|
|
|
|
lc := langCode(k)
|
2023-07-20 17:52:14 +01:00
|
|
|
_, ok := all[lc]
|
2023-07-12 14:06:17 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("validating languages: unexpected language code %q", k)
|
|
|
|
}
|
|
|
|
|
2023-07-20 17:52:14 +01:00
|
|
|
langs = append(langs, lc)
|
2023-07-12 14:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return langs, nil
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLocales reads file with name fn and returns a map, where key is text
|
|
|
|
// label and value is localization.
|
|
|
|
func readLocales(fn string) (loc locales, err error) {
|
|
|
|
b, err := os.ReadFile(fn)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loc = make(locales)
|
|
|
|
err = json.Unmarshal(b, &loc)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("unmarshalling %q: %w", fn, err)
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// summary prints summary for translations.
|
|
|
|
func summary(langs languages) (err error) {
|
|
|
|
basePath := filepath.Join(localesDir, defaultBaseFile)
|
|
|
|
baseLoc, err := readLocales(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("summary: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
size := float64(len(baseLoc))
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
keys := slices.Sorted(maps.Keys(langs))
|
2023-03-21 14:43:48 +00:00
|
|
|
|
|
|
|
for _, lang := range keys {
|
|
|
|
name := filepath.Join(localesDir, string(lang)+".json")
|
|
|
|
if name == basePath {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var loc locales
|
|
|
|
loc, err = readLocales(name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("summary: reading locales: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := float64(len(loc)) * 100 / size
|
|
|
|
|
2023-07-20 17:52:14 +01:00
|
|
|
blocker := ""
|
|
|
|
|
|
|
|
// N is small enough to not raise performance questions.
|
|
|
|
ok := slices.Contains(blockerLangCodes, lang)
|
|
|
|
if ok {
|
|
|
|
blocker = " (blocker)"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s\t %6.2f %%%s\n", lang, f, blocker)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// unused prints unused text labels.
|
2024-09-02 16:03:37 +01:00
|
|
|
func unused(ctx context.Context, l *slog.Logger, basePath string) (err error) {
|
2023-07-12 14:06:17 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "unused: %w") }()
|
2023-03-21 14:43:48 +00:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
baseLoc, err := readLocales(basePath)
|
2023-03-21 14:43:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
locDir := filepath.Clean(localesDir)
|
2024-09-02 16:03:37 +01:00
|
|
|
js, err := findJS(ctx, l, locDir)
|
2023-03-21 14:43:48 +00:00
|
|
|
if err != nil {
|
2023-07-12 14:06:17 +01:00
|
|
|
return err
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
return findUnused(js, baseLoc)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// findJS returns list of JavaScript and JSON files or error.
|
2024-09-02 16:03:37 +01:00
|
|
|
func findJS(ctx context.Context, l *slog.Logger, locDir string) (fileNames []string, err error) {
|
2023-07-12 14:06:17 +01:00
|
|
|
walkFn := func(name string, _ os.FileInfo, err error) error {
|
2023-03-21 14:43:48 +00:00
|
|
|
if err != nil {
|
2024-09-02 16:03:37 +01:00
|
|
|
l.WarnContext(ctx, "accessing a path", slogutil.KeyError, err)
|
2023-03-21 14:43:48 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, locDir) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := filepath.Ext(name)
|
|
|
|
if ext == ".js" || ext == ".json" {
|
|
|
|
fileNames = append(fileNames, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-07-12 14:06:17 +01:00
|
|
|
}
|
2023-03-21 14:43:48 +00:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
err = filepath.Walk(srcDir, walkFn)
|
2023-03-21 14:43:48 +00:00
|
|
|
if err != nil {
|
2023-07-12 14:06:17 +01:00
|
|
|
return nil, fmt.Errorf("filepath walking %q: %w", srcDir, err)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
return fileNames, nil
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:22:15 +01:00
|
|
|
// findUnused prints unused text labels from fileNames.
|
|
|
|
func findUnused(fileNames []string, loc locales) (err error) {
|
2023-03-21 14:43:48 +00:00
|
|
|
knownUsed := []textLabel{
|
|
|
|
"blocking_mode_refused",
|
|
|
|
"blocking_mode_nxdomain",
|
|
|
|
"blocking_mode_custom_ip",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range knownUsed {
|
|
|
|
delete(loc, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range fileNames {
|
|
|
|
var buf []byte
|
|
|
|
buf, err = os.ReadFile(fn)
|
|
|
|
if err != nil {
|
2023-04-03 16:22:15 +01:00
|
|
|
return fmt.Errorf("finding unused: %w", err)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k := range loc {
|
|
|
|
if bytes.Contains(buf, []byte(k)) {
|
|
|
|
delete(loc, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
for _, v := range slices.Sorted(maps.Keys(loc)) {
|
2023-03-21 14:43:48 +00:00
|
|
|
fmt.Println(v)
|
|
|
|
}
|
2023-04-03 16:22:15 +01:00
|
|
|
|
|
|
|
return nil
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// autoAdd adds locales with additions to the git and restores locales with
|
|
|
|
// deletions.
|
|
|
|
func autoAdd(basePath string) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "auto add: %w") }()
|
2023-03-28 16:02:32 +01:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
adds, dels, err := changedLocales()
|
2023-03-28 16:02:32 +01:00
|
|
|
if err != nil {
|
2023-07-12 14:06:17 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2023-03-28 16:02:32 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
if slices.Contains(dels, basePath) {
|
|
|
|
return errors.Error("base locale contains deletions")
|
2023-03-28 16:02:32 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
err = handleAdds(adds)
|
2023-03-21 14:43:48 +00:00
|
|
|
if err != nil {
|
2023-07-12 14:06:17 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
err = handleDels(dels)
|
2023-03-28 16:02:32 +01:00
|
|
|
if err != nil {
|
2023-07-12 14:06:17 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil
|
2023-03-28 16:02:32 +01:00
|
|
|
}
|
2023-03-21 14:43:48 +00:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
return nil
|
2023-03-28 16:02:32 +01:00
|
|
|
}
|
2023-03-21 14:43:48 +00:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// handleAdds adds locales with additions to the git.
|
|
|
|
func handleAdds(locales []string) (err error) {
|
|
|
|
if len(locales) == 0 {
|
|
|
|
return nil
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
args := append([]string{"add"}, locales...)
|
|
|
|
code, out, err := aghos.RunCommand("git", args...)
|
2023-03-21 14:43:48 +00:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
if err != nil || code != 0 {
|
|
|
|
return fmt.Errorf("git add exited with code %d output %q: %w", code, out, err)
|
2023-03-21 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-03 16:22:15 +01:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
// handleDels restores locales with deletions.
|
|
|
|
func handleDels(locales []string) (err error) {
|
|
|
|
if len(locales) == 0 {
|
|
|
|
return nil
|
2023-04-03 16:22:15 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
args := append([]string{"restore"}, locales...)
|
|
|
|
code, out, err := aghos.RunCommand("git", args...)
|
2023-04-03 16:22:15 +01:00
|
|
|
|
2023-07-12 14:06:17 +01:00
|
|
|
if err != nil || code != 0 {
|
|
|
|
return fmt.Errorf("git restore exited with code %d output %q: %w", code, out, err)
|
2023-04-03 16:22:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// changedLocales returns cleaned paths of locales with changes or error. adds
|
|
|
|
// is the list of locales with only additions. dels is the list of locales
|
|
|
|
// with only deletions.
|
|
|
|
func changedLocales() (adds, dels []string, err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "getting changes: %w") }()
|
|
|
|
|
|
|
|
cmd := exec.Command("git", "diff", "--numstat", localesDir)
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("piping: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("starting: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 3 {
|
|
|
|
return nil, nil, fmt.Errorf("invalid input: %q", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fields[2]
|
|
|
|
|
|
|
|
if fields[0] == "0" {
|
|
|
|
dels = append(dels, path)
|
|
|
|
} else if fields[1] == "0" {
|
|
|
|
adds = append(adds, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scanner.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("scanning: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("waiting: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adds, dels, nil
|
|
|
|
}
|