|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/MakeNowJust/heredoc" |
| 12 | + "github.com/loginradius/lr-cli/api" |
| 13 | + "github.com/loginradius/lr-cli/cmdutil" |
| 14 | + "github.com/loginradius/lr-cli/config" |
| 15 | + "github.com/loginradius/lr-cli/request" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +var temp int |
| 21 | + |
| 22 | +type Schema struct { |
| 23 | + Display string `json:"Display"` |
| 24 | + Enabled bool `json:"Enabled"` |
| 25 | + IsMandatory bool `json:"IsMandatory"` |
| 26 | + Parent string `json:"Parent"` |
| 27 | + ParentDataSource string `json:"ParentDataSource"` |
| 28 | + Permission string `json:"Permission"` |
| 29 | + Name string `json:"name"` |
| 30 | + Rules string `json:"rules"` |
| 31 | + Status string `json:"status"` |
| 32 | + Type string `json:"type"` |
| 33 | +} |
| 34 | +type schemaStr struct { |
| 35 | + Data []Schema `json:"Data"` |
| 36 | +} |
| 37 | + |
| 38 | +var urlall string |
| 39 | +var url1 string |
| 40 | + |
| 41 | +func NewschemaCmd() *cobra.Command { |
| 42 | + |
| 43 | + cmd := &cobra.Command{ |
| 44 | + Use: "schema", |
| 45 | + Short: "add schema config", |
| 46 | + Long: `This commmand adds schema config field`, |
| 47 | + Example: heredoc.Doc(`$ lr add schema`), |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + if temp == 0 { |
| 50 | + return &cmdutil.FlagError{Err: errors.New("`field` is required argument")} |
| 51 | + } |
| 52 | + return add(temp) |
| 53 | + |
| 54 | + }, |
| 55 | + } |
| 56 | + fl := cmd.Flags() |
| 57 | + fl.IntVarP(&temp, "field", "f", 0, "field number") |
| 58 | + return cmd |
| 59 | +} |
| 60 | + |
| 61 | +func add(temp int) error { |
| 62 | + |
| 63 | + res, err := api.GetSites() |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + if res.Productplan.Name == "free" { |
| 69 | + fmt.Println("Kindly Upgrade the plan to enable this command for your app") |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + conf := config.GetInstance() |
| 74 | + urlall = conf.AdminConsoleAPIDomain + "/platform-configuration/platform-registration-fields?" |
| 75 | + |
| 76 | + var resultResp schemaStr |
| 77 | + resp, err := request.Rest(http.MethodGet, urlall, nil, "") |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + err = json.Unmarshal(resp, &resultResp) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + var temp1 []int |
| 88 | + for i := 0; i < len(resultResp.Data); i++ { |
| 89 | + if resultResp.Data[i].Parent == "" { |
| 90 | + temp1 = append(temp1, i) |
| 91 | + } |
| 92 | + } |
| 93 | + url1 = conf.AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?" |
| 94 | + var resultResp1 schemaStr |
| 95 | + resp, err = request.Rest(http.MethodGet, url1, nil, "") |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + err = json.Unmarshal(resp, &resultResp1) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + resultResp.Data[temp1[temp-1]].Enabled = true |
| 106 | + var DisplayName string |
| 107 | + var req string |
| 108 | + fmt.Print("Enter the Display Name (" + resultResp.Data[temp1[temp-1]].Display + ") :") |
| 109 | + scanner := bufio.NewScanner(os.Stdin) |
| 110 | + scanner.Scan() |
| 111 | + DisplayName = scanner.Text() |
| 112 | + if DisplayName == "" { |
| 113 | + DisplayName = resultResp.Data[temp1[temp-1]].Display |
| 114 | + } |
| 115 | + fmt.Print("Is Required (y/n):") |
| 116 | + fmt.Scanln(&req) |
| 117 | + for req != "Y" && req != "y" && req != "N" && req != "n" { |
| 118 | + fmt.Print("Please enter (y/n):") |
| 119 | + fmt.Scanln(&req) |
| 120 | + } |
| 121 | + if req == "Y" || req == "y" { |
| 122 | + resultResp.Data[temp1[temp-1]].IsMandatory = true |
| 123 | + } else if req == "N" || req == "n" { |
| 124 | + resultResp.Data[temp1[temp-1]].IsMandatory = false |
| 125 | + } |
| 126 | + resultResp.Data[temp1[temp-1]].Display = DisplayName |
| 127 | + |
| 128 | + resultResp1.Data = append(resultResp1.Data, resultResp.Data[temp1[temp-1]]) |
| 129 | + body, _ := json.Marshal(resultResp1) |
| 130 | + url1 = conf.AdminConsoleAPIDomain + "/platform-configuration/default-fields?" |
| 131 | + var resultResp2 schemaStr |
| 132 | + resp, err = request.Rest(http.MethodPost, url1, nil, string(body)) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + err = json.Unmarshal(resp, &resultResp2) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + fmt.Println("Your field has been sucessfully added") |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments