|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 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 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +var temp string |
| 18 | + |
| 19 | +type Schema struct { |
| 20 | + Display string `json:"Display"` |
| 21 | + Enabled bool `json:"Enabled"` |
| 22 | + IsMandatory bool `json:"IsMandatory"` |
| 23 | + Parent string `json:"Parent"` |
| 24 | + ParentDataSource string `json:"ParentDataSource"` |
| 25 | + Permission string `json:"Permission"` |
| 26 | + Name string `json:"name"` |
| 27 | + Rules string `json:"rules"` |
| 28 | + Status string `json:"status"` |
| 29 | + Type string `json:"type"` |
| 30 | +} |
| 31 | +type schemaStr struct { |
| 32 | + Data []Schema `json:"Data"` |
| 33 | +} |
| 34 | + |
| 35 | +var url string |
| 36 | + |
| 37 | +func NewschemaCmd() *cobra.Command { |
| 38 | + |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: "schema", |
| 41 | + Short: "get schema config", |
| 42 | + Long: `This commmand lists schema config`, |
| 43 | + Example: heredoc.Doc(`$ lr get schema`), |
| 44 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 45 | + fstatus, _ := cmd.Flags().GetBool("all") |
| 46 | + if fstatus { |
| 47 | + temp = "all" |
| 48 | + } |
| 49 | + fstatus1, _ := cmd.Flags().GetBool("active") |
| 50 | + if fstatus1 { |
| 51 | + temp = "active" |
| 52 | + } |
| 53 | + return get() |
| 54 | + |
| 55 | + }, |
| 56 | + } |
| 57 | + fl := cmd.Flags() |
| 58 | + fl.BoolP("all", "a", false, "option to get all fields") |
| 59 | + fl.BoolP("active", "c", false, "option to get active fields") |
| 60 | + |
| 61 | + return cmd |
| 62 | +} |
| 63 | + |
| 64 | +func get() error { |
| 65 | + res, err1 := api.GetSites() |
| 66 | + var re struct { |
| 67 | + Name string "json:\"Name\"" |
| 68 | + Expirytime time.Time "json:\"ExpiryTime\"" |
| 69 | + Billingcycle interface{} "json:\"BillingCycle\"" |
| 70 | + Fromdate interface{} "json:\"FromDate\"" |
| 71 | + } |
| 72 | + if res.Productplan == re || res.Productplan.Name == "free" { |
| 73 | + fmt.Println("Kindly Upgrade the plan to enable this command for your app") |
| 74 | + return nil |
| 75 | + } |
| 76 | + if err1 != nil { |
| 77 | + return err1 |
| 78 | + } |
| 79 | + conf := config.GetInstance() |
| 80 | + if temp == "active" { |
| 81 | + url = conf.AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?" |
| 82 | + } |
| 83 | + if temp == "all" { |
| 84 | + url = conf.AdminConsoleAPIDomain + "/platform-configuration/platform-registration-fields?" |
| 85 | + } |
| 86 | + |
| 87 | + var resultResp schemaStr |
| 88 | + resp, err := request.Rest(http.MethodGet, url, nil, "") |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + err = json.Unmarshal(resp, &resultResp) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + var j = 0 |
| 99 | + var temp1 []int |
| 100 | + fmt.Println("Select one of the fields to get the schema") |
| 101 | + for i := 0; i < len(resultResp.Data); i++ { |
| 102 | + if resultResp.Data[i].Parent == "" { |
| 103 | + fmt.Print(j + 1) |
| 104 | + fmt.Println("." + resultResp.Data[i].Display) |
| 105 | + j++ |
| 106 | + temp1 = append(temp1, i) |
| 107 | + } |
| 108 | + } |
| 109 | + var num int |
| 110 | + |
| 111 | + // Taking input from user |
| 112 | + fmt.Scanln(&num) |
| 113 | + for 1 > num || num > len(temp1) { |
| 114 | + fmt.Println("Please select a number from 1 to " + fmt.Sprint(len(temp1))) |
| 115 | + fmt.Scanln(&num) |
| 116 | + } |
| 117 | + if resultResp.Data[temp1[num-1]].Parent == "" { |
| 118 | + fmt.Println("Display: " + resultResp.Data[temp1[num-1]].Display) |
| 119 | + fmt.Println("Enabled: ", resultResp.Data[temp1[num-1]].Enabled) |
| 120 | + fmt.Println("IsMandatory: ", resultResp.Data[temp1[num-1]].IsMandatory) |
| 121 | + fmt.Println("Parent: ", resultResp.Data[temp1[num-1]].Parent) |
| 122 | + fmt.Println("ParentDataSource: ", resultResp.Data[temp1[num-1]].ParentDataSource) |
| 123 | + fmt.Println("Permission: ", resultResp.Data[temp1[num-1]].Permission) |
| 124 | + fmt.Println("Name: ", resultResp.Data[temp1[num-1]].Name) |
| 125 | + fmt.Println("Rules: ", resultResp.Data[temp1[num-1]].Rules) |
| 126 | + fmt.Println("Status: ", resultResp.Data[temp1[num-1]].Status) |
| 127 | + fmt.Println("Type: ", resultResp.Data[temp1[num-1]].Type) |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
0 commit comments