Skip to content

Commit ac005af

Browse files
RevanthTanneerumohammed786
authored andcommitted
Implement registration schema commands in LoginRadius CLI
Co-authored-by: RevanthTanneeru <revanthtanneeru@LRIN-ENG-36.local> Removed the Usage when error occurs in the cmd Updated Doc Script
1 parent 2b9898f commit ac005af

12 files changed

Lines changed: 200 additions & 44 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ $ brew tap loginradius/tap
2525
$ brew install lr
2626
```
2727

28+
### How to Upgrade the loginradius cli using Homebrew?
29+
30+
```sh
31+
$ brew upgrade loginradius/tap/lr
32+
```
33+
2834
## Other Platforms
2935

3036
Download packaged binaries from the [release page](https://github.com/loginradius/lr-cli/releases/latest).

api/deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type SitesReponse struct {
3636
Privacypolicy interface{} `json:"PrivacyPolicy"`
3737
Termsofservice interface{} `json:"TermsOfService"`
3838
Ownerid string `json:"OwnerId"`
39-
Productplan struct {
39+
Productplan *struct {
4040
Name string `json:"Name"`
4141
Expirytime time.Time `json:"ExpiryTime"`
4242
Billingcycle interface{} `json:"BillingCycle"`

cmd/add/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package add
33
import (
44
"github.com/loginradius/lr-cli/cmd/add/account"
55
"github.com/loginradius/lr-cli/cmd/add/domain"
6+
"github.com/loginradius/lr-cli/cmd/add/schema"
67
"github.com/loginradius/lr-cli/cmd/add/site"
78
"github.com/loginradius/lr-cli/cmd/add/social"
89

@@ -29,5 +30,8 @@ func NewaddCmd() *cobra.Command {
2930
accountCmd := account.NewaccountCmd()
3031
cmd.AddCommand(accountCmd)
3132

33+
schemaCmd := schema.NewschemaCmd()
34+
cmd.AddCommand(schemaCmd)
35+
3236
return cmd
3337
}

cmd/add/schema/schema.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

cmd/cmd.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/loginradius/lr-cli/cmd/root"
85
)
96

107
// Execute adds all child commands to the root command and sets flags appropriately.
118
// This is called by main.main(). It only needs to happen once to the rootCmd.
129
func Execute() {
1310
rootCmd := root.NewRootCmd()
14-
if err := rootCmd.Execute(); err != nil {
15-
fmt.Println(err)
16-
os.Exit(1)
17-
}
11+
rootCmd.Execute()
1812
}

cmd/delete/delete.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/loginradius/lr-cli/cmd/delete/account"
55
"github.com/loginradius/lr-cli/cmd/delete/domain"
66
"github.com/loginradius/lr-cli/cmd/delete/schema"
7+
78
"github.com/loginradius/lr-cli/cmd/delete/site"
89
"github.com/loginradius/lr-cli/cmd/delete/social"
910

cmd/delete/schema/schema.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,35 +61,44 @@ func NewschemaCmd() *cobra.Command {
6161
}
6262

6363
func delete(Field string) error {
64-
res, err2 := api.GetSites()
65-
if (res.Productplan) != res.Productplan || res.Productplan.Name == "free" {
64+
res, err := api.GetSites()
65+
if err != nil {
66+
return err
67+
}
68+
if res.Productplan.Name == "free" {
6669
fmt.Println("Kindly Upgrade the plan to enable this command for your app")
6770
return nil
6871
}
69-
if err2 != nil {
70-
return err2
71-
}
72+
73+
7274
var url string
7375
var url1 string
7476
conf := config.GetInstance()
7577

7678
url1 = conf.AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?"
7779
var resultResp1 Result
78-
resp1, err1 := request.Rest(http.MethodGet, url1, nil, "")
79-
err1 = json.Unmarshal(resp1, &resultResp1)
80-
if err1 != nil {
81-
return err1
80+
resp, err := request.Rest(http.MethodGet, url1, nil, "")
81+
err = json.Unmarshal(resp, &resultResp1)
82+
if err != nil {
83+
return err
8284
}
85+
var noMatch = true
8386
for i := 0; i < len(resultResp1.Data); i++ {
8487
if resultResp1.Data[i].Name == Field {
8588
resultResp1.Data[i].Enabled = false
89+
noMatch = false
8690
}
8791
}
92+
if noMatch {
93+
fmt.Println("Please enter the correct field name")
94+
return nil
95+
}
96+
8897
body, _ := json.Marshal(resultResp1)
8998
url = conf.AdminConsoleAPIDomain + "/platform-configuration/default-fields?"
9099

91100
var resultResp Result
92-
resp, err := request.Rest(http.MethodPost, url, nil, string(body))
101+
resp, err = request.Rest(http.MethodPost, url, nil, string(body))
93102
err = json.Unmarshal(resp, &resultResp)
94103
if err != nil {
95104
return err

cmd/get/schema/schema.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,16 @@ func NewschemaCmd() *cobra.Command {
6262
}
6363

6464
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\""
65+
res, err := api.GetSites()
66+
if err != nil {
67+
return err
7168
}
72-
if res.Productplan == re || res.Productplan.Name == "free" {
69+
if res.Productplan.Name == "free" {
7370
fmt.Println("Kindly Upgrade the plan to enable this command for your app")
7471
return nil
7572
}
76-
if err1 != nil {
77-
return err1
78-
}
73+
74+
7975
conf := config.GetInstance()
8076
if temp == "active" {
8177
url = conf.AdminConsoleAPIDomain + "/platform-configuration/registration-form-settings?"
@@ -109,9 +105,11 @@ func get() error {
109105
var num int
110106

111107
// Taking input from user
108+
fmt.Print("Please select a number from 1 to " + fmt.Sprint(len(temp1)) + " :")
112109
fmt.Scanln(&num)
113110
for 1 > num || num > len(temp1) {
114-
fmt.Println("Please select a number from 1 to " + fmt.Sprint(len(temp1)))
111+
fmt.Print("Please select a number from 1 to " + fmt.Sprint(len(temp1)) + " :")
112+
115113
fmt.Scanln(&num)
116114
}
117115
if resultResp.Data[temp1[num-1]].Parent == "" {

cmd/login/login.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func NewLoginCmd() *cobra.Command {
3131
Use: "login",
3232
Short: "Login to LR account",
3333
Long: heredoc.Doc(`
34-
This commmand logs user into the LR account.
34+
This commmand logs user into the LR account.
3535
`),
3636
Example: heredoc.Doc(`
37-
# Opens Interactive Mode
38-
$ lr login
37+
# Opens Interactive Mode
38+
$ lr login
3939
`),
4040
RunE: func(cmd *cobra.Command, args []string) error {
4141
isValid, err := validateLogin()

cmd/root/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ var cfgFile string
2121
func NewRootCmd() *cobra.Command {
2222
// rootCmd represents the base command when called without any subcommands
2323
rootCmd := &cobra.Command{
24-
Use: "lr",
25-
Short: "LR CLI",
26-
Long: `LoginRadius CLI to support LoginRadius API's and workflow`,
24+
Use: "lr",
25+
Short: "LR CLI",
26+
Long: `LoginRadius CLI to support LoginRadius API's and workflow`,
27+
SilenceUsage: true,
2728
}
2829

2930
helpHelper := func(command *cobra.Command, args []string) {

0 commit comments

Comments
 (0)