-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
129 lines (111 loc) · 2.53 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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/99designs/api-sdk-go"
"github.com/urfave/cli"
)
var client *FaultTolerantClient
var Version = "dev"
func init() {
log.SetFlags(0)
}
func logAndQuitIfError(err error) {
if err != nil {
log.Fatalln(err.Error())
}
}
var cmdBefore = func(c *cli.Context) error {
userID := c.GlobalString("userid")
apiKey := c.GlobalString("apikey")
projectID := c.GlobalString("projectid")
configFile := c.GlobalString("configfile")
timeout := c.GlobalInt("timeout")
if configFile == "" {
configFile = "smartling.yml"
}
var err error
ProjectConfig, err = loadConfig(configFile)
if err != nil {
loadProjectErr = fmt.Errorf("Error loading %s: %s", configFile, err.Error())
}
if ProjectConfig != nil {
if apiKey == "" {
apiKey = ProjectConfig.ApiKey
}
if projectID == "" {
projectID = ProjectConfig.ProjectID
}
if userID == "" {
userID = ProjectConfig.UserID
}
}
if apiKey == "" {
log.Fatalln("ApiKey not specified in --apikey or", configFile)
}
if projectID == "" {
log.Fatalln("ProjectID not specified in --projectid or", configFile)
}
if userID == "" {
log.Fatalln("UserID not specified in --userid or", configFile)
}
sc := smartling.NewClient(userID, apiKey)
if timeout != 0 {
sc.HTTP.Timeout = (time.Duration(timeout) * time.Second)
}
client = &FaultTolerantClient{sc, projectID, 10}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "smartling"
app.Usage = "manage translation files using Smartling"
app.Version = Version
app.Before = func(c *cli.Context) error {
if c.Bool("version") {
cli.VersionPrinter(c)
os.Exit(0)
}
return nil
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "apikey, k",
Usage: "Smartling ApiKey",
EnvVar: "SMARTLING_APIKEY",
}, cli.StringFlag{
Name: "userid, u",
Usage: "Smartling User Identifier",
EnvVar: "SMARTLING_USERID",
}, cli.StringFlag{
Name: "projectid, p",
Usage: "Smartling Project ID",
EnvVar: "SMARTLING_PROJECTID",
}, cli.StringFlag{
Name: "configfile,c",
Usage: "Project config file to use",
EnvVar: "SMARTLING_CONFIGFILE",
}, cli.IntFlag{
Name: "timeout,t",
Value: 60,
Usage: "Maximum time in seconds for an API request to take",
EnvVar: "SMARTLING_API_TIMEOUT",
},
cli.VersionFlag,
}
app.Commands = []cli.Command{
LsCommand,
StatusCommand,
GetCommand,
PutCommand,
RenameCommand,
RmCommand,
LastmodifiedCommand,
LocalesCommand,
ProjectCommand,
}
err := app.Run(os.Args)
logAndQuitIfError(err)
}