|
| 1 | +package hooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/MakeNowJust/heredoc" |
| 10 | + "github.com/loginradius/lr-cli/api" |
| 11 | + "github.com/loginradius/lr-cli/cmdutil" |
| 12 | + "github.com/loginradius/lr-cli/config" |
| 13 | + "github.com/loginradius/lr-cli/request" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type Delete struct { |
| 18 | + Isdeleted bool `json:"isdeleted"` |
| 19 | +} |
| 20 | + |
| 21 | +var hookid string |
| 22 | +var option string |
| 23 | + |
| 24 | +func NewHooksCmd() *cobra.Command { |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "hooks", |
| 27 | + Short: "deletes hooks", |
| 28 | + Long: heredoc.Doc(` |
| 29 | + This command deletes webhooks configured with an App. |
| 30 | + `), |
| 31 | + Example: heredoc.Doc(` |
| 32 | + $ lr delete hooks --hookid <hookid> |
| 33 | + (Y) |
| 34 | +
|
| 35 | + Webhook has been deleted. |
| 36 | + |
| 37 | + `), |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + if hookid == "" { |
| 40 | + return &cmdutil.FlagError{Err: errors.New("`--hookid` is required argument")} |
| 41 | + } |
| 42 | + return deleteHooks() |
| 43 | + }, |
| 44 | + } |
| 45 | + fl := cmd.Flags() |
| 46 | + fl.StringVarP(&hookid, "hookid", "i", "", "Hook Unique ID") |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +func deleteHooks() error { |
| 51 | + err := api.CurrentPlan() |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + checkHookID, err := api.CheckHookID(hookid) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if !checkHookID { |
| 60 | + fmt.Println("Hook ID does not exist.") |
| 61 | + return nil |
| 62 | + } |
| 63 | + fmt.Printf("Are you sure you want to proceed ? Press Y to continue: ") |
| 64 | + fmt.Scanf("%s", &option) |
| 65 | + if option != "Y" { |
| 66 | + return nil |
| 67 | + } |
| 68 | + isDeleted, err := delete() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + if isDeleted { |
| 73 | + fmt.Println("Webhook has been deleted.") |
| 74 | + } else { |
| 75 | + fmt.Println("Delete action failed.") |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func delete() (bool, error) { |
| 81 | + conf := config.GetInstance() |
| 82 | + delete := conf.AdminConsoleAPIDomain + "/integrations/webhook/" + hookid |
| 83 | + resp, err := request.Rest(http.MethodDelete, delete, nil, "") |
| 84 | + if err != nil { |
| 85 | + return false, err |
| 86 | + } |
| 87 | + var status Delete |
| 88 | + err = json.Unmarshal(resp, &status) |
| 89 | + if err != nil { |
| 90 | + return false, err |
| 91 | + } |
| 92 | + if status.Isdeleted == true { |
| 93 | + return true, nil |
| 94 | + } |
| 95 | + return false, nil |
| 96 | + |
| 97 | +} |
0 commit comments