/
domain.go
67 lines (55 loc) · 1.66 KB
/
domain.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
package domain
import (
"context"
"fmt"
"strconv"
"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/delete/domain"
api "github.com/aziontech/azion-cli/pkg/api/domain"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
func NewCmd(f *cmdutil.Factory) *cobra.Command {
var domainID int64
cmd := &cobra.Command{
Use: msg.Usage,
Short: msg.ShortDescription,
Long: msg.LongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azion delete domain --domain-id 1234
`),
RunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed("domain-id") {
answer, err := utils.AskInput(msg.AskDeleteInput)
if err != nil {
return err
}
num, err := strconv.ParseInt(answer, 10, 64)
if err != nil {
logger.Debug("Error while converting answer to int64", zap.Error(err))
return msg.ErrorConvertId
}
domainID = num
}
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
ctx := context.Background()
err := client.Delete(ctx, domainID)
if err != nil {
return fmt.Errorf(msg.ErrorFailToDeleteDomain.Error(), err)
}
deleteOut := output.GeneralOutput{
Msg: fmt.Sprintf(msg.OutputSuccess, domainID),
Out: f.IOStreams.Out}
return output.Print(&deleteOut)
},
}
cmd.Flags().Int64Var(&domainID, "domain-id", 0, msg.FlagId)
cmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
return cmd
}