forked from 99designs/smartling
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdo_init.go
158 lines (133 loc) · 2.77 KB
/
do_init.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
package main
import (
"bytes"
"errors"
"fmt"
"os"
"regexp"
smartling "github.com/Smartling/api-sdk-go"
"github.com/reconquest/hierr-go"
"github.com/tcnksm/go-input"
)
func doInit(config Config, args map[string]interface{}) error {
fmt.Printf("Generating %s...\n\n", config.path)
prompt := func(
message string,
value interface{},
zero bool,
hidden bool,
variable interface{},
) {
display := regexp.MustCompile(`^(.{1,3}).*$`).ReplaceAllString(
fmt.Sprint(value),
`$1***`,
)
if !zero {
message = fmt.Sprintf("%s [default is %q]", message, display)
}
read, err := input.DefaultUI().Ask(
message,
&input.Options{
Default: fmt.Sprint(value),
Hide: hidden,
HideDefault: true,
},
)
if err != nil {
if input.ErrInterrupted == err {
os.Exit(1)
}
}
fmt.Sscanln(read, variable)
}
var input Config
prompt(
"Smartling API V2.0 User Identifier",
config.UserID,
config.UserID == "",
false,
&input.UserID,
)
if input.UserID != "" {
config.UserID = input.UserID
}
prompt(
"Smartling API V2.0 Token Secret",
config.Secret,
config.Secret == "",
true,
&input.Secret,
)
if input.Secret != "" {
config.Secret = input.Secret
}
prompt(
"Account ID (optional)",
config.AccountID,
config.AccountID == "",
false,
&input.AccountID,
)
if input.AccountID != "" {
config.AccountID = input.AccountID
}
prompt(
"Project ID",
config.ProjectID,
config.ProjectID == "",
false,
&input.ProjectID,
)
if input.ProjectID != "" {
config.ProjectID = input.ProjectID
}
var result bytes.Buffer
err := configTemplate.Execute(&result, config)
if err != nil {
return hierr.Errorf(
err,
"unable to compile config template",
)
}
logger.HideFromConfig(config)
fmt.Println("Testing connection to Smartling API...")
client, err := createClient(config, args)
if err != nil {
return hierr.Errorf(
err,
"unable to create client for testing connection",
)
}
err = client.Authenticate()
if err != nil {
if _, ok := err.(smartling.NotAuthorizedError); ok {
return NewError(
errors.New("not authorized"),
"Your credentials are invalid. Double check them and run "+
"init command again",
)
} else {
return NewError(
hierr.Errorf(err, "failure while testing connection"),
"Contact developer for more info",
)
}
}
fmt.Println("Connection is successful.")
if args["--dry-run"].(bool) {
fmt.Println()
fmt.Println("Configured for Dry run. Not writing config file.")
fmt.Println("New config is displayed below.")
fmt.Println()
fmt.Println(result.String())
} else {
err = os.WriteFile(config.path, result.Bytes(), 0644)
if err != nil {
return hierr.Errorf(
err,
"unable to write new config file",
)
}
}
return nil
}