-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
124 lines (105 loc) · 4.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"log"
"github.com/s0nerik/goloc/goloc"
"github.com/s0nerik/goloc/registry"
"github.com/s0nerik/goloc/sources"
"gopkg.in/alecthomas/kingpin.v2"
// Register all supported platforms
_ "github.com/s0nerik/goloc/platforms"
)
const remoteSources = `google_sheets`
const localSources = `csv`
// Must be set using '-ldflags "-X main.version=<version>"'
var version string
var availableSources = fmt.Sprintf(`%v, %v`, remoteSources, localSources)
var (
// Basic params
source = kingpin.Flag(`source`, fmt.Sprintf(`Data source. Available sources: %v`, availableSources)).Default(`google_sheets`).String()
platformName = kingpin.Flag(`platform`, `Target platform name.`).Short('p').Required().String()
resDir = kingpin.Flag(`resources`, `Path to the resources folder in the project.`).Short('r').Required().String()
// Local source params
locFilePath = kingpin.Flag(`localizations-file-path`, fmt.Sprintf(`Localizations file path. Required for sources: %v`, localSources)).String()
formatsFilePath = kingpin.Flag(`formats-file-path`, fmt.Sprintf(`Formats file path. Required for sources: %v`, localSources)).String()
// Google Sheets params
sheetID = kingpin.Flag(`spreadsheet`, `Spreadsheet ID. Required if selected source is 'google_sheets'`).Short('s').String()
credentials = kingpin.Flag(`credentials`, `Credentials to access a spreadsheet.`).Short('c').Default(`client_secret.json`).String()
tabName = kingpin.Flag(`tab`, `Localizations tab name.`).Short('t').Default(`localizations`).String()
formatsTabName = kingpin.Flag(`formats-tab`, `Formats tab name.`).Short('f').Default(`formats`).String()
// Advanced configuration
keyColumn = kingpin.Flag(`key-column`, `Title of the key column.`).Default(`key`).String()
stopOnMissing = kingpin.Flag(`stop-on-missing`, `Stop execution if missing localization is found.`).Default(`false`).Bool()
formatNameColumn = kingpin.Flag(`format-name-column`, `Title of the format name column.`).Default(`format`).String()
defFormatName = kingpin.Flag(`default-format-name`, `Name of the format to be used in place of "{}"`).Default("").String()
defLoc = kingpin.Flag(`default-localization`, `Default localization language (e.g. "en"). Specifying this doesn't have any effect if the "--default-localization-file-path" is not specified.`).Default(`en`).String()
defLocPath = kingpin.Flag(`default-localization-file-path`, `Full path to the default localization file. Specify this if you want to write a default localization into a specific file (ignoring the localization path generation logic for a language specified in "--default-localization").`).String()
emptyLocalizationMatch = kingpin.Flag(`empty-localization-match`, `Regex for empty localization string.`).Default(`^$`).Regexp()
// Extra features
missingLocalizationsReport = kingpin.Flag(`missing-localizations-report`, `Specify this flag if you want to only pretty-print missing localizations without generating the actual localization files.`).Default(`false`).Bool()
)
func main() {
kingpin.Version(version)
kingpin.Parse()
platform := registry.GetPlatform(*platformName)
if platform == nil {
log.Fatalf(`Platform "%v" is not supported.`, *platformName)
}
src := resolveSource()
if src == nil {
log.Fatalf(`"%v" is not a supported source. Supported sources: %v.`, *source, availableSources)
}
err := goloc.Run(
src,
platform,
*resDir,
*keyColumn,
*formatNameColumn,
*defLoc,
*defLocPath,
*stopOnMissing,
*missingLocalizationsReport,
*defFormatName,
*emptyLocalizationMatch,
)
if err != nil {
log.Fatal(err)
}
}
func resolveSource() goloc.Source {
switch *source {
case "google_sheets":
if sheetID == nil {
log.Fatalf(`"--spreadsheet" parameter must be specified`)
}
if credentials == nil {
log.Fatalf(`"--credentials" parameter must be specified`)
}
if *tabName == "" {
log.Fatalf(`"--tab" parameter cannot be empty`)
}
if *formatsTabName == "" {
log.Fatalf(`"--formats-tab" parameter cannot be empty`)
}
source, err := sources.GoogleSheets(*credentials, *sheetID, *formatsTabName, *tabName)
if err != nil {
log.Fatalf("can't create googlesheets source, %v", err.Error())
}
return source
case "csv":
if locFilePath == nil {
log.Fatalf(`"--localizations-file-path" parameter must be specified`)
}
if *locFilePath == "" {
log.Fatalf(`"--localizations-file-path" must be a valid file path`)
}
if formatsFilePath == nil {
log.Fatalf(`"--formats-file-path" parameter must be specified`)
}
if *formatsFilePath == "" {
log.Fatalf(`"--formats-file-path" must be a valid file path`)
}
return sources.CSV(*locFilePath, *formatsFilePath)
}
return nil
}