-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
58 lines (48 loc) · 1.36 KB
/
profile.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
package cmd
import (
"context"
"github.com/brawaru/marct/launcher"
"github.com/brawaru/marct/locales"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/urfave/cli/v2"
)
const profilesKey ctxKey = "profiles"
var profileCommand = createCommand(&cli.Command{
Name: "profile",
Aliases: []string{"profiles"},
Usage: locales.Translate(&i18n.Message{
ID: "command.profile.usage",
Other: "Manage game profiles",
}),
Description: locales.Translate(&i18n.Message{
ID: "command.profile.description",
Other: "This command allows you to manage game profiles",
}),
Before: func(ctx *cli.Context) error {
workDir := ctx.Context.Value(instanceKey).(*launcher.Instance)
profiles, err := readProfilesOrExit(workDir)
if err != nil {
return err
}
ctx.Context = context.WithValue(ctx.Context, profilesKey, profiles)
return nil
},
})
func readProfilesOrExit(w *launcher.Instance) (*launcher.Profiles, error) {
profiles, err := w.ReadOrCreateProfiles()
if err != nil {
return nil, cli.Exit(locales.TranslateUsing(&i18n.LocalizeConfig{
TemplateData: map[string]string{
"Error": err.Error(),
},
DefaultMessage: &i18n.Message{
ID: "command.profile-create.error.profiles-read",
Other: "Cannot read profiles file: {{ .Error }}",
},
}), 1)
}
return profiles, nil
}
func init() {
app.Commands = append(app.Commands, profileCommand)
}