-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
271 lines (232 loc) · 5.77 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/sheepla/strans/api"
"github.com/sheepla/strans/audio"
ui "github.com/sheepla/strans/ui"
cli "github.com/urfave/cli/v2"
)
//nolint:gochecknoglobals
var (
appName = "strans"
appDescription = "a command line translate tool with GNU Readline like interactive mode"
appVersion = "unknown"
appRevision = "unknown"
)
type exitCode int
const (
exitCodeOK exitCode = iota
exitCodeErrArgs
exitCodeErrTranslate
exitCodeErrIO
exitCodeErrUI
)
func (e exitCode) Int() int {
return int(e)
}
func main() {
if err := initApp().Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "[ ERR ] %s\n", err)
}
}
//nolint:exhaustivestruct,exhaustruct,funlen
func initApp() *cli.App {
app := &cli.App{
Name: appName,
Usage: appDescription,
ArgsUsage: "TEXT...",
Version: fmt.Sprintf("%s-%s", appVersion, appRevision),
UsageText: fmt.Sprintf("%s [OPTIONS] TEXT...\necho TEXT... | %s [OPTIONS] -\n%s [OPTIONS] - < FILE", appName, appName, appName),
Suggest: true,
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "source",
Aliases: []string{"s", "from"},
Required: true,
Usage: "Source language to translate; specifying the value '?' will shows a selectable menu",
EnvVars: []string{"STRANS_SOURCE_LANG"},
Action: func(ctx *cli.Context, s string) error {
if strings.TrimSpace(s) == "" {
return cli.Exit(
"source language must not be empty string",
exitCodeErrArgs.Int(),
)
}
return nil
},
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t", "to"},
Required: true,
Usage: "Target language to translate; specifying the value '?' will shows a selectable menu",
EnvVars: []string{"STRANS_TARGET_LANG"},
Action: func(ctx *cli.Context, s string) error {
if strings.TrimSpace(s) == "" {
return cli.Exit(
"target language must not be empty string",
exitCodeErrArgs.Int(),
)
}
return nil
},
},
&cli.StringFlag{
Name: "instance",
Aliases: []string{"i"},
Required: false,
Usage: "Instance host name of Lingva Translate",
EnvVars: []string{"STRANS_INSTANCE"},
Action: func(ctx *cli.Context, s string) error {
if strings.TrimSpace(s) == "" {
return cli.Exit(
"instance must not be empty string",
exitCodeErrArgs.Int(),
)
}
return nil
},
},
&cli.BoolFlag{
Name: "repl",
Aliases: []string{"r"},
Required: false,
Usage: "Start interactive mode",
},
&cli.BoolFlag{
Name: "audio",
Aliases: []string{"a"},
Required: false,
Usage: "Read translated text aloud",
},
&cli.BoolFlag{
Name: "list-source",
Aliases: []string{"S"},
Usage: "Show a list of source languages",
Required: false,
},
&cli.BoolFlag{
Name: "list-target",
Aliases: []string{"T"},
Usage: "Show a list of target languages",
Required: false,
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{},
Required: false,
Usage: "Enable debug mode",
Hidden: true,
},
}
app.Action = run
return app
}
func run(ctx *cli.Context) error {
if ctx.Bool("list-source") {
langs, err := api.ListSourceLangs(ctx.String("instance"))
if err != nil {
return cli.Exit(
err,
exitCodeErrTranslate.Int(),
)
}
printLangs(ctx.App.Writer, langs)
return cli.Exit("", exitCodeOK.Int())
}
if ctx.Bool("list-target") {
langs, err := api.ListTargetLangs(ctx.String("instance"))
if err != nil {
return cli.Exit(
err,
exitCodeErrTranslate.Int(),
)
}
printLangs(ctx.App.Writer, langs)
return cli.Exit("", exitCodeOK.Int())
}
// Read stdin mode
var text string
if ctx.NArg() == 1 && ctx.Args().First() == "-" {
var err error
text, err = readString(ctx.App.Reader)
if err != nil {
return cli.Exit(
fmt.Sprintf("failed to read stnadard input: %s", err),
exitCodeErrIO.Int(),
)
}
} else {
text = strings.Join(ctx.Args().Slice(), " ")
}
// Get instance param
instance := ctx.String("instance")
// Get source language param
source := ctx.String("source")
if source == "?" {
sourceLangs, err := api.ListSourceLangs(instance)
if err != nil {
return cli.Exit(err, exitCodeErrTranslate.Int())
}
idx, err := ui.SelectLang(*sourceLangs)
if err != nil {
return cli.Exit(err, exitCodeErrUI.Int())
}
source = (*sourceLangs)[idx].Code
}
// Get target language param
target := ctx.String("target")
if target == "?" {
targetLangs, err := api.ListSourceLangs(instance)
if err != nil {
return cli.Exit(err, exitCodeErrTranslate.Int())
}
idx, err := ui.SelectLang(*targetLangs)
if err != nil {
return cli.Exit(err, exitCodeErrUI.Int())
}
target = (*targetLangs)[idx].Code
}
// Create parameter
param, err := api.NewTranslateParam(source, target, text, instance)
if err != nil {
return cli.Exit(err, exitCodeErrArgs.Int())
}
// Start REPL mode
if ctx.Bool("repl") {
ui.Repl(param, ctx.Bool("audio"))
return cli.Exit("", exitCodeOK.Int())
}
// Execute translate
result, err := api.Translate(param)
if err != nil {
return cli.Exit(err, exitCodeErrTranslate.Int())
}
// Output translated text
fmt.Fprintln(ctx.App.Writer, result.Text)
// Read translated text aloud
if ctx.Bool("audio") {
if err := audio.FetchAndPlay(param.TargetLang, result.Text, param.Instance); err != nil {
return cli.Exit(err, exitCodeErrIO.Int())
}
}
return cli.Exit("", exitCodeOK.Int())
}
func readString(r io.Reader) (string, error) {
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(r); err != nil {
//nolint:wrapcheck
return "", err
}
return buf.String(), nil
}
func printLangs(w io.Writer, langs *[]api.Lang) {
for _, lang := range *langs {
fmt.Fprintf(w, "%s\t%s\n", lang.Code, lang.Name)
}
}