-
Notifications
You must be signed in to change notification settings - Fork 31
/
edge_application.go
152 lines (129 loc) · 3.8 KB
/
edge_application.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
package edgeapplication
import (
"context"
"fmt"
"os"
"strconv"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/delete/edge_application"
app "github.com/aziontech/azion-cli/pkg/api/edge_applications"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
"github.com/tidwall/sjson"
"go.uber.org/zap"
)
type DeleteCmd struct {
Io *iostreams.IOStreams
GetAzion func() (*contracts.AzionApplicationOptions, error)
f *cmdutil.Factory
UpdateJson func(cmd *DeleteCmd) error
}
func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDeleteCmd(f))
}
func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd {
return &DeleteCmd{
Io: f.IOStreams,
GetAzion: utils.GetAzionJsonContent,
f: f,
UpdateJson: updateAzionJson,
}
}
func NewCobraCmd(delete *DeleteCmd) *cobra.Command {
var application_id int64
cmd := &cobra.Command{
Use: msg.Usage,
Short: msg.ShortDescription,
Long: msg.LongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azion delete edge-application --application-id 1234
$ azion delete edge-application --cascade
`),
RunE: func(cmd *cobra.Command, args []string) error {
return delete.run(cmd, application_id)
},
}
cmd.Flags().Int64Var(&application_id, "application-id", 0, msg.FlagId)
cmd.Flags().Bool("cascade", true, msg.CascadeFlag)
cmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
return cmd
}
func (del *DeleteCmd) run(cmd *cobra.Command, application_id int64) error {
ctx := context.Background()
if cmd.Flags().Changed("cascade") {
err := del.Cascade(ctx)
if err != nil {
return err
}
return nil
}
if !cmd.Flags().Changed("application-id") {
qs := []*survey.Question{
{
Name: "id",
Prompt: &survey.Input{Message: msg.AskInput},
Validate: survey.Required,
},
}
answer := ""
err := survey.Ask(qs, &answer)
if err != nil {
logger.Debug("Error while parsing answer", zap.Error(err))
return utils.ErrorParseResponse
}
num, err := strconv.ParseInt(answer, 10, 64)
if err != nil {
logger.Debug("Error while converting answer to int64", zap.Error(err))
return msg.ErrorConvertId
}
application_id = num
}
client := app.NewClient(del.f.HttpClient, del.f.Config.GetString("api_url"), del.f.Config.GetString("token"))
err := client.Delete(ctx, application_id)
if err != nil {
return fmt.Errorf(msg.ErrorFailToDeleteApplication.Error(), err)
}
deleteOut := output.GeneralOutput{
Msg: fmt.Sprintf(msg.OutputSuccess, application_id),
Out: del.f.IOStreams.Out,
FlagOutPath: del.f.Out,
FlagFormat: del.f.Format,
}
return output.Print(&deleteOut)
}
func updateAzionJson(cmd *DeleteCmd) error {
path, err := utils.GetWorkingDir()
if err != nil {
return utils.ErrorInternalServerError
}
azionJson := path + "/azion/azion.json"
byteAzionJson, err := os.ReadFile(azionJson)
if err != nil {
return utils.ErrorUnmarshalAzionJsonFile
}
jsonReplaceFunc, err := sjson.Set(string(byteAzionJson), "function.id", 0)
if err != nil {
return msg.ErrorFailedUpdateAzionJson
}
jsonReplaceApp, err := sjson.Set(jsonReplaceFunc, "application.id", 0)
if err != nil {
return msg.ErrorFailedUpdateAzionJson
}
jsonReplaceDomain, err := sjson.Set(jsonReplaceApp, "domain.id", 0)
if err != nil {
return msg.ErrorFailedUpdateAzionJson
}
err = os.WriteFile(azionJson, []byte(jsonReplaceDomain), 0644)
if err != nil {
return fmt.Errorf(utils.ErrorCreateFile.Error(), azionJson)
}
return nil
}