-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_remove.go
130 lines (114 loc) · 3.47 KB
/
profile_remove.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
package cmd
import (
"github.com/AlecAivazis/survey/v2"
"github.com/brawaru/marct/launcher"
"github.com/brawaru/marct/locales"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/urfave/cli/v2"
)
var profileRemoveCommand = createCommand(&cli.Command{
Name: "remove",
Aliases: []string{"rm"},
ArgsUsage: locales.Translate(&i18n.Message{
ID: "command.profile-remove.args-usage",
Other: "[profile identifier]",
}),
Usage: locales.Translate(&i18n.Message{
ID: "command.profile-remove.usage",
Other: "Remove profile",
}),
Description: locales.Translate(&i18n.Message{
ID: "command.profile-remove.description",
Other: "Remove existing profile",
}),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: locales.Translate(&i18n.Message{
ID: "command.profile-remove.args.force",
Other: "Remove profile without confirmation",
}),
},
},
Action: func(ctx *cli.Context) error {
workDir := ctx.Context.Value(instanceKey).(*launcher.Instance)
if ctx.NArg() < 1 {
return cli.Exit(locales.Translate(&i18n.Message{
ID: "command.profile-remove.error.no-profile-identifier",
Other: "You must specify profile identifier",
}), 1)
} else if ctx.NArg() > 1 {
return cli.Exit(locales.Translate(&i18n.Message{
ID: "command.profile-remove.error.too-many-arguments",
Other: "Too many arguments, only profile identifier is expected",
}), 1)
}
profileID := ctx.Args().First()
profiles := ctx.Context.Value(profilesKey).(*launcher.Profiles)
profile, ok := profiles.Profiles[profileID]
if !ok {
return cli.Exit(locales.TranslateUsing(&i18n.LocalizeConfig{
TemplateData: map[string]string{
"ProfileID": profileID,
},
DefaultMessage: &i18n.Message{
ID: "command.profile-remove.error.profile-not-found",
Other: "Profile with identifier \"{{ .ProfileID }}\" not found",
},
}), 1)
}
profileName := profile.Name
if profileName == "" {
profileName = locales.Translate(&i18n.Message{
ID: "command.profile-remove.unnamed-profile",
Other: "Unnamed profile",
})
}
if !ctx.Bool("force") {
var answer bool
err := survey.AskOne(&survey.Confirm{
Message: locales.TranslateUsing(&i18n.LocalizeConfig{
TemplateData: map[string]string{
"ProfileName": profileName,
"ProfileID": profileID,
},
DefaultMessage: &i18n.Message{
ID: "command.profile-remove.confirm-remove",
Other: "Are you sure you want to remove {{ .ProfileName }} ({{ .ProfileID }})? Associated files will not be deleted.",
},
}),
}, &answer)
if err != nil {
return cli.Exit(locales.TranslateUsing(&i18n.LocalizeConfig{
TemplateData: map[string]string{
"Error": err.Error(),
},
DefaultMessage: &i18n.Message{
ID: "command.profile-remove.error.ask-confirmation",
Other: "Cannot ask for confirmation: {{ .Error }}",
},
}), 1)
}
if !answer {
return nil
}
}
delete(profiles.Profiles, profileID)
if err := workDir.WriteProfiles(profiles); err != nil {
return cli.Exit(locales.TranslateUsing(&i18n.LocalizeConfig{
TemplateData: map[string]string{
"Error": err.Error(),
},
DefaultMessage: &i18n.Message{
ID: "command.profile-remove.error.write-profiles",
Other: "Cannot save profiles: {{ .Error }}",
},
}), 1)
}
return nil
},
})
func init() {
profileCommand.Subcommands = append(profileCommand.Subcommands, profileRemoveCommand)
}