-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapplication.go
More file actions
119 lines (96 loc) · 3.34 KB
/
application.go
File metadata and controls
119 lines (96 loc) · 3.34 KB
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
package delete
import (
"io"
"github.com/ibm-verify/verify-sdk-go/pkg/config/applications"
errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
"github.com/ibm-verify/verify-sdk-go/pkg/i18n"
"github.com/ibm-verify/verifyctl/pkg/config"
cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
"github.com/ibm-verify/verifyctl/pkg/util/templates"
"github.com/spf13/cobra"
)
const (
applicationUsage = "application [options]"
applicationMessagePrefix = "DeleteApplication"
applicationEntitlements = "Manage applications"
applicationResourceName = "application"
)
var (
applicationLongDesc = templates.LongDesc(cmdutil.TranslateLongDesc(applicationMessagePrefix, `
Delete Verify Application based on ApplicationID.
Resources managed on Verify have specific entitlements, so ensure that the application or API client used
with the 'auth' command is configured with the appropriate entitlements.
You can identify the entitlement required by running:
verifyctl delete application --entitlements`))
applicationExamples = templates.Examples(cmdutil.TranslateExamples(messagePrefix, `
# Delete an Application
verifyctl delete application --applicationID "applicationID"`,
))
)
type applicationsOptions struct {
options
applicationID string
config *config.CLIConfig
}
func NewApplicationCommand(config *config.CLIConfig, streams io.ReadWriter) *cobra.Command {
o := &applicationsOptions{
config: config,
}
cmd := &cobra.Command{
Use: applicationUsage,
Short: cmdutil.TranslateShortDesc(applicationMessagePrefix, "Delete Verify Application based on an application ID."),
Long: applicationLongDesc,
Example: applicationExamples,
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.ExitOnError(cmd, o.Complete(cmd, args))
cmdutil.ExitOnError(cmd, o.Validate(cmd, args))
cmdutil.ExitOnError(cmd, o.Run(cmd, args))
},
}
cmd.SetOut(streams)
cmd.SetErr(streams)
cmd.SetIn(streams)
o.AddFlags(cmd)
return cmd
}
func (o *applicationsOptions) AddFlags(cmd *cobra.Command) {
o.addCommonFlags(cmd)
cmd.Flags().StringVar(&o.applicationID, "applicationID", o.applicationID, i18n.Translate("applicationID to be deleted"))
}
func (o *applicationsOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
func (o *applicationsOptions) Validate(cmd *cobra.Command, args []string) error {
if o.entitlements {
return nil
}
calledAs := cmd.CalledAs()
if calledAs == "application" && o.applicationID == "" {
return errorsx.G11NError(i18n.Translate("'applicationID' flag is required"))
}
return nil
}
func (o *applicationsOptions) Run(cmd *cobra.Command, args []string) error {
if o.entitlements {
cmdutil.WriteString(cmd, entitlementsMessage+" "+applicationEntitlements)
return nil
}
_, err := o.config.SetAuthToContext(cmd.Context())
if err != nil {
return err
}
if cmd.CalledAs() == "application" || len(o.applicationID) > 0 {
return o.handleSingleApplication(cmd, args)
}
return nil
}
func (o *applicationsOptions) handleSingleApplication(cmd *cobra.Command, _ []string) error {
c := applications.NewApplicationClient()
err := c.DeleteApplicationByID(cmd.Context(), o.applicationID)
if err != nil {
return err
}
cmdutil.WriteString(cmd, "Resource deleted: "+o.applicationID)
return nil
}