-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (103 loc) · 2.79 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
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/z0rr0/ytapigo/arguments"
"github.com/z0rr0/ytapigo/config"
"github.com/z0rr0/ytapigo/handle"
)
// Name is a program name.
const Name = "YtAPIGo"
var (
// Version is a version from GIT tags
Version = "0.0.0"
// Revision is GIT revision number
Revision = "git:000000"
// BuildDate is build date
BuildDate = "2016-01-01_01:01:01UTC"
// GoVersion is runtime Go language version
GoVersion = runtime.Version()
logger = log.New(io.Discard, "DEBUG: ", log.Lmicroseconds|log.Lshortfile)
)
func main() {
var (
debug bool
version bool
noCache bool
direction string
timeout = 5 * time.Second
start = time.Now()
)
defer func() {
logger.Printf("total duration %v\n", time.Since(start).Truncate(time.Millisecond))
if r := recover(); r != nil {
if _, e := fmt.Fprintf(os.Stderr, "ERROR: %v\n", r); e != nil {
panic(e)
}
os.Exit(1)
}
}()
configDir, cacheDir, err := defaultDirectories()
if err != nil {
panic(err)
}
configFile := filepath.Join(configDir, "config.json")
flag.BoolVar(&debug, "d", false, "debug mode")
flag.BoolVar(&version, "v", false, "print version")
flag.StringVar(&configFile, "c", configFile, "configuration file")
flag.BoolVar(&noCache, "r", false, "reset cache")
flag.DurationVar(&timeout, "t", timeout, "timeout for requests")
flag.StringVar(
&direction, "g", "",
fmt.Sprintf("translation direction "+
"(empty - 'en-ru' or 'ru-en' by ASCII codes, %q - auto-detected language to ru)", handle.AutoLanguageDetect,
),
)
flag.Parse()
if version {
fmt.Printf("%v: %v %v %v %v\n", Name, Version, Revision, GoVersion, BuildDate)
flag.PrintDefaults()
return
}
cfg, err := config.New(configFile, configDir, cacheDir, noCache, debug, logger)
if err != nil {
panic(err)
}
cfg.Logger.Printf("configuration"+
"\n\tCONFIG:\t%v\n\tKEY:\t%v\n\tCACHE:\t%v", configFile, cfg.Translation.KeyFile, cfg.AuthCache,
)
params, err := arguments.Build(flag.Args(), os.Stdin)
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
y := handle.New(cfg)
if err = y.Run(ctx, direction, params); err != nil {
panic(err)
}
}
// defaultDirectories returns default configuration and cache directories.
func defaultDirectories() (string, string, error) {
var (
configDir string
cacheDir string
err error
appFolder = strings.ToLower(Name)
)
if configDir, err = os.UserConfigDir(); err != nil {
return "", "", fmt.Errorf("user config dir: %v", err)
}
if cacheDir, err = os.UserCacheDir(); err != nil {
return "", "", fmt.Errorf("user cache dir: %v", err)
}
return filepath.Join(configDir, appFolder), filepath.Join(cacheDir, appFolder), nil
}