|
| 1 | +package site |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/MakeNowJust/heredoc" |
| 10 | + "github.com/loginradius/lr-cli/api" |
| 11 | + "github.com/loginradius/lr-cli/config" |
| 12 | + "github.com/loginradius/lr-cli/request" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var appid int |
| 17 | +var AppsInfo *api.CoreAppData |
| 18 | +var option string |
| 19 | + |
| 20 | +type Delete struct { |
| 21 | + Isdeleted bool `json:"isdeleted"` |
| 22 | +} |
| 23 | + |
| 24 | +func NewSiteCmd() *cobra.Command { |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "site", |
| 27 | + Short: "Deletes a site", |
| 28 | + Long: heredoc.Doc(` |
| 29 | + This command deletes a site. |
| 30 | + `), |
| 31 | + Example: heredoc.Doc(` |
| 32 | + $ lr delete site --appid <appid> |
| 33 | + Take note of the following changes. Press Y to continue: (Y) |
| 34 | + |
| 35 | + Your site has been deleted |
| 36 | + `), |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + return deleteSite() |
| 39 | + }, |
| 40 | + } |
| 41 | + fl := cmd.Flags() |
| 42 | + fl.IntVarP(&appid, "appid", "i", -1, "AppId of the site") |
| 43 | + return cmd |
| 44 | +} |
| 45 | + |
| 46 | +func deleteSite() error { |
| 47 | + checkApp, err := api.CheckApp(appid) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + if !checkApp { |
| 52 | + fmt.Println("There is no site with this AppID.") |
| 53 | + return nil |
| 54 | + } |
| 55 | + AppsInfo, err = api.AppInfo() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if len(AppsInfo.Apps.Data) == 1 { |
| 60 | + fmt.Println("Unable to delete since there is only 1 remaining App.") |
| 61 | + return nil |
| 62 | + } |
| 63 | + currentID, err := api.CurrentID() |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + if currentID.CurrentAppId == appid { |
| 68 | + fmt.Println("This is the current active site. Please switch to another site before deleting.") |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + fmt.Println("1. All configuration for the App will be lost.") |
| 73 | + fmt.Println("2. All active user data will be removed.") |
| 74 | + fmt.Println("3. You will not be able to create new app with same name.") |
| 75 | + |
| 76 | + fmt.Printf("Take note of the following changes. Press Y to continue: ") |
| 77 | + fmt.Scanf("%s", &option) |
| 78 | + if option != "Y" { |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + res, err := delete() |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + if res { |
| 87 | + fmt.Println("Your App has been deleted") |
| 88 | + } else { |
| 89 | + fmt.Println("Delete action failed") |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func delete() (bool, error) { |
| 96 | + conf := config.GetInstance() |
| 97 | + site := conf.AdminConsoleAPIDomain + "/account/site?" |
| 98 | + body, _ := json.Marshal(map[string]string{ |
| 99 | + "appID": strconv.Itoa(appid), |
| 100 | + "customerId": AppsInfo.Apps.Data[0].Ownerid, |
| 101 | + }) |
| 102 | + resp, err := request.Rest(http.MethodDelete, site, nil, string(body)) |
| 103 | + if err != nil { |
| 104 | + return false, err |
| 105 | + } |
| 106 | + var resObj Delete |
| 107 | + err = json.Unmarshal(resp, &resObj) |
| 108 | + if err != nil { |
| 109 | + return false, err |
| 110 | + } |
| 111 | + if resObj.Isdeleted == true { |
| 112 | + return true, nil |
| 113 | + } |
| 114 | + return false, nil |
| 115 | +} |
0 commit comments