|
| 1 | +package sott |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/MakeNowJust/heredoc" |
| 9 | + "github.com/loginradius/lr-cli/api" |
| 10 | + "github.com/loginradius/lr-cli/config" |
| 11 | + "github.com/loginradius/lr-cli/prompt" |
| 12 | + "github.com/loginradius/lr-cli/request" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var token string |
| 17 | +var option bool |
| 18 | +var all *bool |
| 19 | + |
| 20 | +type Response struct { |
| 21 | + Isdeleted bool `json:"isdeleted"` |
| 22 | +} |
| 23 | + |
| 24 | +func NewSottCmd() *cobra.Command { |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "sott", |
| 27 | + Short: "Deletes SOTTs", |
| 28 | + Long: `Use this command to delete a single or all SOTTs configured to your app.`, |
| 29 | + Example: heredoc.Doc(` |
| 30 | + $ lr delete sott --token <value> //Pass Authenticity token of SOTT to be deleted as value. |
| 31 | +
|
| 32 | + SOTT deleted successfully. |
| 33 | +
|
| 34 | + $ lr delete sott --all //Deletes all SOTTs |
| 35 | +
|
| 36 | + All SOTTs for your app have been deleted successfully. |
| 37 | +
|
| 38 | + `), |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + return deleteSott() |
| 41 | + |
| 42 | + }, |
| 43 | + } |
| 44 | + fl := cmd.Flags() |
| 45 | + all = fl.Bool("all", false, "Deletes all SOTT") |
| 46 | + fl.StringVarP(&token, "token", "t", "", "Authenticity Token") |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +func deleteSott() error { |
| 51 | + if token == "--all" { |
| 52 | + fmt.Println("Use exactly one of the following flags:") |
| 53 | + fmt.Println("--all: Deletes all SOTTs configured to your app.") |
| 54 | + fmt.Println("--token: Deletes SOTT with matching Authenticity token.") |
| 55 | + return nil |
| 56 | + } |
| 57 | + err := prompt.Confirm("Are you sure you want to proceed ?", &option) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + if !option { |
| 62 | + return nil |
| 63 | + } |
| 64 | + conf := config.GetInstance() |
| 65 | + if !*all && token != "" { |
| 66 | + checkToken, err := api.CheckToken(token) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + if !checkToken { |
| 71 | + fmt.Println("SOTT with this Authenticity Token does not exist.") |
| 72 | + return nil |
| 73 | + } |
| 74 | + url := conf.AdminConsoleAPIDomain + "/deployment/sott?" + "authenticityToken=" + token |
| 75 | + isDeleted, err := delete(url) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + if isDeleted { |
| 80 | + fmt.Println("SOTT deleted successfully.") |
| 81 | + } else { |
| 82 | + fmt.Println("Delete action failed.") |
| 83 | + } |
| 84 | + } else if *all && token == "" { |
| 85 | + url := conf.AdminConsoleAPIDomain + "/deployment/sott/all?" |
| 86 | + isDeleted, err := delete(url) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + if isDeleted { |
| 91 | + fmt.Println("All SOTTs have been deleted successfully.") |
| 92 | + } else { |
| 93 | + fmt.Println("Delete action failed.") |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func delete(url string) (bool, error) { |
| 100 | + resp, err := request.Rest(http.MethodDelete, url, nil, "") |
| 101 | + if err != nil { |
| 102 | + return false, err |
| 103 | + } |
| 104 | + var status Response |
| 105 | + err = json.Unmarshal(resp, &status) |
| 106 | + if err != nil { |
| 107 | + return false, err |
| 108 | + } |
| 109 | + if status.Isdeleted == true { |
| 110 | + return true, nil |
| 111 | + } |
| 112 | + return false, nil |
| 113 | +} |
0 commit comments