-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
258 lines (212 loc) Β· 6.31 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
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/cloudfoundry-attic/jibber_jabber"
"github.com/itchio/itch-setup/bindata"
"github.com/itchio/itch-setup/cl"
"github.com/itchio/itch-setup/localize"
"github.com/itchio/itch-setup/native"
"github.com/pkg/errors"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
version = "head" // set by command-line on CI release builds
builtAt = "" // set by command-line on CI release builds
commit = "" // set by command-line on CI release builds
target = "" // set by command-line on CI release builds
versionString = "" // formatted on boot from 'version' and 'builtAt'
app = kingpin.New("itch-setup", "The itch installer and self-updater")
)
var cli cl.CLI
func init() {
app.Flag("prefer-launch", "Launch if a valid version of itch is installed").BoolVar(&cli.PreferLaunch)
app.Flag("upgrade", "Upgrade the itch app if necessary").BoolVar(&cli.Upgrade)
app.Flag("uninstall", "Uninstall the itch app").BoolVar(&cli.Uninstall)
app.Flag("info", "Just show info and quit").BoolVar(&cli.Info)
app.Flag("relaunch", "Relaunch a new version of the itch app").BoolVar(&cli.Relaunch)
app.Flag("relaunch-pid", "PID to wait for before relaunching").IntVar(&cli.RelaunchPID)
app.Flag("appname", "Application name (itch or kitch)").StringVar(&cli.AppName)
app.Flag("silent", "Run installation silently").BoolVar(&cli.Silent)
app.Arg("args", "Arguments to pass down to itch (only supported on Linux & Windows)").StringsVar(&cli.Args)
}
func must(err error) {
if err != nil {
log.Fatalf("Fatal error: %+v", err)
}
}
func detectAppName() {
if cli.AppName != "" {
log.Printf("App name specified on command-line: %s", cli.AppName)
} else if target != "" {
cli.AppName = strings.TrimSuffix(target, "-setup")
log.Printf("App name specified at build time: %s", cli.AppName)
} else {
execPath, err := os.Executable()
must(err)
ext := ""
if runtime.GOOS == "windows" {
ext = ".exe"
}
kitchBinary := fmt.Sprintf("kitch-setup%s", ext)
if filepath.Base(execPath) == kitchBinary {
cli.AppName = "kitch"
} else {
cli.AppName = "itch"
}
log.Printf("App name detected: %s", cli.AppName)
}
app.Name = fmt.Sprintf("%s-setup", cli.AppName)
}
const DefaultLocale = "en-US"
var localizer *localize.Localizer
func main() {
if runtime.GOOS == "darwin" {
// this makes Cocoa very happy.
runtime.LockOSThread()
}
logFileName := filepath.Join(os.TempDir(), "itch-setup-log.txt")
log.Printf("itch-setup will log to %s", logFileName)
logger := &lumberjack.Logger{
Filename: logFileName,
MaxSize: 3, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
}
log.SetOutput(io.MultiWriter(os.Stderr, logger))
app.UsageTemplate(kingpin.CompactUsageTemplate)
app.HelpFlag.Short('h')
if builtAt != "" {
epoch, err := strconv.ParseInt(builtAt, 10, 64)
must(err)
versionString = fmt.Sprintf("%s, built on %s", version, time.Unix(epoch, 0).Format("Jan _2 2006 @ 15:04:05"))
} else {
versionString = fmt.Sprintf("%s, no build date", version)
}
if commit != "" {
versionString = fmt.Sprintf("%s, ref %s", versionString, commit)
}
log.Printf("=========================================")
log.Printf("itch-setup %q starting up at %q with arguments:", versionString, time.Now())
for _, arg := range os.Args {
log.Printf("%q", arg)
}
log.Printf("=========================================")
app.Version(versionString)
app.VersionFlag.Short('V')
app.Author("Amos Wenger <[email protected]>")
cli.VersionString = versionString
var cliArgs []string
// running in environments where we can't control command line arguments will
// cause itch-setup to bail due to kingpin being strict about arugments. We
// try to detect the environments and strip the args before parsing :/
// detect macos app
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-psn") {
// see https://github.com/itchio/itch-setup/issues/3
log.Printf("Filtering out argument %q (passed by macOS when opened with Finder)", arg)
} else {
cliArgs = append(cliArgs, arg)
}
}
// detect EGS, default to preferring launch instead of running setup
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-epicapp=") {
cliArgs = []string{"--prefer-launch"}
break
}
}
_, err := app.Parse(cliArgs)
must(err)
detectAppName()
userLocale, err := jibber_jabber.DetectIETF()
if err != nil {
log.Println("Couldn't detect locale, falling back to default", DefaultLocale)
userLocale = "en-US"
}
envLocale := os.Getenv("ITCH_SETUP_LOCALE")
if envLocale != "" {
log.Println("ITCH_SETUP_LOCALE set, switching to ", envLocale)
userLocale = envLocale
}
log.Println("Locale: ", userLocale)
localizer, err = localize.NewLocalizer(bindata.Asset)
if err != nil {
log.Fatal(err)
}
err = localizer.LoadLocale(userLocale)
if err != nil {
if len(userLocale) >= 2 {
userLocale = userLocale[:2]
err = localizer.LoadLocale(userLocale)
} else {
log.Println("Ignoring locale: ", userLocale)
}
}
if err == nil {
localizer.SetLang(userLocale)
}
cli.Localizer = localizer
nc, err := native.NewCore(cli)
if err != nil {
panic(err)
}
var verbs []string
if cli.Upgrade {
verbs = append(verbs, "upgrade")
}
if cli.Relaunch {
verbs = append(verbs, "relaunch")
}
if cli.Uninstall {
verbs = append(verbs, "uninstall")
}
if cli.Info {
verbs = append(verbs, "info")
}
if len(verbs) > 1 {
nc.ErrorDialog(errors.Errorf("Cannot specify more than one verb: got %s", strings.Join(verbs, ", ")))
}
if len(verbs) == 0 {
verbs = append(verbs, "install")
}
switch verbs[0] {
case "install":
err = nc.Install()
if err != nil {
nc.ErrorDialog(err)
}
case "upgrade":
err = nc.Upgrade()
if err != nil {
jsonlBail(errors.WithMessage(err, "Fatal upgrade error"))
}
case "relaunch":
if cli.RelaunchPID <= 0 {
jsonlBail(errors.Errorf("--relaunch needs a valid --relaunch-pid (got %d)", cli.RelaunchPID))
}
err = nc.Relaunch()
if err != nil {
jsonlBail(errors.WithMessage(err, "Fatal relaunch error"))
}
case "uninstall":
err = nc.Uninstall()
if err != nil {
nc.ErrorDialog(err)
}
case "info":
nc.Info()
}
}
func jsonlBail(err error) {
// TODO: use json-lines
log.Fatalf("%+v", err)
}