-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
136 lines (110 loc) · 2.63 KB
/
config.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
125
126
127
128
129
130
131
132
133
134
135
136
package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/vorlif/xspreak/tmpl"
)
const (
ExtractFormatPot = "pot"
ExtractFormatJSON = "json"
)
type Config struct {
IsVerbose bool
CurrentDir string
SourceDir string
OutputDir string
OutputFile string
CommentPrefixes []string
ExtractErrors bool
ErrorContext string
TemplatePatterns []string
Keywords []*tmpl.Keyword
DefaultDomain string
WriteNoLocation bool
WrapWidth int
DontWrap bool
OmitHeader bool
CopyrightHolder string
PackageName string
BugsAddress string
MaxDepth int
Args []string
Timeout time.Duration
ExtractFormat string
TmplIsMonolingual bool
}
func NewDefault() *Config {
return &Config{
IsVerbose: false,
SourceDir: "",
OutputDir: filepath.Clean("./"),
OutputFile: "",
CommentPrefixes: []string{"TRANSLATORS"},
ExtractErrors: false,
ErrorContext: "errors",
DefaultDomain: "messages",
WriteNoLocation: false,
WrapWidth: 80,
DontWrap: false,
OmitHeader: false,
CopyrightHolder: "THE PACKAGE'S COPYRIGHT HOLDER",
PackageName: "PACKAGE VERSION",
BugsAddress: "",
MaxDepth: 20,
Timeout: 15 * time.Minute,
ExtractFormat: ExtractFormatPot,
}
}
func (c *Config) Prepare() error {
c.ErrorContext = strings.TrimSpace(c.ErrorContext)
c.DefaultDomain = strings.TrimSpace(c.DefaultDomain)
if c.DefaultDomain == "" {
return errors.New("a default domain is required")
}
if c.Timeout < 1*time.Minute {
return errors.New("the value for Timeout must be at least one minute")
}
currentDir, errC := os.Getwd()
if errC != nil {
return errC
}
c.CurrentDir = currentDir
if c.SourceDir == "" {
c.SourceDir = c.CurrentDir
}
//nolint:revive
if absP, err := filepath.Abs(c.SourceDir); err != nil {
return err
} else {
c.SourceDir = absP
}
if c.OutputFile != "" {
c.OutputDir = filepath.Dir(c.OutputFile)
c.OutputFile = filepath.Base(c.OutputFile)
} else {
c.OutputFile = c.DefaultDomain + "." + c.ExtractFormat
}
//nolint:revive
if absP, err := filepath.Abs(c.OutputDir); err != nil {
return err
} else {
c.OutputDir = absP
}
if c.DontWrap {
c.WrapWidth = -1
}
if c.ExtractFormat == "po" {
c.ExtractFormat = ExtractFormatPot
}
if c.ExtractFormat != ExtractFormatPot && c.ExtractFormat != ExtractFormatJSON {
return fmt.Errorf("only the JSON and pot format is supported, you want %v", c.ExtractFormat)
}
if len(c.TemplatePatterns) > 0 && len(c.Keywords) == 0 {
c.Keywords = tmpl.DefaultKeywords("T", c.TmplIsMonolingual)
}
return nil
}